Skip to content

Commit bd08e71

Browse files
committed
feat: Update container build, scanning, and publishing
1 parent ec9a16e commit bd08e71

File tree

3 files changed

+256
-125
lines changed

3 files changed

+256
-125
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
name: Container Build and Release
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
version:
7+
description: "Semantic version of the image"
8+
required: true
9+
type: string
10+
11+
container-file:
12+
description: "Path to the Dockerfile"
13+
type: string
14+
default: "Dockerfile"
15+
16+
container-name:
17+
description: "Name of the container"
18+
type: string
19+
default: "${{ github.repository }}"
20+
21+
sbom:
22+
description: "Generate and upload SBOM"
23+
type: string
24+
default: "true"
25+
26+
signing:
27+
description: "Sign the image"
28+
type: string
29+
default: "false"
30+
31+
env:
32+
REGISTRY: ghcr.io
33+
34+
jobs:
35+
publish-image:
36+
runs-on: ubuntu-latest
37+
38+
permissions:
39+
# to upload SBOM
40+
id-token: write
41+
contents: write
42+
# to upload Docker image
43+
packages: write
44+
45+
steps:
46+
- name: Checkout repository
47+
uses: actions/checkout@v4
48+
49+
- name: Set up Docker Buildx
50+
uses: docker/setup-buildx-action@c47758b77c9736f4b2ef4073d4d51994fabfe349 # v3.7.1
51+
52+
- name: Log in to the Container registry
53+
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
54+
with:
55+
registry: ${{ env.REGISTRY }}
56+
username: ${{ github.actor }}
57+
password: ${{ secrets.GITHUB_TOKEN }}
58+
59+
- name: Set Container Metadata
60+
uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81
61+
with:
62+
images: ${{ env.REGISTRY }}/${{ inputs.container-name }}
63+
tags: |
64+
# latest / main
65+
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }}
66+
# SemVer
67+
type=semver,pattern={{version}},value=${{ inputs.version }}
68+
type=semver,pattern=v{{version}},value=${{ inputs.version }}
69+
type=semver,pattern=v{{major}},value=${{ inputs.version }}
70+
type=semver,pattern=v{{major}}.{{minor}},value=${{ inputs.version }}
71+
72+
- name: Build & Publish Container ${{ inputs.container-name }}
73+
uses: docker/build-push-action@4f58ea79222b3b9dc2c8bbdd6debcef730109a75 # v6.9.0
74+
if: ${{ steps.set-version.outputs.release == 'true' }}
75+
id: build
76+
with:
77+
file: "${{ inputs.container-file }}"
78+
context: .
79+
push: true
80+
tags: ${{ steps.meta.outputs.tags }}
81+
labels: ${{ steps.meta.outputs.labels }}
82+
# SBOM Settings
83+
sbom: true
84+
85+
# Upload Software Bill of Materials (SBOM) to GitHub
86+
- name: Upload SBOM
87+
uses: advanced-security/spdx-dependency-submission-action@5530bab9ee4bbe66420ce8280624036c77f89746 # v0.1.1
88+
if: ${{ inputs.sbom == 'true' }}
89+
with:
90+
filePath: '.'
91+
filePattern: '*.spdx.json'
92+
93+
sign-image:
94+
runs-on: ubuntu-latest
95+
needs: publish-image
96+
# Sign the image only if it is being published
97+
if: ${{ inputs.signing == 'true' && inputs.publish == 'true' }}
98+
99+
permissions:
100+
# read the image from GitHub Container Registry
101+
packages: read
102+
103+
steps:
104+
- name: Checkout repository
105+
uses: actions/checkout@v4
106+
107+
- uses: sigstore/cosign-installer@dc72c7d5c4d10cd6bcb8cf6e3fd625a9e5e537da # v3.7.0
108+
with:
109+
cosign-release: 'v2.4.1'
110+
111+
- name: Log in to the Container registry
112+
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
113+
with:
114+
registry: ${{ env.REGISTRY }}
115+
username: ${{ github.actor }}
116+
password: ${{ secrets.GITHUB_TOKEN }}
117+
118+
- name: Sign the published container
119+
# This step uses the identity token to provision an ephemeral certificate against
120+
# the sigstore community Fulcio instance.
121+
run: |
122+
cosign sign --yes \
123+
${{ env.IMAGE_NAME }}@${{ needs.build-publish-image.outputs.digest }}
124+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: Container Security Scanning
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
version:
7+
description: "Semantic version of the image"
8+
type: string
9+
10+
container-file:
11+
description: "Path to the Dockerfile"
12+
type: string
13+
default: "Dockerfile"
14+
15+
container-name:
16+
description: "Name of the container"
17+
type: string
18+
default: "${{ github.repository }}"
19+
20+
scanning-block:
21+
description: "Block the build if vulnerabilities are found"
22+
type: string
23+
default: "false"
24+
25+
env:
26+
REGISTRY: ghcr.io
27+
28+
jobs:
29+
scan-image:
30+
runs-on: ubuntu-latest
31+
32+
permissions:
33+
contents: read
34+
35+
steps:
36+
- name: Checkout repository
37+
uses: actions/checkout@v4
38+
39+
- name: Set up Docker Buildx
40+
uses: docker/setup-buildx-action@c47758b77c9736f4b2ef4073d4d51994fabfe349 # v3.7.1
41+
42+
- name: Log in to the Container registry
43+
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
44+
with:
45+
registry: ${{ env.REGISTRY }}
46+
username: ${{ github.actor }}
47+
password: ${{ secrets.GITHUB_TOKEN }}
48+
49+
- name: Build Initial Container ${{ inputs.container-name }}
50+
uses: docker/build-push-action@4f58ea79222b3b9dc2c8bbdd6debcef730109a75 # v6.9.0
51+
id: build
52+
with:
53+
file: "${{ inputs.container-file }}"
54+
context: .
55+
tags: ${{ steps.meta.outputs.tags }}
56+
labels: ${{ steps.meta.outputs.labels }}
57+
58+
# Scan the image for vulnerabilities
59+
- name: Run the Anchore / Grype scan action
60+
if: ${{ inputs.scanning == 'true' }}
61+
uses: anchore/scan-action@f2ba85e044c8f5e5014c9a539328a9c78d3bfa49 # v5.2.1
62+
id: scan
63+
with:
64+
image: "${{ env.REGISTRY }}/${{ inputs.container-name }}:${{ steps.build.outputs.digest }}"
65+
only-fixed: true
66+
fail-build: ${{ inputs.scanning-block }}
67+
68+
- name: Upload vulnerability report
69+
if: ${{ inputs.scanning == 'true' }}
70+
uses: github/codeql-action/upload-sarif@v3
71+
with:
72+
sarif_file: ${{ steps.scan.outputs.sarif }}

0 commit comments

Comments
 (0)