Skip to content

Commit 5b13a83

Browse files
committed
ci(gha): add trivy container image scan
1 parent ab93a63 commit 5b13a83

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

.github/workflows/pipeline.yml

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,16 @@ jobs:
154154
with:
155155
github-token: ${{ secrets.GITHUB_TOKEN }}
156156
- name: Enable auto-merge for Dependabot PRs
157-
# if: contains(steps.metadata.outputs.dependency-names, 'my-dependency') && steps.metadata.outputs.update-type == 'version-update:semver-patch'
157+
if: contains(steps.metadata.outputs.dependency-names, 'my-dependency') && steps.metadata.outputs.update-type == 'version-update:semver-patch'
158158
run: gh pr merge --auto --merge "$PR_URL"
159159
env:
160160
PR_URL: ${{github.event.pull_request.html_url}}
161161
GH_TOKEN: ${{secrets.GITHUB_TOKEN}}
162-
docker-build-push:
162+
docker-build-scan-push:
163163
if: github.ref == 'refs/heads/main'
164164
runs-on: ubuntu-latest
165+
env:
166+
BASE_IMAGE: abhisheksr01/companieshouse
165167
needs:
166168
- unit-test
167169
- mutation-test
@@ -182,13 +184,13 @@ jobs:
182184
with:
183185
dry-run: true # Since we are setting dryrun argument the bump-version will always be available until 'current-version' is pushed as release
184186
- name: check-bump-version-output
187+
shell: bash
185188
run: |
186189
echo "previous-version: ${{ steps.bump-version.outputs.previous-version }}"
187190
echo "bump-version: ${{ steps.bump-version.outputs.bump-version }}"
188191
echo "current-version: ${{ steps.bump-version.outputs.current-version }}"
189192
echo "is-version-bumped: ${{ steps.bump-version.outputs.is-version-bumped }}"
190193
echo "is-dryrun-version-bumped: ${{ steps.bump-version.outputs.is-dryrun-version-bumped }}"
191-
shell: bash
192194
- name: Login to Docker Hub
193195
uses: docker/login-action@v3
194196
with:
@@ -201,7 +203,7 @@ jobs:
201203
id: meta
202204
uses: docker/metadata-action@v5
203205
with:
204-
images: abhisheksr01/companieshouse
206+
images: ${{ env.BASE_IMAGE }}
205207
context: git
206208
tags: |
207209
type=ref,event=pr
@@ -212,25 +214,56 @@ jobs:
212214
"org.opencontainers.image.url": "https://github.com/abhisheksr01/spring-boot-microservice-best-practices",
213215
"org.opencontainers.image.source": "https://github.com/abhisheksr01/spring-boot-microservice-best-practices",
214216
"org.opencontainers.image.version": ${{ steps.bump-version.outputs.bump-version }},
215-
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
217+
"org.opencontainers.image.created": "$(date +"%Y%m%d%H%M%S")",
216218
"org.opencontainers.image.revision": ${{ github.sha }},
217219
"org.opencontainers.image.licenses": "MIT"
218-
- name: Build and push
220+
- name: Build Image
219221
if: ${{ steps.bump-version.outputs.is-dryrun-version-bumped == 'true' }}
220222
uses: docker/build-push-action@v6
221223
with:
222-
push: ${{ github.event_name != 'pull_request' && steps.bump-version.outputs.is-dryrun-version-bumped == 'true' }} # Only push on main branch & when version is bumped with dryrun. We will create tags and creates separately after proper testing
224+
load: true
225+
tags: ${{ steps.meta.outputs.tags }}
226+
labels: ${{ steps.meta.outputs.labels }}
227+
cache-to: type=registry,ref=${{ env.BASE_IMAGE }}:cache
228+
cache-from: type=registry,ref=${{ env.BASE_IMAGE }}:cache,mode=max
229+
- name: Scan Image
230+
uses: aquasecurity/[email protected]
231+
with:
232+
versin: 0.66.0
233+
image-ref: ${{ steps.meta.outputs.tags }}
234+
format: 'table'
235+
exit-code: '1'
236+
ignore-unfixed: true
237+
vuln-type: 'os,library'
238+
scanners: 'vuln,secret,misconfig'
239+
- name: Validate Container Image
240+
run: |
241+
docker run -d -p 8080:8080 ${{ steps.meta.outputs.tags }}
242+
sleep 5 # Wait for container to start
243+
HEALTH_STATUS=$(curl -s http://localhost:8080/companieshouse/actuator/health | jq -r '.status')
244+
if [ "$HEALTH_STATUS" != "UP" ]; then
245+
echo "Health check failed. Status: $HEALTH_STATUS"
246+
exit 1
247+
fi
248+
echo "Health check passed. Status: $HEALTH_STATUS"
249+
- name: Re-Build & Push Image
250+
uses: docker/build-push-action@v6
251+
with:
252+
push: true
223253
tags: ${{ steps.meta.outputs.tags }}
224254
labels: ${{ steps.meta.outputs.labels }}
255+
cache-to: type=registry,ref=${{ env.BASE_IMAGE }}:cache
256+
cache-from: type=registry,ref=${{ env.BASE_IMAGE }}:cache,mode=max
225257
sbom: true
226258
provenance: true
259+
227260
create-release:
228261
if: ${{ needs.docker-build-push.outputs.is-dryrun-version-bumped == 'true' }} # Only release when new version is available
229262
runs-on: ubuntu-latest
230263
permissions:
231264
contents: write # to be able to publish a GitHub release
232265
needs:
233-
- docker-build-push
266+
- docker-build-scan-push
234267
environment:
235268
name: approve-release # Manual Approval to decide if we are ready to push tags and release
236269
steps:

0 commit comments

Comments
 (0)