Skip to content

Commit 99d559c

Browse files
fcanovaisxd
authored andcommitted
ci: copy and sign prod images
Use skopeo to copy testing images to the production registry when they pass the security tests, instead of rebuilding them. After that, we sign the production images too. Signed-off-by: Francesco Canovai <[email protected]>
1 parent 058205b commit 99d559c

File tree

1 file changed

+41
-33
lines changed

1 file changed

+41
-33
lines changed

.github/workflows/bake.yaml

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -121,57 +121,65 @@ jobs:
121121
with:
122122
sarif_file: snyk.sarif
123123

124-
# Build the image for production.
125-
#
126-
# TODO: no need to rebuild everything, just copy the testing images we have generated to the production registry
127-
# if we get here and we are building for production.
128-
prodbuild:
124+
# Use the metadata generated in the `testbuild` step to find all the images
125+
# that have been built. We copy them one by one to the procuction registry
126+
# using skopeo. Then we sign the production images too.
127+
copytoproduction:
128+
name: Copy images to production
129129
if: github.event.inputs.environment == 'production' || github.event_name == 'schedule'
130-
name: Build for production
131130
runs-on: ubuntu-latest
132131
needs:
132+
- testbuild
133133
- security
134134
permissions:
135135
contents: read
136136
packages: write
137137
security-events: write
138+
# Required by the cosign step
139+
id-token: write
138140
steps:
139-
- name: Checkout Code
140-
uses: actions/checkout@v4
141-
142141
- name: Log in to the GitHub Container registry
143142
uses: docker/login-action@v3
144143
with:
145144
registry: ghcr.io
146145
username: ${{ github.actor }}
147146
password: ${{ secrets.GITHUB_TOKEN }}
148147

149-
- name: Set up QEMU
150-
uses: docker/setup-qemu-action@v3
151-
with:
152-
platforms: 'arm64'
153-
154-
- name: Set up Docker Buildx
155-
uses: docker/setup-buildx-action@v3
156-
157-
- name: Build and push
158-
uses: docker/bake-action@v6
159-
id: build
160-
env:
161-
environment: production
162-
registry: ghcr.io/${{ github.repository_owner }}
163-
revision: ${{ github.sha }}
164-
with:
165-
push: true
166-
148+
- name: Copy images
149+
run: |
150+
images=$(echo '${{needs.testbuild.outputs.metadata}}' |
151+
jq -r '
152+
.[] as $items |
153+
(
154+
$items."image.name" |
155+
split(",")[] +
156+
"@" +
157+
$items."containerimage.digest"
158+
)
159+
'
160+
)
161+
for image in $images
162+
do
163+
testimageshaonly="${image%:*@*}@${image#*@}"
164+
testimagenosha="${image%@*}"
165+
prodimage="${testimagenosha/-testing/}"
166+
echo "Copying ${testimageshaonly} to ${prodimage}"
167+
docker run --quiet quay.io/skopeo/stable:v1.17.0-immutable copy -q -a \
168+
--dest-creds ${{ github.actor }}:${{ secrets.GITHUB_TOKEN }} \
169+
docker://${testimageshaonly} docker://${prodimage}
170+
done
167171
- name: Install cosign
168172
uses: sigstore/cosign-installer@v3
169-
# See https://github.blog/security/supply-chain-security/safeguard-container-signing-capability-actions/
170-
# and https://github.com/actions/starter-workflows/blob/main/ci/docker-publish.yml for more details on
171-
# how to use cosign.
172173
- name: Sign images
173174
run: |
174-
images=$(echo '${{ steps.build.outputs.metadata }}' |
175-
jq '.[] | (."image.name" | sub(",.*";"" )) + "@" + ."containerimage.digest"'
175+
images=$(echo '${{needs.testbuild.outputs.metadata}}' |
176+
jq -r '.[] |
177+
(
178+
."image.name" |
179+
sub(",.*";"") |
180+
sub("-testing:[^@]+";"")
181+
) + "@" + ."containerimage.digest"
182+
'
176183
)
177-
cosign sign --yes ${images}
184+
echo "Signing ${images}"
185+
cosign sign --yes ${images}

0 commit comments

Comments
 (0)