
Esegue Trivy come GitHub Action per scansionare l'immagine del tuo container Docker alla ricerca di vulnerabilità.
GitHub Action per Trivy
[![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
In questo caso trivy.yaml è una configurazione YAML che viene inclusa nel repository. Informazioni dettagliate sono disponibili sul sito web di Trivy, ma un esempio è il seguente:
format: json
exit-code: 1
severity: CRITICAL
secret:
config: config/trivy/secret.yaml
È possibile definire tutte le opzioni nel file trivy.yaml. L'indicazione di singole opzioni tramite l'action è mantenuta per motivi di retrocompatibilità. È necessario definire quanto segue poiché non può essere definito tramite il file di configurazione:
scan-ref: se si utilizzano scansioni fs, repo.image-ref: se si utilizza la scansione image.scan-type: per definire il tipo di scansione, ad es. image, fs, repo, ecc.Trivy utilizza Viper, che ha un ordine di precedenza definito per le opzioni. L'ordine è il seguente:
L'action dispone di una funzionalità integrata per la cache e il ripristino di the vulnerability DB, the Java DB e the checks bundle se vengono scaricati durante la scansione.
La cache è memorizzata per impostazione predefinita nella directory $GITHUB_WORKSPACE/.cache/trivy.
La cache viene ripristinata prima dell'inizio della scansione e salvata al termine della scansione.
Utilizza actions/cache internamente ma richiede meno impostazioni di configurazione. L'input della cache è opzionale e la cache è attivata per impostazione predefinita.
Se desideri disattivare la cache, imposta l'input cache su false, ma consigliamo di mantenerla attiva per evitare problemi di rate limiting.
- name: Run Trivy scanner without cache
uses: aquasecurity/[email protected]
with:
scan-type: 'fs'
scan-ref: '.'
cache: 'false'
Tieni presente che esistono restrizioni sull'accesso alla cache tra i branch in GitHub Actions.
Per impostazione predefinita, un workflow può accedere e ripristinare una cache creata nel branch corrente o nel branch predefinito (solitamente main o master).
Se devi condividere le cache tra i branch, potrebbe essere necessario creare una cache nel branch predefinito e ripristinarla nel branch corrente.
Per ottimizzare il tuo workflow, puoi impostare un cron job per aggiornare regolarmente la cache nel branch predefinito. Ciò consente alle scansioni successive di utilizzare il DB in cache senza scaricarlo nuovamente.
# 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 }}
Quando si esegue una scansione, impostare le variabili d'ambiente TRIVY_SKIP_DB_UPDATE e TRIVY_SKIP_JAVA_DB_UPDATE per saltare il processo di download.
- 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