Skip to content

Commit 0103327

Browse files
committed
feat: Dockerfile attestation and signing
1 parent c0c436b commit 0103327

File tree

1 file changed

+89
-7
lines changed

1 file changed

+89
-7
lines changed

.github/workflows/docker.yml

Lines changed: 89 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
# This is a GitHub Actions workflow to make the Docker image
2+
3+
# It's triggered on either:
4+
# A) Pushes to the main branch, which will update the :latest tag
5+
# B) The creation of a new git tag, which will create a new version tag :x.x.x
6+
# C) Manual workflow runs with a custom tag, to publish a temp feature image
7+
8+
# The workflow does the following:
9+
# 1. Checks out the code
10+
# 2. Sets up QEMU for multi-arch builds, and Buildx for advanced building
11+
# 3. Authenticates with registries (DockerHub, GHCR)
12+
# 4. Determines the Docker tags based on the branch or tag
13+
# 5. Builds the Docker image for specified architectures
14+
# 6. Pushes the Docker image to authenticated registries
15+
# 7. Generates an SBOM in SPDX for the included dependencies
16+
# 8. Attests and pushes the build provenance and SBOM to registries
17+
118
name: 🐳 Build & Push Docker Image
219

320
on:
@@ -6,11 +23,34 @@ on:
623
- main
724
tags:
825
- 'v*'
26+
workflow_dispatch:
27+
inputs:
28+
dry_run:
29+
description: 'Run build without pushing image?'
30+
required: false
31+
default: 'false'
32+
33+
permissions:
34+
id-token: write
35+
contents: read
36+
attestations: write
37+
packages: write
938

1039
jobs:
1140
build-and-push:
1241
runs-on: ubuntu-latest
1342

43+
strategy:
44+
matrix:
45+
target: [dockerhub, ghcr]
46+
include:
47+
- target: dockerhub
48+
registry: docker.io
49+
image: lissy93/domain-locker
50+
- target: ghcr
51+
registry: ghcr.io
52+
image: ghcr.io/lissy93/domain-locker
53+
1454
steps:
1555
- name: Check out repo
1656
uses: actions/checkout@v4
@@ -22,35 +62,77 @@ jobs:
2262
uses: docker/setup-buildx-action@v3
2363

2464
- name: Login to Docker Hub
65+
if: matrix.target == 'dockerhub'
2566
uses: docker/login-action@v3
2667
with:
2768
username: ${{ secrets.DOCKER_USERNAME }}
2869
password: ${{ secrets.DOCKER_PASSWORD }}
2970

3071
- name: Login to GitHub Container Registry
72+
if: matrix.target == 'ghcr'
3173
uses: docker/login-action@v3
3274
with:
3375
registry: ghcr.io
3476
username: ${{ github.actor }}
3577
password: ${{ secrets.GITHUB_TOKEN }}
36-
# username: liss-bot
37-
# password: ${{ secrets.BOT_TOKEN }}
3878

3979
- name: Determine Docker Tags
4080
id: docker_tags
4181
run: |
4282
if [[ "${GITHUB_REF}" == refs/tags/v* ]]; then
4383
RAW_VERSION="${GITHUB_REF#refs/tags/v}"
44-
echo "tags=lissy93/domain-locker:${RAW_VERSION},ghcr.io/lissy93/domain-locker:${RAW_VERSION}" >> $GITHUB_OUTPUT
84+
echo "tag_name=${RAW_VERSION}" >> $GITHUB_OUTPUT
85+
echo "is_tagged=true" >> $GITHUB_OUTPUT
86+
echo "tags=${{ matrix.image }}:${RAW_VERSION}" >> $GITHUB_OUTPUT
4587
else
46-
# On main branch
47-
echo "tags=lissy93/domain-locker:latest,ghcr.io/lissy93/domain-locker:latest" >> $GITHUB_OUTPUT
88+
echo "tag_name=latest" >> $GITHUB_OUTPUT
89+
echo "is_tagged=false" >> $GITHUB_OUTPUT
90+
echo "tags=${{ matrix.image }}:latest" >> $GITHUB_OUTPUT
4891
fi
49-
92+
5093
- name: Build & Push Multi-Arch Docker Image
94+
id: build
5195
uses: docker/build-push-action@v5
5296
with:
5397
context: .
54-
push: true
98+
push: ${{ github.event_name != 'workflow_dispatch' || github.event.inputs.dry_run != 'true' }}
5599
tags: ${{ steps.docker_tags.outputs.tags }}
56100
platforms: linux/amd64,linux/arm64
101+
provenance: false
102+
outputs: type=registry
103+
labels: |
104+
org.opencontainers.image.source=https://github.com/${{ github.repository }}
105+
org.opencontainers.image.version=${{ steps.docker_tags.outputs.tag_name }}
106+
org.opencontainers.image.revision=${{ github.sha }}
107+
108+
- name: Extract digest from Docker image
109+
id: digest
110+
run: |
111+
echo "digest=${{ steps.build.outputs.digest }}" >> $GITHUB_OUTPUT
112+
113+
- name: Generate SBOM (SPDX)
114+
if: github.event_name != 'workflow_dispatch' || github.event.inputs.dry_run != 'true'
115+
id: sbom
116+
uses: anchore/[email protected]
117+
with:
118+
image: ${{ matrix.image }}:${{ steps.docker_tags.outputs.tag_name }}
119+
format: spdx-json
120+
output-file: sbom.spdx.json
121+
122+
- name: Generate provenance attestation
123+
if: github.event_name != 'workflow_dispatch' || github.event.inputs.dry_run != 'true'
124+
uses: actions/attest-build-provenance@v2
125+
with:
126+
subject-name: ${{ matrix.image }}
127+
subject-digest: ${{ steps.digest.outputs.digest }}
128+
push-to-registry: true
129+
130+
- name: Attest SBOM to registry
131+
if: github.event_name != 'workflow_dispatch' || github.event.inputs.dry_run != 'true'
132+
uses: actions/attest-sbom@v1
133+
with:
134+
subject-name: ${{ matrix.image }}
135+
subject-digest: ${{ steps.digest.outputs.digest }}
136+
sbom-path: sbom.spdx.json
137+
push-to-registry: true
138+

0 commit comments

Comments
 (0)