Skip to content

Commit 57ad0ca

Browse files
committed
fix: add better error message to docker-scan and code-scan workflows
1 parent 542f6b5 commit 57ad0ca

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

.github/workflows/code-scan.yml

+13-3
Original file line numberDiff line numberDiff line change
@@ -658,24 +658,34 @@ jobs:
658658
env:
659659
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
660660
run: |
661+
set +e # Do not exit if a command fails
661662
get_alerts () {
662663
alerts="$(gh api \
663664
--method GET \
664665
-H 'Accept: application/vnd.github+json' \
665666
-H 'X-GitHub-Api-Version: 2022-11-28' \
666667
/repos/${GITHUB_REPOSITORY}/code-scanning/alerts \
667668
-F severity="$1" -F state='open' -F ref='${{ github.ref }}' -F per_page='100' -F tool_name="$2" --paginate)"
669+
alerts_exit_code=$? # Save exit code from gh api command
668670
}
669671
get_alerts "error" "Semgrep OSS"
670672
semgrep_alerts=$alerts
671673
672674
get_alerts "critical" "CodeQL"
673675
codeql_alerts=$alerts
674676
675-
if [ "$semgrep_alerts" == "[]" ] && [ "$codeql_alerts" == "[]" ]; then
676-
echo 'GHA_SECURITY_CODE_SCAN_CREATE_ALERT_COMMENT='False >> $GITHUB_ENV
677+
if [ $alerts_exit_code != 0 ]; then
678+
echo "Failed to get alerts from Github. The previous upload vulnerability report step might have failed to be processed/uploaded. Try running the job again"
679+
echo '## Code Scan - Failed to get alerts.
680+
Upload vulnerability report step might have failed to be processed/uploaded.
681+
Try running the job again' >> $GITHUB_STEP_SUMMARY
682+
exit 1
677683
else
678-
echo 'GHA_SECURITY_CODE_SCAN_CREATE_ALERT_COMMENT='True >> $GITHUB_ENV
684+
if [ "$semgrep_alerts" == "[]" ] && [ "$codeql_alerts" == "[]" ]; then
685+
echo 'GHA_SECURITY_CODE_SCAN_CREATE_ALERT_COMMENT='False >> $GITHUB_ENV
686+
else
687+
echo 'GHA_SECURITY_CODE_SCAN_CREATE_ALERT_COMMENT='True >> $GITHUB_ENV
688+
fi
679689
fi
680690
681691
- name: "Print to job summary if critical alerts are found"

.github/workflows/docker-scan.yml

+10
Original file line numberDiff line numberDiff line change
@@ -251,17 +251,27 @@ jobs:
251251
env:
252252
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
253253
run: |
254+
set +e # Do not exit if a command fails
254255
alerts="$(gh api \
255256
--method GET \
256257
-H 'Accept: application/vnd.github+json' \
257258
-H 'X-GitHub-Api-Version: 2022-11-28' \
258259
/repos/${GITHUB_REPOSITORY}/code-scanning/alerts \
259260
-F severity='critical' -F state='open' -F ref='${{ github.ref }}' -F per_page='100' -F tool_name='Grype' --paginate)"
261+
262+
if [ $? != 0 ]; then
263+
echo "Failed to get alerts from Github. The previous upload vulnerability report step might have failed to be processed/uploaded. Try running the job again"
264+
echo '## Docker Scan - Failed to get alerts.
265+
Upload vulnerability report step failed to be processed/uploaded.
266+
Try running the job again' >> $GITHUB_STEP_SUMMARY
267+
exit 1
268+
else
260269
if [ "$alerts" == "[]" ]; then
261270
echo 'GHA_SECURITY_DOCKER_SCAN_CREATE_ALERT_COMMENT='False >> $GITHUB_ENV
262271
else
263272
echo 'GHA_SECURITY_DOCKER_SCAN_CREATE_ALERT_COMMENT='True >> $GITHUB_ENV
264273
fi
274+
fi
265275
- name: "Print to job summary if critical alerts are found"
266276
if: ${{ env.GHA_SECURITY_DOCKER_SCAN_CREATE_ALERT_COMMENT == 'True' }}
267277
env:

0 commit comments

Comments
 (0)