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+ # ##############################################################################
112name : " Docker Build"
213description : " Build and push Docker image to registry"
314
@@ -17,10 +28,12 @@ inputs:
1728 default : " ghcr.io"
1829 registry-username :
1930 description : " Registry username"
20- required : true
31+ required : false
32+ default : " "
2133 registry-password :
2234 description : " Registry password/token"
23- required : true
35+ required : false
36+ default : " "
2437 push :
2538 description : " Push to registry after build"
2639 required : false
@@ -29,6 +42,30 @@ inputs:
2942 description : " Run Trivy vulnerability scan"
3043 required : false
3144 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"
3269 cache :
3370 description : " Use GitHub Actions cache"
3471 required : false
5693 uses : docker/setup-buildx-action@v2
5794
5895 - name : Login to registry
96+ if : inputs.registry-username != '' && inputs.registry-password != ''
5997 uses : docker/login-action@v2
6098 with :
6199 registry : ${{ inputs.registry }}
@@ -78,27 +116,25 @@ runs:
78116 uses : docker/metadata-action@v4
79117 with :
80118 images : ${{ inputs.registry }}/${{ inputs.image-name }}
81- tags : |
82- type=ref,event=branch
83- type=sha,prefix={{branch}}-
84- type=semver,pattern={{version}}
85- type=raw,value=latest,enable={{is_default_branch}}
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}}' }}
86120
87121 - name : Build and push Docker image
88122 id : build
89123 uses : docker/build-push-action@v4
90124 with :
91125 context : ${{ inputs.context }}
92126 file : ${{ inputs.dockerfile }}
127+ platforms : ${{ inputs.platforms }}
93128 push : ${{ inputs.push }}
94129 tags : ${{ steps.meta.outputs.tags }}
95130 labels : ${{ steps.meta.outputs.labels }}
96- cache-from : ${{ inputs.cache == 'true' && 'type=gha' || '' }}
97- cache-to : ${{ inputs.cache == 'true' && format('type=gha,mode=max,key ={0}', steps .cache-key.outputs.key ) || '' }}
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 ) || '' }}
98133 build-args : |
99134 BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
100135 VCS_REF=${{ github.sha }}
101136 VERSION=${{ github.ref_name }}
137+ ${{ inputs.build-args }}
102138
103139 - name : Run Trivy vulnerability scan
104140 id : trivy
@@ -108,7 +144,8 @@ runs:
108144 image-ref : ${{ steps.meta.outputs.tags }}
109145 format : " sarif"
110146 output : " trivy-results.sarif"
111- severity : " CRITICAL,HIGH"
147+ severity : ${{ inputs.scan-severity }}
148+ exit-code : ${{ inputs.scan-exit-code }}
112149 continue-on-error : true
113150
114151 - name : Upload Trivy results
0 commit comments