Skip to content

Commit d2fdfc1

Browse files
committed
ci: retry build on cache miss
Out build pipeline fails on "stale" branches and runs into cache not found errors. To circumvent this, we first try to build the image with cache and should that fail, we try again without cache.
1 parent f95d271 commit d2fdfc1

1 file changed

Lines changed: 23 additions & 9 deletions

File tree

.github/workflows/101_build.yml

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ on:
1010
type: string
1111
default: "all"
1212
outputs:
13-
chart_version:
13+
chart_version:
1414
description: "Connaisseur Helm chart version"
1515
value: ${{ jobs.context.outputs.chart_version }}
1616
original_registry:
@@ -35,7 +35,8 @@ on:
3535
description: "Workflow build tag used for testing (unique for each run)"
3636
value: ${{ jobs.context.outputs.build_tag }}
3737
build_image:
38-
description: "Workflow build image used for testing, i.e. registry + repository + tag"
38+
description: "Workflow build image used for testing, i.e. registry + repository
39+
+ tag"
3940
value: ${{ jobs.context.outputs.build_image }}
4041
build_labels:
4142
description: "Repository- and workflow-specific build labels"
@@ -119,8 +120,8 @@ jobs:
119120
BUILD_TAG="${BUILD_IMAGE//${PREFIX}/}"
120121
[[ ${BUILD_TAG} == "sha-"* ]] || exit 1 # check as parsing of the BUILD_TAG maybe fragile and dependent on docker/metadata-action priorities
121122
REF_TAGS="${REF_TAGS//${BUILD_TAG}/}"
122-
123-
123+
124+
124125
echo CHART_VERSION=${CHART_VERSION} >> ${GITHUB_OUTPUT}
125126
echo ORIGINAL_REGISTRY=${ORIGINAL_REGISTRY} >> ${GITHUB_OUTPUT}
126127
echo ORIGINAL_REPO=${ORIGINAL_REPO} >> ${GITHUB_OUTPUT}
@@ -136,7 +137,7 @@ jobs:
136137
build:
137138
runs-on: ubuntu-latest
138139
if: inputs.jobs_to_run != 'skip-all'
139-
needs: [context]
140+
needs: [ context ]
140141
permissions:
141142
packages: write
142143
steps:
@@ -158,8 +159,9 @@ jobs:
158159
TAGS="${PREFIX}${{ needs.context.outputs.build_tag }},$(echo ${{ needs.context.outputs.ref_tags }} | tr ' ' '\n' | awk '{print "${PREFIX}"$1}' | envsubst | tr '\n' ',')"
159160
echo tags=${TAGS} >> ${GITHUB_OUTPUT}
160161
shell: bash
161-
- name: Build and push image
162-
id: build
162+
- name: Build and push image (with cache)
163+
id: build_cached
164+
continue-on-error: true
163165
uses: docker/build-push-action@bcafcacb16a39f128d818304e6c9c0c18556b85f # v7.1.0
164166
with:
165167
push: true
@@ -169,7 +171,19 @@ jobs:
169171
labels: ${{ needs.context.outputs.build_labels }}
170172
tags: ${{ steps.tags.outputs.tags }}
171173
sbom: false
172-
provenance: false #TODO: Set to false, as resulting format is not OCI (GHCR) compliant (https://github.com/docker/build-push-action/issues/820) and causes problems with GHCR and e.g. image deletion (https://github.com/snok/container-retention-policy/issues/63)
174+
provenance: false #TODO: Set to false, as resulting format is not OCI (GHCR) compliant (https://github.com/docker/build-push-action/issues/820) and causes problems with GHCR and e.g. image deletion (https://github.com/snok/container-retention-policy/issues/63)
175+
- name: Build and push image (without cache)
176+
id: build
177+
if: steps.build_cached.outcome == 'failure'
178+
uses: docker/build-push-action@bcafcacb16a39f128d818304e6c9c0c18556b85f # v7.1.0
179+
with:
180+
push: true
181+
cache-to: type=gha,scope=build-${{ github.ref_name }},mode=max
182+
file: build/Dockerfile
183+
labels: ${{ needs.context.outputs.build_labels }}
184+
tags: ${{ steps.tags.outputs.tags }}
185+
sbom: false
186+
provenance: false #TODO: Set to false, as resulting format is not OCI (GHCR) compliant (https://github.com/docker/build-push-action/issues/820) and causes problems with GHCR and e.g. image deletion (https://github.com/snok/container-retention-policy/issues/63)
173187
- name: Show build information
174188
run: |
175189
echo "# :building_construction: Build Information" >> ${GITHUB_STEP_SUMMARY}
@@ -178,7 +192,7 @@ jobs:
178192
echo "<tr><td>Original image</td><td><code>${{ needs.context.outputs.original_image }}</code></td></tr>" >> ${GITHUB_STEP_SUMMARY}
179193
echo "<tr><td>Workflow image</td><td><code>${{ needs.context.outputs.build_registry }}/${{ needs.context.outputs.build_repo }}:${{ needs.context.outputs.build_tag }}</code></td></tr>" >> ${GITHUB_STEP_SUMMARY}
180194
echo "<tr><td>All images</td><td><code>$(echo ${{ steps.tags.outputs.tags }} | tr ',' '\n')</code></td></tr>" >> ${GITHUB_STEP_SUMMARY}
181-
echo "<tr><td>Digest</td><td><code>${{ steps.build.outputs.digest }}</code></td></tr>" >> ${GITHUB_STEP_SUMMARY}
195+
echo "<tr><td>Digest</td><td><code>${{ steps.build_cached.outputs.digest || steps.build.outputs.digest }}</code></td></tr>" >> ${GITHUB_STEP_SUMMARY}
182196
echo "</table>" >> ${GITHUB_STEP_SUMMARY}
183197
echo "" >> ${GITHUB_STEP_SUMMARY}
184198
shell: bash

0 commit comments

Comments
 (0)