Trivy Container Scan #6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Trivy Container Scan | |
permissions: | |
contents: read | |
on: | |
workflow_dispatch: | |
schedule: | |
- cron: "0 6 * * 1" # At 06:00 on Monday. | |
jobs: | |
CVE-scan: | |
runs-on: ubuntu-latest | |
environment: dev | |
permissions: | |
issues: write | |
security-events: write # for github/codeql-action/upload-sarif to upload SARIF results | |
steps: | |
- name: Checkout the code | |
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 #v4.1.1 | |
- name: Build the Docker image | |
run: "docker build . --tag localbuild/testimage:latest" | |
- name: Trivy scan | |
id: scan | |
uses: aquasecurity/trivy-action@6e7b7d1fd3e4fef0c5fa8cce1229c54b2c9bd0d8 # v0.16.1 | |
with: | |
image-ref: "localbuild/testimage:latest" | |
format: "sarif" | |
output: "results.sarif" | |
env: | |
ACTIONS_RUNTIME_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Upload Trivy scan results to GitHub Security tab | |
uses: github/codeql-action/upload-sarif@v3 | |
with: | |
sarif_file: "results.sarif" | |
- name: CVE Description escaped extraction and print | |
run: | | |
SCAN_RESULTS=$(jq -r '.runs[0].tool.driver.rules | map(.help.text) | join("\\n")' results.sarif) | |
echo "CVE_CRITICAL=$(echo $SCAN_RESULTS | grep -o CRITICAL | wc -l)" >> $GITHUB_ENV | |
echo "CVE_HIGH=$(echo $SCAN_RESULTS | grep -o HIGH | wc -l)" >> $GITHUB_ENV | |
echo "CVE_MEDIUM=$(echo $SCAN_RESULTS | grep -o MEDIUM | wc -l)" >> $GITHUB_ENV | |
echo $SCAN_RESULTS | |
- name: Fails if CVE HIGH or CRITICAL are detected | |
id: cve-threshold | |
if: env.CVE_HIGH > 0 || env.CVE_CRITICAL > 0 | |
run: exit 1 | |
- name: Send notification to Slack | |
id: slack | |
if: always() && github.event_name == 'schedule' && steps.cve-threshold.outcome == 'failure' | |
uses: slackapi/slack-github-action@e28cf165c92ffef168d23c5c9000cffc8a25e117 #v1.24.0 | |
with: | |
payload: | | |
{ | |
"blocks": [ | |
{ | |
"type": "header", | |
"text": { | |
"type": "plain_text", | |
"text": "[ ${{ github.event.repository.name }} ]" | |
} | |
}, | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": " `CRITICAL` : *${{ env.CVE_CRITICAL }}*\n\n`HIGH` : *${{ env.CVE_HIGH }}*\n\n`MEDIUM` : *${{ env.CVE_MEDIUM }}*\n\n<https://github.com/${{ github.repository }}/security/code-scanning |See details on GitHub>" | |
} | |
} | |
] | |
} | |
env: | |
SLACK_WEBHOOK_URL: ${{ secrets.CVE_SCAN_SLACK_WEBHOOK }} # You will need to populate this value from the slack applications window. | |
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK |