name: Docker Build & Scan on: push: branches: [main] tags: ["v*"] pull_request: branches: [main] permissions: contents: read packages: write security-events: write env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }} jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Lowercase image name id: img run: echo "name=${GITHUB_REPOSITORY,,}" >> "$GITHUB_OUTPUT" - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Log in to GHCR if: github.event_name != 'pull_request' uses: docker/login-action@v4 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Extract metadata id: meta uses: docker/metadata-action@v6 with: images: ${{ env.REGISTRY }}/${{ steps.img.outputs.name }} tags: | type=ref,event=branch type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} type=sha,prefix=sha- - name: Build image uses: docker/build-push-action@v5 with: context: . push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} cache-from: type=gha cache-to: type=gha,mode=max load: ${{ github.event_name == 'pull_request' }} - name: Trivy scan if: github.event_name != 'pull_request' uses: aquasecurity/trivy-action@master with: image-ref: ${{ env.REGISTRY }}/${{ steps.img.outputs.name }}:sha-${{ github.sha }} format: sarif output: trivy-results.sarif severity: CRITICAL,HIGH exit-code: "0" continue-on-error: true - name: Upload Trivy results if: github.event_name != 'pull_request' && hashFiles('trivy-results.sarif') != '' uses: github/codeql-action/upload-sarif@v3 with: sarif_file: trivy-results.sarif category: trivy - name: Generate SBOM if: github.event_name != 'pull_request' uses: anchore/sbom-action@v0 with: image: ${{ env.REGISTRY }}/${{ steps.img.outputs.name }}:sha-${{ github.sha }} format: spdx-json output-file: sbom.spdx.json continue-on-error: true - name: Upload SBOM artifact if: always() uses: actions/upload-artifact@v7 with: name: sbom path: sbom.spdx.json