diff --git a/.github/workflows/bakery-build-pr.yml b/.github/workflows/bakery-build-pr.yml index 36f21a42..79f909b0 100644 --- a/.github/workflows/bakery-build-pr.yml +++ b/.github/workflows/bakery-build-pr.yml @@ -35,6 +35,21 @@ on: default: "exclude" required: false type: string + scan-image: + description: "Scan built images with Trivy [default: false]" + default: false + required: false + type: boolean + scan-fail-on-severity: + description: "Comma-separated severities that fail the scan if found (e.g. CRITICAL). Empty means findings never fail." + default: "" + required: false + type: string + trivy-version: + description: "Trivy release to install (e.g. v0.73.0). 'latest' disables binary caching in setup-trivy." + default: "v0.73.0" + required: false + type: string retry: description: "Number of times to retry a failed build" default: 1 @@ -309,6 +324,44 @@ jobs: --matrix-versions "$MATRIX_VERSIONS" \ --context "$BAKERY_CONTEXT" + - name: Setup trivy + if: ${{ inputs.scan-image }} + uses: aquasecurity/setup-trivy@81e514348e19b6112ce2a7e3ecbafe19c1e1f567 # v0.3.1 + with: + version: ${{ inputs.trivy-version }} + cache: true + + # After Test: only Test's `docker run` makes every target addressable + # locally. Scanning earlier resolves stale tags and silently scans the + # published image instead of the one just built. + - name: Trivy Scan + if: ${{ inputs.scan-image }} + continue-on-error: true + env: + IMAGE_NAME: ${{ matrix.img.image }} + IMAGE_VERSION: ${{ matrix.img.version }} + NORMALIZED_PLATFORM: ${{ steps.normalize-platform.outputs.platform }} + IMG_DEV: ${{ matrix.img.dev }} + MATRIX_VERSIONS: ${{ inputs.matrix-versions }} + BAKERY_CONTEXT: ${{ inputs.context }} + FAIL_ON_SEVERITY: ${{ inputs.scan-fail-on-severity }} + run: | + if [ "$IMG_DEV" = "true" ]; then + DEV_VERSIONS=only + else + DEV_VERSIONS=exclude + fi + FAIL_FLAGS=() + [[ -n "$FAIL_ON_SEVERITY" ]] && FAIL_FLAGS=(--fail-on-severity "$FAIL_ON_SEVERITY") + bakery trivy scan \ + --image-name "^${IMAGE_NAME}$" \ + --image-version "$IMAGE_VERSION" \ + --image-platform "$NORMALIZED_PLATFORM" \ + --dev-versions "$DEV_VERSIONS" \ + --matrix-versions "$MATRIX_VERSIONS" \ + "${FAIL_FLAGS[@]}" \ + --context "$BAKERY_CONTEXT" + build-test-result: name: Build/Test result needs: build-test