Skip to content

Commit 3f6d477

Browse files
authored
chore(all): add govulncheck workflow (#2774)
Add a workflow to run govulncheck regularly and create an issue if any vulnerability is found. Example issue: #2777 Fixes #2736
1 parent 5fc9e3b commit 3f6d477

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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

all_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,6 @@ func TestGoModTidy(t *testing.T) {
242242
rungo(t, "mod", "tidy", "-diff")
243243
}
244244

245-
func TestGovulncheck(t *testing.T) {
246-
rungo(t, "run", "golang.org/x/vuln/cmd/[email protected]", "./...")
247-
}
248-
249245
func rungo(t *testing.T, args ...string) {
250246
t.Helper()
251247

0 commit comments

Comments
 (0)