用于 Trivy 的 GitHub Action
[![GitHub Release][release-img]][release] [![GitHub Marketplace][marketplace-img]][marketplace] [![License][license-img]][license]

name: build
on:
push:
branches:
- main
pull_request:
jobs:
build:
name: Build
runs-on: ubuntu-24.04
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Build an image from Dockerfile
run: docker build -t docker.io/my-organization/my-app:${{ github.sha }} .
- name: Run Trivy vulnerability scanner
uses: aquasecurity/[email protected]
with:
image-ref: 'docker.io/my-organization/my-app:${{ github.sha }}'
format: 'table'
exit-code: '1'
ignore-unfixed: true
vuln-type: 'os,library'
severity: 'CRITICAL,HIGH'
name: build
on:
push:
branches:
- main
pull_request:
jobs:
build:
name: Build
runs-on: ubuntu-24.04
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run Trivy vulnerability scanner in fs mode
uses: aquasecurity/[email protected]
with:
scan-type: 'fs'
scan-ref: '.'
trivy-config: trivy.yaml
在这种情况下,trivy.yaml 是一个作为仓库一部分检入的 YAML 配置文件。详细信息可在 Trivy 网站上获取,示例如下:
format: json
exit-code: 1
severity: CRITICAL
secret:
config: config/trivy/secret.yaml
可以在 trivy.yaml 文件中定义所有选项。通过 action 指定单个选项的做法仅为向后兼容而保留。以下选项必须定义,因为它们无法通过配置文件定义:
scan-ref:在使用 fs、repo 扫描时使用。image-ref:在使用 image 扫描时使用。scan-type:用于定义扫描类型,例如 image、fs、repo 等。Trivy 使用 Viper,它定义了选项的优先级顺序。顺序如下:
该 action 内置了缓存和恢复 漏洞数据库、Java 数据库 和 检查规则包 的功能(前提是它们在扫描期间被下载)。
缓存默认存储在 $GITHUB_WORKSPACE/.cache/trivy 目录中。
缓存在扫描开始前恢复,并在扫描结束后保存。
它在底层使用 actions/cache,但所需的配置设置更少。 缓存输入是可选的,默认开启缓存。
如果你想禁用缓存,请将 cache 输入设置为 false,但我们建议保持启用,以避免触发速率限制问题。
- name: Run Trivy scanner without cache
uses: aquasecurity/[email protected]
with:
scan-type: 'fs'
scan-ref: '.'
cache: 'false'
请注意,GitHub Actions 中分支之间的缓存访问存在限制。
默认情况下,工作流可以访问并恢复在当前分支或默认分支(通常是 main 或 master)中创建的缓存。
如果你需要在多个分支之间共享缓存,可能需要先在默认分支中创建缓存,然后在当前分支中恢复它。
为了优化工作流,你可以设置一个 cron 任务来定期更新默认分支中的缓存。 这样,后续扫描就可以直接使用缓存的数据库,而无需再次下载。
# Note: This workflow only updates the cache. You should create a separate workflow for your actual Trivy scans.
# In your scan workflow, set TRIVY_SKIP_DB_UPDATE=true and TRIVY_SKIP_JAVA_DB_UPDATE=true.
name: Update Trivy Cache
on:
schedule:
- cron: '0 0 * * *' # Run daily at midnight UTC
workflow_dispatch: # Allow manual triggering
jobs:
update-trivy-db:
runs-on: ubuntu-latest
steps:
- name: Setup oras
uses: oras-project/setup-oras@v1
- name: Get current date
id: date
run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
- name: Download and extract the vulnerability DB
run: |
mkdir -p $GITHUB_WORKSPACE/.cache/trivy/db
oras pull ghcr.io/aquasecurity/trivy-db:2
tar -xzf db.tar.gz -C $GITHUB_WORKSPACE/.cache/trivy/db
rm db.tar.gz
- name: Download and extract the Java DB
run: |
mkdir -p $GITHUB_WORKSPACE/.cache/trivy/java-db
oras pull ghcr.io/aquasecurity/trivy-java-db:1
tar -xzf javadb.tar.gz -C $GITHUB_WORKSPACE/.cache/trivy/java-db
rm javadb.tar.gz
- name: Cache DBs
uses: actions/cache/save@v4
with:
path: ${{ github.workspace }}/.cache/trivy
key: cache-trivy-${{ steps.date.outputs.date }}
在执行扫描时,请设置环境变量 TRIVY_SKIP_DB_UPDATE 和 TRIVY_SKIP_JAVA_DB_UPDATE 以跳过下载过程。
- name: Run Trivy scanner without downloading DBs
uses: aquasecurity/[email protected]
with:
scan-type: 'image'
scan-ref: 'myimage'
env:
TRIVY_SKIP_DB_UPDATE: true
TRIVY_SKIP_JAVA_DB_UPDATE: true
默认情况下,该 action 会在第一步调用 aquasecurity/setup-trivy,
以安装由 version 输入指定的 trivy 版本。如果你已通过其他方式安装了 trivy,
例如直接调用 aquasecurity/setup-trivy,或多次调用此 action,则可以使用
skip-setup-trivy 输入来禁用此步骤。
name: build
on:
push:
branches:
- main
pull_request:
jobs:
build:
name: Build
runs-on: ubuntu-24.04
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Manual Trivy Setup
uses: aquasecurity/[email protected]
with:
cache: true
version: v0.76.2
- name: Run Trivy vulnerability scanner in repo mode
uses: aquasecurity/[email protected]
with:
scan-type: 'fs'
ignore-unfixed: true
format: 'sarif'
output: 'trivy-results.sarif'
severity: 'CRITICAL'
skip-setup-trivy: true
另一个常见用例是当一次构建多次调用此 action 时,在这种情况下,我们可以在后续调用中将 skip-setup-trivy 设置为
true,例如
name: build
on:
push:
branches:
- main
pull_request:
jobs:
test:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Check out Git repository
uses: actions/checkout@v4
# The first call to the action will invoke setup-trivy and install trivy
- name: Generate Trivy Vulnerability Report
uses: aquasecurity/[email protected]
with:
scan-type: "fs"
output: trivy-report.json
format: json
scan-ref: .
exit-code: 0
- name: Upload Vulnerability Scan Results
uses: actions/upload-artifact@v4
with:
name: trivy-report
path: trivy-report.json
retention-days: 30
- name: Fail build on High/Criticial Vulnerabilities
uses: aquasecurity/[email protected]
with:
scan-type: "fs"
format: table
scan-ref: .
severity: HIGH,CRITICAL
ignore-unfixed: true
exit-code: 1
# On a subsequent call to the action we know trivy is already installed so can skip this
skip-setup-trivy: true
GitHub Enterprise Server (GHES) 会为 https://github.com 服务器使用无效的 github.token。
因此,你无法使用 setup-trivy action 安装 Trivy。