
TrivyをGitHub Actionとして実行し、Dockerコンテナイメージを脆弱性スキャンします。
GitHub Action のための 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'
### CIパイプラインのスキャン(Trivy Config付き)```yaml
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のウェブサイトで入手できますが、例は以下の通りです:```yaml
format: json
exit-code: 1
severity: CRITICAL
secret:
config: config/trivy/secret.yaml
すべてのオプションは `trivy.yaml` ファイルで定義することが可能です。アクションを介して個々のオプションを指定することは、後方互換性を保つために残されています。次に示すものは、設定ファイルでは定義できないため、指定が必要です:
- `scan-ref`: `fs, repo` スキャンを使用する場合。
- `image-ref`: `image` スキャンを使用する場合。
- `scan-type`: スキャンタイプを定義するため(例:`image`, `fs`, `repo` など)。
#### オプションの優先順位
Trivy は [Viper](https://github.com/spf13/viper) を使用しており、オプションの優先順位が定義されています。順序は次の通りです:
- GitHub Action フラグ
- 環境変数
- 設定ファイル
- デフォルト
### キャッシュ
このアクションには、スキャン中にダウンロードされた [脆弱性 DB](https://github.com/aquasecurity/trivy-db)、[Java DB](https://github.com/aquasecurity/trivy-java-db)、[チェックバンドル](https://github.com/aquasecurity/trivy-checks) をキャッシュして復元する機能が組み込まれています。
キャッシュはデフォルトで `$GITHUB_WORKSPACE/.cache/trivy` ディレクトリに保存されます。
キャッシュはスキャン開始前に復元され、スキャン終了後に保存されます。
内部では [actions/cache](https://github.com/actions/cache) を使用していますが、必要な設定は少なくなっています。
キャッシュ入力はオプションであり、デフォルトでキャッシュは有効になっています。
#### キャッシュの無効化
キャッシュを無効にする場合は、`cache` 入力を `false` に設定します。ただし、レート制限の問題を避けるため、有効にしておくことを推奨します。```yaml
- name: Run Trivy scanner without cache
uses: aquasecurity/[email protected]
with:
scan-type: 'fs'
scan-ref: '.'
cache: 'false'
GitHub Actions では、ブランチ間のキャッシュアクセスに関する制限があることに注意してください。
デフォルトでは、ワークフローは現在のブランチまたはデフォルトブランチ(通常は main または master)のいずれかで作成されたキャッシュにアクセスして復元できます。
ブランチ間でキャッシュを共有する必要がある場合は、デフォルトブランチにキャッシュを作成し、現在のブランチで復元する必要があるかもしれません。
ワークフローを最適化するために、cronジョブを設定してデフォルトブランチのキャッシュを定期的に更新することができます。 これにより、後続のスキャンでキャッシュされたDBを再度ダウンロードせずに使用できるようになります。```yaml
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` を設定してください。```yaml
- 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
デフォルトでは、アクションは最初のステップとしてaquasecurity/setup-trivyを呼び出し、version入力で指定されたtrivyバージョンをインストールします。既に別の方法でtrivyをインストールしている場合(例えば、直接aquasecurity/setup-trivyを呼び出した場合)、またはこのアクションを複数回呼び出す場合は、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.72.0
- 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
#### 複数回Trivy Actionを呼び出す際のセットアップをスキップする
もう一つの一般的なユースケースは、ビルドがこのアクションを複数回呼び出す場合です。その場合、以降の呼び出しでは `skip-setup-trivy` を `true` に設定できます。例:```yaml
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 アクションを使用して Trivy をインストールすることはできません。この問題を解決するには、token-setup-trivy 入力を使用して setup-trivy のトークンを上書きする必要があります:```yaml
- name: Run Trivy scanner without cache
uses: aquasecurity/[email protected]
with:
scan-type: 'fs'
scan-ref: '.'
token-setup-trivy: ${{ secrets.GITHUB_PAT }}
GitHubには、同様のケース向けに [create-github-app-token](https://github.com/actions/create-github-app-token) も用意されています。
### ターボールのスキャン```yaml
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: Generate tarball from image
run: |
docker pull <your-docker-image>
docker save -o vuln-image.tar <your-docker-image>
- name: Run Trivy vulnerability scanner in tarball mode
uses: aquasecurity/[email protected]
with:
input: /github/workspace/vuln-image.tar
severity: 'CRITICAL,HIGH'
このアクションは [Trivy テンプレート][trivy-templates] をサポートしています。
テンプレートファイルへのパスを指定するには template 入力を使用します(パスの前に @ を付けるのを忘れないでください)。```yaml
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
uses: aquasecurity/[email protected]
with:
scan-type: "fs"
scan-ref: .
format: 'template'
template: "@path/to/my_template.tpl"
#### デフォルトテンプレート
Trivy には[デフォルトテンプレート][trivy-default-templates]があります。
デフォルトでは、`setup-trivy` はそれらを `$HOME/.local/bin/trivy-bin/contrib` ディレクトリにインストールします。```yaml
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
uses: aquasecurity/[email protected]
with:
scan-type: "fs"
scan-ref: .
format: 'template'
template: "@$HOME/.local/bin/trivy-bin/contrib/html.tpl"
もし GitHub code scanning が利用可能であれば、Trivyをスキャンツールとして次のように使用できます。```yaml name: build on: push: branches: - main pull_request: jobs: build: name: Build runs-on: ubuntu-24.04 permissions: contents: read # Required to checkout and read repo files security-events: write # Required to upload SARIF files to Security tab 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: 'sarif'
output: 'trivy-results.sarif'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v4
with:
sarif_file: 'trivy-results.sarif'
あなたは、Kitploit のための専門的なサイバーセキュリティツールコンテンツ翻訳者です。あなたの唯一の役割は、オープンソースセキュリティツールのディレクトリである Kitploit のために、Markdown コンテンツをある言語から別の言語に正確で慣用的かつ簡潔に翻訳することです。
重要なルール:
- 自然言語のテキストのみを翻訳してください。コード、ターミナルコマンド、ファイルパス、URL、パッケージ名、CVE ID、環境変数名、識別子は絶対に翻訳しないでください。
- すべての Markdown 構造 (見出し、リスト、入れ子リスト、表(配置を含む)、リンク、画像、インラインコード、コードフェンス、引用ブロック、HTMLブロック、水平線) を完全に保持してください。
- 正確な書式 (空白、改行、インデント、表の列数と配置マーカー) を保持してください。
- バッジ、シールド、ステータス画像はそのまま正確に保持してください。
- コードの動作を変更する可能性のある句読点を追加、削除、変更しないでください。
- 翻訳されたテキストのみを返してください。前置き、「ここに翻訳があります」、「もちろんです!」、説明、コメント、メタテキストは一切付けないでください。
- 応答を Markdown コードブロックで囲まないでください。
- JSON、YAML、XML、配列、オブジェクト、キー/バリューラッパー、スキーマ、または "translated"、"language"、"markdown"、"content"、"result" のようなフィールドを出力しないでください。
- 応答は Markdown/プレーンテキストコンテンツのみでなければなりません。ソースにコードフェンス内の JSON/YAML/XML の例が含まれている場合、それらの例は Markdown の一部としてそのまま保持し、応答全体を構造化オブジェクトにしないでください。
- 質問をしないでください。会話をしないでください。
- 技術用語を安全に翻訳できない場合は、壊すリスクを冒さずに翻訳しないでください。
以下の Kitploit ツールコンテンツを翻訳してください。
これは、より長い Markdown ドキュメントを順番に翻訳する際の 51 分の 27 番目のチャンクです。
ソース言語は en です。
ターゲット言語: ja。
コンテンツタイプ: README チャンク 27/51。
チャンク固有のルール:
1. 自然言語のテキストのみを翻訳してください。コードブロック、シェルコマンド、ファイルパス、URL、パッケージ名、技術識別子、CVE ID、環境変数名は絶対に翻訳しないでください。
2. すべての Markdown 構文をそのまま正確に保持してください。
3. 「## Chunk N」、「## Part N」、「## Continued from...」、「## Translation of chunk...」のような導入見出しを追加しないでください。「End of chunk N」や「Content continues...」というマーカーを追加しないでください。
4. 省略を示す「...」の省略記号を追加しないでください。提供されたテキストのみを文字通り構造的に翻訳してください。
5. チャンクの境界は意図的です。チャンクを継ぎ目なく連結できるように構造を保持してください。
6. 翻訳されたテキストのみを返してください。前置きなし、コメントなし、コードブロックで囲まない、JSON/YAML/XML なし、配列なし、オブジェクトなし、スキーマなし、キー/バリューラッパーなし。
7. チャンクが段落の途中から始まる場合は、その時点から翻訳を続けてください。ソースに存在しない限り、先頭に改行やインデントを追加しないでください。
入力:
You can find a more in-depth example here: https://github.com/aquasecurity/trivy-sarif-demo/blob/master/.github/workflows/scan.yml
If you would like to upload SARIF results to GitHub Code scanning even upon a non zero exit code from Trivy Scan, you can add the following to your upload step:```yaml
name: build
on:
push:
branches:
- main
pull_request:
jobs:
build:
name: Build
runs-on: ubuntu-24.04
permissions:
contents: read # Required to checkout and read repo files
security-events: write # Required to upload SARIF files to Security tab
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: 'sarif'
output: 'trivy-results.sarif'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v4
if: always()
with:
sarif_file: 'trivy-results.sarif'
詳細はこちら:https://docs.github.com/en/actions/learn-github-actions/expressions#always
Trivyの組み込みのリポジトリスキャンを使用して、gitリポジトリをスキャンすることも可能です。これは、リポジトリで開かれる各PRに対してビルド時のチェックとしてTrivyを実行したい場合に便利です。これにより、各PRで導入される可能性のある潜在的な脆弱性を特定するのに役立ちます。
利用可能な GitHub code scanning がある場合、Trivyをスキャンツールとして次のように使用できます:```yaml name: build on: push: branches: - main pull_request: jobs: build: name: Build runs-on: ubuntu-24.04 permissions: contents: read # Required to checkout and read repo files security-events: write # Required to upload SARIF files to Security tab steps: - name: Checkout code uses: actions/checkout@v4
- 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'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v4
with:
sarif_file: 'trivy-results.sarif'
### Trivyを使用してrootfsディレクトリをスキャンする
また、Trivyに組み込まれているrootfsスキャンを使用して、ルートファイルシステムのディレクトリをスキャンすることも可能です。これは、リポジトリに開かれる各PRに対してビルド時のチェックとしてTrivyを実行したい場合に便利です。これにより、各PRで導入される可能性のある潜在的な脆弱性を特定するのに役立ちます。
[GitHub code scanning](https://docs.github.com/en/github/finding-security-vulnerabilities-and-errors-in-your-code/about-code-scanning)が利用可能な場合、以下のようにTrivyをスキャンツールとして使用できます。```yaml
name: build
on:
push:
branches:
- main
pull_request:
jobs:
build:
name: Build
runs-on: ubuntu-24.04
permissions:
contents: read # Required to checkout and read repo files
security-events: write # Required to upload SARIF files to Security tab
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run Trivy vulnerability scanner with rootfs command
uses: aquasecurity/[email protected]
with:
scan-type: 'rootfs'
scan-ref: 'rootfs-example-binary'
ignore-unfixed: true
format: 'sarif'
output: 'trivy-results.sarif'
severity: 'CRITICAL'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v4
with:
sarif_file: 'trivy-results.sarif'
Trivyの組み込みリポジトリスキャンを使用してIaCリポジトリをスキャンすることも可能です。 これは、リポジトリで開かれる各PRに対してビルド時チェックとしてTrivyを実行したい場合に便利です。 これにより、各PRで導入される可能性のある脆弱性を特定できます。
GitHub code scanning が利用可能な場合、Trivyをスキャンツールとして次のように使用できます:```yaml name: build on: push: branches: - main pull_request: jobs: build: name: Build runs-on: ubuntu-24.04 permissions: contents: read # Required to checkout and read repo files security-events: write # Required to upload SARIF files to Security tab steps: - name: Checkout code uses: actions/checkout@v4
- name: Run Trivy vulnerability scanner in IaC mode
uses: aquasecurity/[email protected]
with:
scan-type: 'config'
hide-progress: true
format: 'sarif'
output: 'trivy-results.sarif'
exit-code: '1'
severity: 'CRITICAL,HIGH'
- name: Upload Trivy scan results to GitHub Security tab
if: always()
uses: github/codeql-action/upload-sarif@v4
with:
sarif_file: 'trivy-results.sarif'
**注記**: Terraform 設定にプライベートモジュールが含まれている場合は、それらをホストしているリポジトリで認証するように Git を設定してください。これは、CI ワークフローにアクセスを設定するステップを追加することで実現できます。例えば、Personal Access Token (PAT) や SSH キーを使用します:```yaml
- name: Configure Git for private modules
run: |
git config --global url."https://$GITHUB_USER:[email protected]/".insteadOf "https://github.com/"
env:
GITHUB_USER: ${{ github.actor }}
PRIVATE_REPO_TOKEN: ${{ secrets.PRIVATE_REPO_TOKEN }}
これにより、Trivyはプライベートモジュールをダウンロードできるようになります。
Trivyを使用して、依存関係のSBOMを生成し、GitHub Dependency Graphなどのコンシューマに送信することが可能です。
SBOMをGitHubに送信機能は、現在GitHub Dependency Graphがリポジトリで有効になっている場合にのみ利用可能です。
GITHUB_TOKENとしても知られています)を使用する必要があります:```yamlname: Generate SBOM on: push: branches: - main
permissions: contents: write
jobs: generate-sbom: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4
- name: Run Trivy in GitHub SBOM mode and submit results to Dependency Graph
uses: aquasecurity/[email protected]
with:
scan-type: 'fs'
format: 'github'
output: 'dependency-results.sbom.json'
scan-ref: '.'
github-pat: ${{ secrets.GITHUB_TOKEN }} # or ${{ secrets.github_pat_name }} if you're using a PAT
画像をスキャンする際、実際の出力JSONを解析したい場合があります。例えば、Github Dependencyでは各依存関係のファイルパスなどの詳細がすべて表示されないためです。
レポートをアーティファクトとしてアップロードし、ダウンロードすることができます。例えば、[upload-artifact action](https://github.com/actions/upload-artifact)を使用します。```yaml
---
name: Generate SBOM
on:
push:
branches:
- main
## GITHUB_TOKEN authentication, add only if you're not going to use a PAT
permissions:
contents: write
jobs:
generate-sbom:
runs-on: ubuntu-latest
steps:
- name: Scan image in a private registry
uses: aquasecurity/[email protected]
with:
image-ref: "private_image_registry/image_name:image_tag"
scan-type: image
format: 'github'
output: 'dependency-results.sbom.json'
github-pat: ${{ secrets.GITHUB_TOKEN }} # or ${{ secrets.github_pat_name }} if you're using a PAT
severity: "MEDIUM,HIGH,CRITICAL"
scanners: "vuln"
env:
TRIVY_USERNAME: "image_registry_admin_username"
TRIVY_PASSWORD: "image_registry_admin_password"
- name: Upload trivy report as a Github artifact
uses: actions/upload-artifact@v4
with:
name: trivy-sbom-report
path: '${{ github.workspace }}/dependency-results.sbom.json'
retention-days: 20 # 90 is the default
Trivyの組み込みイメージスキャンを使用して、プライベートレジストリをスキャンすることも可能です。必要なのはENV変数を設定することだけです。
Docker HubではTRIVY_USERNAMEとTRIVY_PASSWORDが必要です。
公開リポジトリからダウンロードする場合はENV変数を設定する必要はありません。```yaml
name: build
on:
push:
branches:
- main
pull_request:
jobs:
build:
name: Build
runs-on: ubuntu-24.04
permissions:
contents: read # Required to checkout and read repo files
security-events: write # Required to upload SARIF results to the GitHub Security tab
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run Trivy vulnerability scanner
uses: aquasecurity/[email protected]
with:
image-ref: 'docker.io/my-organization/my-app:${{ github.sha }}'
format: 'sarif'
output: 'trivy-results.sarif'
env:
TRIVY_USERNAME: Username
TRIVY_PASSWORD: Password
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v4
with:
sarif_file: 'trivy-results.sarif'
#### AWS ECR(Elastic Container Registry)
TrivyはAWS SDKを使用します。`aws` CLIツールをインストールする必要はありません。
[AWS CLIの環境変数][env-var]を使用できます。
[env-var]: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html%60%60%60yaml
name: build
on:
push:
branches:
- main
pull_request:
jobs:
build:
name: Build
runs-on: ubuntu-24.04
permissions:
contents: read # Required to checkout and read repo files
security-events: write # Required to upload SARIF files to Security tab
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run Trivy vulnerability scanner
uses: aquasecurity/[email protected]
with:
image-ref: 'aws_account_id.dkr.ecr.region.amazonaws.com/imageName:${{ github.sha }}'
format: 'sarif'
output: 'trivy-results.sarif'
env:
AWS_ACCESS_KEY_ID: key_id
AWS_SECRET_ACCESS_KEY: access_key
AWS_DEFAULT_REGION: us-west-2
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v4
with:
sarif_file: 'trivy-results.sarif'
Trivy は Google Cloud SDK を使用します。gcloud コマンドをインストールする必要はありません。
ターゲットプロジェクトのリポジトリを使用したい場合は、GOOGLE_APPLICATION_CREDENTIALS で設定できます。```yaml
name: build
on:
push:
branches:
- main
pull_request:
jobs:
build:
name: Build
runs-on: ubuntu-24.04
permissions:
contents: read # Required to checkout and read repo files
security-events: write # Required to upload SARIF files to Security tab
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run Trivy vulnerability scanner
uses: aquasecurity/[email protected]
with:
image-ref: 'docker.io/my-organization/my-app:${{ github.sha }}'
format: 'sarif'
output: 'trivy-results.sarif'
env:
GOOGLE_APPLICATION_CREDENTIALS: /path/to/credential.json
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v4
with:
sarif_file: 'trivy-results.sarif'
#### セルフホステッド
BasicAuthサーバーには`TRIVY_USERNAME`と`TRIVY_PASSWORD`が必要です。
ポート80を使用する場合は、NonSSL `TRIVY_NON_SSL=true` を使用してください。```yaml
name: build
on:
push:
branches:
- main
pull_request:
jobs:
build:
name: Build
runs-on: ubuntu-24.04
permissions:
contents: read # Required to checkout and read repo files
security-events: write # Required to upload SARIF files to Security tab
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run Trivy vulnerability scanner
uses: aquasecurity/[email protected]
with:
image-ref: 'docker.io/my-organization/my-app:${{ github.sha }}'
format: 'sarif'
output: 'trivy-results.sarif'
env:
TRIVY_USERNAME: Username
TRIVY_PASSWORD: Password
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v4
with:
sarif_file: 'trivy-results.sarif'
ワークフローサマリーでスキャン結果を参照することも可能です。
この手順は、GitHub Advanced Security ライセンスを持たないプライベートリポジトリに特に有用です。```yaml
name: Run Trivy scanner uses: aquasecurity/[email protected] with: scan-type: config hide-progress: true output: trivy.txt
name: Publish Trivy Output to Summary run: | if [[ -s trivy.txt ]]; then { echo "### Security Output" echo "
terraform' cat trivy.txt echo ''
echo "## カスタマイズ
設定の優先順位:
- [入力値](#inputs)
- [環境変数](#environment-variables)
- [Trivy設定ファイル](#trivy-config-file)
- デフォルト値
### 入力値
以下の入力値を`step.with`キーとして使用できます:
| 名前 | 型 | デフォルト | 説明 |
|------------------------------|---------|------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `scan-type` | String | `image` | スキャンタイプ(例: `image` または `fs`) |
| `input` | String | | Tar参照(例: `alpine-latest.tar`) |
| `image-ref` | String | | イメージ参照(例: `alpine:3.10.2`) |
| `scan-ref` | String | `/github/workspace/` | スキャン参照(例: `/github/workspace/` または `.`) |
| `format` | String | `table` | 出力形式(`table`, `json`, `template`, `sarif`, `cyclonedx`, `spdx`, `spdx-json`, `github`, `cosign-vuln`) |
| `template` | String | | 出力テンプレート(`@$HOME/.local/bin/trivy-bin/contrib/gitlab.tpl`, `@$HOME/.local/bin/trivy-bin/contrib/junit.tpl`) |
| `tf-vars` | String | | Terraform変数ファイルへのパス |
| `output` | String | | 結果をファイルに保存 |
| `exit-code` | String | `0` | 指定された脆弱性が見つかった場合の終了コード |
| `ignore-unfixed` | Boolean | false | 未パッチ/未修正の脆弱性を無視 |
| `vuln-type` | String | `os,library` | 脆弱性タイプ(os,library) |
| `severity` | String | `UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL` | スキャンおよび表示する脆弱性の重大度 |
| `skip-dirs` | String | | トラバーサルをスキップするディレクトリのカンマ区切りリスト |
| `skip-files` | String | | トラバーサルをスキップするファイルのカンマ区切りリスト |
| `cache-dir` | String | `$GITHUB_WORKSPACE/.cache/trivy` | キャッシュディレクトリ。注: この値は`trivy.yaml`では設定できません。 |
| `timeout` | String | `5m0s` | スキャンのタイムアウト時間 |
| `ignore-policy` | String | | OPA Rego言語で脆弱性をフィルタリング |
| `hide-progress` | String | `false` | プログレスバーとログ出力を抑制 |
| `list-all-pkgs` | String | | 脆弱性に関係なくすべてのパッケージを出力 |
| `scanners` | String | `vuln,secret` | 検出するセキュリティ問題のカンマ区切りリスト(`vuln`,`secret`,`misconfig`,`license`) |
| `trivyignores` | String | | リポジトリ内の1つ以上の`.trivyignore`ファイルへの相対パス、または単一の`.trivyignore.yaml`ファイルへの相対パスのカンマ区切りリスト。 |
| `trivy-config` | String | | trivy.yaml設定へのパス |
| `github-pat` | String | | SBOMスキャン結果をGitHub依存関係グラフに送信するための認証トークン。GitHub Personal Access Token(PAT)またはGITHUB_TOKENのいずれか |
| `limit-severities-for-sarif` | Boolean | false | デフォルトでは*SARIF*形式は設定された重大度に関係なくすべての脆弱性を出力します。この動作を上書きするには、このパラメータを**true**に設定します |
| `docker-host` | String | | デフォルトでは`unix://var/run/docker.sock`に設定されていますが、コンテナ化されたインフラストラクチャの値を指定するために更新できます(`unix:/`または他のプレフィックスが必要です) |
| `version` | String | `v0.72.0` | 使用するTrivyバージョン(例: `latest` または `v0.72.0`) |
| `skip-setup-trivy` | Boolean | false | `setup-trivy`アクションの呼び出しをスキップして`trivy`をインストールしない |
| `token-setup-trivy` | Boolean | | `setup-trivy`が`trivy`リポジトリをチェックアウトするために使用する`github.token`を上書き |
### 環境変数
[Trivy環境変数][trivy-env]を使用して、必要なオプションを設定できます([入力値](#inputs)でサポートされていないフラグ(`--secret-config`など)も含みます)。
**注意** 一部の古いバージョンのActionでは、あるAction呼び出しの入力値が後続のAction呼び出しに漏洩するバグがありました。これにより、複数回Actionを呼び出すワークフロー(複数のスキャンを実行する、または異なる出力形式で同じスキャンを実行するなど)が期待どおりの出力を生成しない可能性があります。該当するかどうかは、GitHub Actionsのステップ情報を確認してください。Actions出力に表示される`env`セクションに、明示的に設定していない`TRIVY_*`環境変数が含まれている場合、このバグの影響を受けており、最新のActionバージョンにアップグレードする必要があります。
### Trivy設定ファイル
`trivy-config` [入力値](#inputs)を使用する場合、[Trivy設定ファイル][trivy-config]を使用してオプションを設定できます([入力値](#inputs)でサポートされていないフラグ(`--secret-config`など)も含みます)。
[release]: https://github.com/aquasecurity/trivy-action/releases/latest
[release-img]: https://img.shields.io/github/release/aquasecurity/trivy-action.svg?logo=github
[marketplace]: https://github.com/marketplace/actions/aqua-security-trivy
[marketplace-img]: https://img.shields.io/badge/marketplace-trivy--action-blue?logo=github
[license]: https://raw.githubusercontent.com/aquasecurity/trivy-action/master/LICENSE
[license-img]: https://img.shields.io/github/license/aquasecurity/trivy-action
[trivy-env]: https://aquasecurity.github.io/trivy/latest/docs/configuration/#environment-variables
[trivy-config]: https://aquasecurity.github.io/trivy/latest/docs/references/configuration/config-file/
[trivy-templates]: https://aquasecurity.github.io/trivy/latest/docs/configuration/reporting/#template
[trivy-default-templates]: https://aquasecurity.github.io/trivy/latest/docs/configuration/reporting/#template