|
| 1 | +name: 'Govulncheck Scan & Issue Creator' |
| 2 | +on: |
| 3 | + schedule: |
| 4 | + # 8:00 every day. |
| 5 | + - cron: '0 8 * * *' |
| 6 | +jobs: |
| 7 | + scan-and-report: |
| 8 | + name: Run govulncheck and Create Issue |
| 9 | + runs-on: ubuntu-24.04 |
| 10 | + permissions: |
| 11 | + contents: read # To check out code |
| 12 | + issues: write # To create issues |
| 13 | + steps: |
| 14 | + - name: Checkout code |
| 15 | + uses: actions/checkout@v5 |
| 16 | + - name: Set up Go |
| 17 | + uses: actions/setup-go@v6 |
| 18 | + with: |
| 19 | + go-version-file: "go.mod" |
| 20 | + - name: Install govulncheck |
| 21 | + run: go install golang.org/x/vuln/cmd/govulncheck@latest |
| 22 | + - name: Run govulncheck and count findings |
| 23 | + id: govulncheck-scan |
| 24 | + run: | |
| 25 | + # Run with -json (which never fails) and save to a file |
| 26 | + govulncheck -json ./... > results.json |
| 27 | + # Count the number of findings using jq. |
| 28 | + COUNT=$(jq -s 'length' results.json) |
| 29 | + echo "Found $COUNT vulnerabilities." |
| 30 | + # Set an output for the next steps to use |
| 31 | + echo "vuln_count=$COUNT" >> $GITHUB_OUTPUT |
| 32 | + - name: Upload scan results artifact |
| 33 | + if: steps.govulncheck-scan.outputs.vuln_count > 0 |
| 34 | + uses: actions/upload-artifact@v4 |
| 35 | + with: |
| 36 | + name: govulncheck-results-json |
| 37 | + path: results.json |
| 38 | + retention-days: 7 |
| 39 | + - name: Create GitHub Issue (if vulns found) |
| 40 | + if: steps.govulncheck-scan.outputs.vuln_count > 0 |
| 41 | + env: |
| 42 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 43 | + GH_REPO: ${{ github.repository }} |
| 44 | + run: | |
| 45 | + ISSUE_TITLE="Security Vulnerabilities Detected in main branch" |
| 46 | + # Check if an open issue with this exact title already exists |
| 47 | + EXISTING_ISSUE=$(gh issue list --state open --search "in:title \"$ISSUE_TITLE\"" --json number -R $GH_REPO) |
| 48 | + if [[ "$EXISTING_ISSUE" == "[]" ]]; then |
| 49 | + echo "No existing issue found. Creating a new one." |
| 50 | + BODY="**Automated Vulnerability Report**\n\n\`govulncheck\` found **${{ steps.govulncheck-scan.outputs.vuln_count }}** vulnerabilities on the \`main\` branch. Please review Workflow Run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} for detail." |
| 51 | + gh issue create --title "$ISSUE_TITLE" --body "$BODY" -R $GH_REPO |
| 52 | + else |
| 53 | + echo "An open issue with this title already exists. Skipping creation." |
| 54 | + fi |
0 commit comments