|
| 1 | +# yaml-language-server: $schema=https://www.schemastore.org/github-workflow.json |
| 2 | +--- |
| 3 | +name: 🛡️ GoVulnCheck |
| 4 | + |
| 5 | +on: |
| 6 | + push: |
| 7 | + branches: |
| 8 | + - main |
| 9 | + schedule: |
| 10 | + # yamllint disable-line rule:quoted-strings |
| 11 | + - cron: "30 4 * * *" |
| 12 | + workflow_dispatch: |
| 13 | + |
| 14 | +permissions: {} |
| 15 | + |
| 16 | +concurrency: |
| 17 | + group: ${{ github.workflow }} |
| 18 | + cancel-in-progress: true |
| 19 | + |
| 20 | +env: |
| 21 | + ISSUE_TITLE: "govulncheck: vulnerabilities detected on main" |
| 22 | + ISSUE_LABEL: govulncheck |
| 23 | + |
| 24 | +jobs: |
| 25 | + govulncheck: |
| 26 | + name: 🛡️ GoVulnCheck |
| 27 | + runs-on: ubuntu-24.04 |
| 28 | + timeout-minutes: 15 |
| 29 | + permissions: |
| 30 | + contents: read |
| 31 | + issues: write |
| 32 | + steps: |
| 33 | + - name: ⤵️ Checkout |
| 34 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 35 | + with: |
| 36 | + persist-credentials: false |
| 37 | + |
| 38 | + - name: 🚧 Setup Go |
| 39 | + uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0 |
| 40 | + with: |
| 41 | + go-version-file: go.mod |
| 42 | + |
| 43 | + - name: 🚧 Setup Task |
| 44 | + uses: go-task/setup-task@01a4adf9db2d14c1de7a560f09170b6e0df736aa # v2.1.0 |
| 45 | + with: |
| 46 | + repo-token: ${{ github.token }} |
| 47 | + |
| 48 | + - name: 🔨 Install govulncheck |
| 49 | + run: task go:install:govulncheck |
| 50 | + |
| 51 | + - name: 🔍 Run govulncheck |
| 52 | + id: vulncheck |
| 53 | + run: | |
| 54 | + set +e |
| 55 | + govulncheck -test -json ./... | tee govulncheck.json |
| 56 | + status=${PIPESTATUS[0]} |
| 57 | + set -e |
| 58 | +
|
| 59 | + if [ "$status" -ne 0 ] && [ "$status" -ne 3 ]; then |
| 60 | + echo "govulncheck failed with unexpected exit code: $status" |
| 61 | + exit "$status" |
| 62 | + fi |
| 63 | +
|
| 64 | + if jq -e 'select(.finding != null)' govulncheck.json > /dev/null 2>&1; then |
| 65 | + echo "found=true" >> "$GITHUB_OUTPUT" |
| 66 | + elif jq empty govulncheck.json 2>/dev/null; then |
| 67 | + echo "found=false" >> "$GITHUB_OUTPUT" |
| 68 | + else |
| 69 | + echo "::error::govulncheck produced invalid JSON" |
| 70 | + exit 1 |
| 71 | + fi |
| 72 | +
|
| 73 | + - name: 📋 Build issue body |
| 74 | + if: steps.vulncheck.outputs.found == 'true' |
| 75 | + shell: bash |
| 76 | + run: | |
| 77 | + { |
| 78 | + echo '## 🛡️ govulncheck: vulnerabilities detected on main' |
| 79 | + echo '' |
| 80 | + echo 'The govulncheck workflow has detected known vulnerabilities in project dependencies.' |
| 81 | + echo '' |
| 82 | + echo "**Latest run:** [GitHub Actions](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})" |
| 83 | + echo "**Commit:** \`${{ github.sha }}\`" |
| 84 | + echo "**Date:** $(date -u +%Y-%m-%dT%H:%M:%SZ)" |
| 85 | + echo '' |
| 86 | + echo '### Next steps' |
| 87 | + echo '' |
| 88 | + echo '1. Open the [workflow run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) to review vulnerability details' |
| 89 | + echo '2. Update affected dependencies to their fixed versions' |
| 90 | + # shellcheck disable=SC2016 |
| 91 | + echo '3. Run `go mod tidy` and verify with `govulncheck -test ./...`' |
| 92 | + echo '' |
| 93 | + echo '> This issue is automatically created and updated by the [GoVulnCheck workflow](https://github.com/${{ github.repository }}/actions/workflows/govulncheck.yml).' |
| 94 | + } > govulncheck-issue-body.md |
| 95 | +
|
| 96 | + - name: 🏷️ Ensure label exists |
| 97 | + if: steps.vulncheck.outputs.found == 'true' |
| 98 | + env: |
| 99 | + GH_TOKEN: ${{ github.token }} |
| 100 | + run: | |
| 101 | + gh label create "$ISSUE_LABEL" \ |
| 102 | + --description "Automated govulncheck vulnerability reports" \ |
| 103 | + --color "D93F0B" \ |
| 104 | + --repo "${{ github.repository }}" 2>/dev/null || true |
| 105 | +
|
| 106 | + - name: 🔎 Find existing issue |
| 107 | + id: find-issue |
| 108 | + env: |
| 109 | + GH_TOKEN: ${{ github.token }} |
| 110 | + run: | |
| 111 | + issue_number=$(gh issue list \ |
| 112 | + --label "$ISSUE_LABEL" \ |
| 113 | + --state open \ |
| 114 | + --search "in:title $ISSUE_TITLE" \ |
| 115 | + --json number \ |
| 116 | + --jq '.[0].number // empty' \ |
| 117 | + --repo "${{ github.repository }}" 2>/dev/null || true) |
| 118 | + echo "number=${issue_number}" >> "$GITHUB_OUTPUT" |
| 119 | +
|
| 120 | + - name: 📝 Create or update issue |
| 121 | + if: steps.vulncheck.outputs.found == 'true' |
| 122 | + env: |
| 123 | + GH_TOKEN: ${{ github.token }} |
| 124 | + run: | |
| 125 | + if [ -n "${{ steps.find-issue.outputs.number }}" ]; then |
| 126 | + gh issue edit "${{ steps.find-issue.outputs.number }}" \ |
| 127 | + --body-file govulncheck-issue-body.md \ |
| 128 | + --repo "${{ github.repository }}" |
| 129 | + echo "✅ Updated issue #${{ steps.find-issue.outputs.number }}" |
| 130 | + else |
| 131 | + gh issue create \ |
| 132 | + --title "$ISSUE_TITLE" \ |
| 133 | + --label "$ISSUE_LABEL" \ |
| 134 | + --body-file govulncheck-issue-body.md \ |
| 135 | + --repo "${{ github.repository }}" |
| 136 | + echo "✅ Created new issue" |
| 137 | + fi |
| 138 | +
|
| 139 | + - name: ✅ Close issue if no vulnerabilities |
| 140 | + if: steps.vulncheck.outputs.found == 'false' && steps.find-issue.outputs.number != '' |
| 141 | + env: |
| 142 | + GH_TOKEN: ${{ github.token }} |
| 143 | + run: | |
| 144 | + gh issue close "${{ steps.find-issue.outputs.number }}" \ |
| 145 | + --comment "🎉 All govulncheck vulnerabilities have been resolved as of commit ${{ github.sha }}." \ |
| 146 | + --repo "${{ github.repository }}" |
| 147 | + echo "✅ Closed issue #${{ steps.find-issue.outputs.number }}" |
0 commit comments