Skip to content

Commit e76d304

Browse files
authored
Fail govulncheck if vulnerabilities are found (#6033)
* Fail govulncheck if vulnerabilities are found * Add comment about regex used in the script
1 parent cf684d3 commit e76d304

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed
Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
set -euo pipefail
3+
set -eo pipefail
44

55
mkdir -p ./govulncheck 2>/dev/null
66

@@ -13,23 +13,36 @@ FAILED=0
1313
# Repository prefix to remove from package names
1414
REPO_PREFIX=$(go list -m)
1515

16+
# Use a bash regex to extract the the value of the --format flag
17+
# from the GOVULN_OPTS environment variable
18+
if [[ "$GOVULN_OPTS" =~ .*--format[[:space:]]+([a-z]+).* ]]; then
19+
FORMAT=${BASH_REMATCH[1]}
20+
fi
21+
1622
# Run govulncheck for each package
1723
for pkg in $ALL_PKG_DIRS; do
18-
# Remove the repository prefix from the package name to keep the category names short
19-
# and replace slashes with underscores to make clear that the categories are not nested.
20-
OUTPUT_FILE="./govulncheck/$(echo "$pkg" | sed "s|^$REPO_PREFIX/||" | tr '/' '_').sarif"
21-
echo -e "\nRunning govulncheck for package $pkg"
22-
if ! govulncheck ${GOVULN_OPTS:-} "$pkg" > "$OUTPUT_FILE"; then
23-
echo "govulncheck failed for package $pkg, output saved to $OUTPUT_FILE"
24-
FAILED=1
24+
echo -e "\n**** Running govulncheck for package $pkg"
25+
set +e
26+
if [[ -z $FORMAT ]]; then
27+
govulncheck ${GOVULN_OPTS} $pkg
2528
else
26-
echo "govulncheck succeeded for package $pkg, output saved to $OUTPUT_FILE"
29+
# Remove the repository prefix from the package name to keep the category names short
30+
# and replace slashes with underscores to make clear that the categories are not nested.
31+
OUTPUT_FILE="./govulncheck/$(echo "$pkg" | sed "s|^$REPO_PREFIX/||" | tr '/' '_').$FORMAT"
32+
govulncheck ${GOVULN_OPTS} $pkg > $OUTPUT_FILE
33+
fi
34+
if [ $? -eq 0 ]; then
35+
echo -e "\n**** govulncheck succeeded for package $pkg"
36+
else
37+
echo -e "\n**** govulncheck failed for package $pkg"
38+
FAILED=1
2739
fi
40+
set -e
2841
done
2942

3043
if [ $FAILED -ne 0 ]; then
31-
echo -e "\ngovulncheck failed for one or more packages"
44+
echo -e "\n**** govulncheck failed for one or more packages"
3245
exit 1
3346
fi
3447

35-
echo -e "\ngovulncheck completed successfully for all packages"
48+
echo -e "\n**** govulncheck completed successfully for all packages"

.github/workflows/vuln-scans.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ jobs:
284284

285285
- run: govulncheck --version
286286

287-
- name: Run `govulncheck` script
287+
- name: Run `govulncheck` to generate SARIF files
288288
env:
289289
GOVULN_OPTS: --format sarif --scan symbol
290290
run: ./.github/workflows/scripts/govulncheck-run.sh
@@ -295,6 +295,11 @@ jobs:
295295
name: govulncheck-results
296296
path: ./govulncheck/
297297

298+
- name: Run `govulncheck` to fail the workflow if vulnerabilities are found
299+
env:
300+
GOVULN_OPTS: --show verbose --scan symbol
301+
run: ./.github/workflows/scripts/govulncheck-run.sh
302+
298303
govulncheck-categories:
299304
runs-on: ubuntu-24.04
300305
outputs:
@@ -320,6 +325,7 @@ jobs:
320325
govulncheck-upload:
321326
runs-on: ubuntu-24.04
322327
needs: [govulncheck-run, govulncheck-categories]
328+
if: always() # Always run to upload results to code-scanning even if the scan fails
323329
strategy:
324330
matrix: ${{ fromJSON(needs.govulncheck-categories.outputs.matrix) }}
325331
steps:

0 commit comments

Comments
 (0)