|
| 1 | +############################################################################### |
| 2 | +# composite action: docker-build |
| 3 | +# |
| 4 | +# Builds a Docker image, optionally pushes it to a registry, and runs a |
| 5 | +# Trivy vulnerability scan on the resulting image. |
| 6 | +# |
| 7 | +# Callers: staging.yml (build job), release.yml (build-production job) |
| 8 | +# Inputs: context, dockerfile, image-name, registry, tags, build-args, |
| 9 | +# scan, scan-severity, scan-exit-code, cache-scope, |
| 10 | +# registry-username, registry-password |
| 11 | +############################################################################### |
| 12 | +name: "Docker Build" |
| 13 | +description: "Build and push Docker image to registry" |
| 14 | + |
| 15 | +inputs: |
| 16 | + context: |
| 17 | + description: "Build context path" |
| 18 | + required: true |
| 19 | + dockerfile: |
| 20 | + description: "Dockerfile path" |
| 21 | + required: true |
| 22 | + image-name: |
| 23 | + description: "Image name (without registry)" |
| 24 | + required: true |
| 25 | + registry: |
| 26 | + description: "Container registry" |
| 27 | + required: false |
| 28 | + default: "ghcr.io" |
| 29 | + registry-username: |
| 30 | + description: "Registry username" |
| 31 | + required: false |
| 32 | + default: "" |
| 33 | + registry-password: |
| 34 | + description: "Registry password/token" |
| 35 | + required: false |
| 36 | + default: "" |
| 37 | + push: |
| 38 | + description: "Push to registry after build" |
| 39 | + required: false |
| 40 | + default: "true" |
| 41 | + scan: |
| 42 | + description: "Run Trivy vulnerability scan" |
| 43 | + required: false |
| 44 | + default: "true" |
| 45 | + scan-severity: |
| 46 | + description: "Trivy severities to include" |
| 47 | + required: false |
| 48 | + default: "CRITICAL,HIGH" |
| 49 | + scan-exit-code: |
| 50 | + description: "Trivy exit code on findings" |
| 51 | + required: false |
| 52 | + default: "0" |
| 53 | + tags: |
| 54 | + description: "docker/metadata-action tag rules" |
| 55 | + required: false |
| 56 | + default: "" |
| 57 | + build-args: |
| 58 | + description: "Additional Docker build args" |
| 59 | + required: false |
| 60 | + default: "" |
| 61 | + cache-scope: |
| 62 | + description: "GHA cache scope" |
| 63 | + required: false |
| 64 | + default: "" |
| 65 | + platforms: |
| 66 | + description: "Target platforms for image build" |
| 67 | + required: false |
| 68 | + default: "linux/amd64" |
| 69 | + cache: |
| 70 | + description: "Use GitHub Actions cache" |
| 71 | + required: false |
| 72 | + default: "true" |
| 73 | + cache-key-files: |
| 74 | + description: "Glob pattern for files to include in cache key (e.g., poetry.lock, package-lock.json, *.tf)" |
| 75 | + required: false |
| 76 | + default: "" |
| 77 | + |
| 78 | +outputs: |
| 79 | + image-uri: |
| 80 | + description: "Full image URI" |
| 81 | + value: ${{ steps.meta.outputs.tags }} |
| 82 | + image-digest: |
| 83 | + description: "Image digest" |
| 84 | + value: ${{ steps.build.outputs.digest }} |
| 85 | + vulnerabilities-found: |
| 86 | + description: "Whether vulnerabilities were found" |
| 87 | + value: ${{ steps.trivy.outputs.vulnerabilities-found }} |
| 88 | + |
| 89 | +runs: |
| 90 | + using: "composite" |
| 91 | + steps: |
| 92 | + - name: Set up Docker Buildx |
| 93 | + uses: docker/setup-buildx-action@v2 |
| 94 | + |
| 95 | + - name: Login to registry |
| 96 | + if: inputs.registry-username != '' && inputs.registry-password != '' |
| 97 | + uses: docker/login-action@v2 |
| 98 | + with: |
| 99 | + registry: ${{ inputs.registry }} |
| 100 | + username: ${{ inputs.registry-username }} |
| 101 | + password: ${{ inputs.registry-password }} |
| 102 | + |
| 103 | + - name: Generate dynamic cache key |
| 104 | + id: cache-key |
| 105 | + shell: bash |
| 106 | + run: | |
| 107 | + if [[ -n "${{ inputs.cache-key-files }}" ]]; then |
| 108 | + CACHE_KEY="docker-${{ inputs.image-name }}-${{ hashFiles(inputs.cache-key-files) }}" |
| 109 | + else |
| 110 | + CACHE_KEY="docker-${{ inputs.image-name }}-${{ github.sha }}" |
| 111 | + fi |
| 112 | + echo "key=${CACHE_KEY}" >> $GITHUB_OUTPUT |
| 113 | +
|
| 114 | + - name: Extract metadata |
| 115 | + id: meta |
| 116 | + uses: docker/metadata-action@v4 |
| 117 | + with: |
| 118 | + images: ${{ inputs.registry }}/${{ inputs.image-name }} |
| 119 | + tags: ${{ inputs.tags != '' && inputs.tags || 'type=ref,event=branch\ntype=sha,prefix={{branch}}-\ntype=semver,pattern={{version}}\ntype=raw,value=latest,enable={{is_default_branch}}' }} |
| 120 | + |
| 121 | + - name: Build and push Docker image |
| 122 | + id: build |
| 123 | + uses: docker/build-push-action@v4 |
| 124 | + with: |
| 125 | + context: ${{ inputs.context }} |
| 126 | + file: ${{ inputs.dockerfile }} |
| 127 | + platforms: ${{ inputs.platforms }} |
| 128 | + push: ${{ inputs.push }} |
| 129 | + tags: ${{ steps.meta.outputs.tags }} |
| 130 | + labels: ${{ steps.meta.outputs.labels }} |
| 131 | + cache-from: ${{ inputs.cache == 'true' && format('type=gha,scope={0}', inputs.cache-scope != '' && inputs.cache-scope || inputs.image-name) || '' }} |
| 132 | + cache-to: ${{ inputs.cache == 'true' && format('type=gha,mode=max,scope={0}', inputs.cache-scope != '' && inputs.cache-scope || inputs.image-name) || '' }} |
| 133 | + build-args: | |
| 134 | + BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') |
| 135 | + VCS_REF=${{ github.sha }} |
| 136 | + VERSION=${{ github.ref_name }} |
| 137 | + ${{ inputs.build-args }} |
| 138 | +
|
| 139 | + - name: Run Trivy vulnerability scan |
| 140 | + id: trivy |
| 141 | + if: inputs.scan == 'true' |
| 142 | + uses: aquasecurity/trivy-action@master |
| 143 | + with: |
| 144 | + image-ref: ${{ steps.meta.outputs.tags }} |
| 145 | + format: "sarif" |
| 146 | + output: "trivy-results.sarif" |
| 147 | + severity: ${{ inputs.scan-severity }} |
| 148 | + exit-code: ${{ inputs.scan-exit-code }} |
| 149 | + continue-on-error: true |
| 150 | + |
| 151 | + - name: Upload Trivy results |
| 152 | + if: inputs.scan == 'true' |
| 153 | + uses: github/codeql-action/upload-sarif@v2 |
| 154 | + with: |
| 155 | + sarif_file: "trivy-results.sarif" |
| 156 | + category: "trivy-docker-${{ inputs.image-name }}" |
| 157 | + continue-on-error: true |
| 158 | + |
| 159 | + - name: Report build completion |
| 160 | + shell: bash |
| 161 | + run: | |
| 162 | + echo "✓ Docker image built and pushed" |
| 163 | + echo " Image: ${{ steps.meta.outputs.tags }}" |
| 164 | + echo " Digest: ${{ steps.build.outputs.digest }}" |
0 commit comments