@@ -106,7 +106,7 @@ runs:
106106
107107 - name : Check if image exists and build it if it doesnt
108108 id : check-and-build-image
109- if : ${{ steps.calculate-image.outputs.skip != 'true' && inputs.always-rebuild }}
109+ if : ${{ steps.calculate-image.outputs.skip != 'true' }}
110110 env :
111111 REPO_NAME : ${{ github.event.repository.name }}
112112 BASE_REVISION : ${{ github.event.pull_request.base.sha || github.sha }}
@@ -115,6 +115,7 @@ runs:
115115 DOCKER_IMAGE : ${{ steps.calculate-image.outputs.docker-image }}
116116 DOCKER_TAG : ${{ steps.calculate-image.outputs.docker-tag }}
117117 DOCKER_REGISTRY : ${{ inputs.docker-registry }}
118+ ALWAYS_REBUILD : ${{ inputs.always-rebuild }}
118119 # NB: Retry here as this step frequently fails with network error downloading various stuffs
119120 uses :
nick-fields/[email protected] 120121 with :
@@ -143,42 +144,50 @@ runs:
143144 exit 0
144145 }
145146
146- pushd "${WORKING_DIRECTORY}"
147-
148- # Check if image already exists, if it does then skip building it
149- if docker manifest inspect "${DOCKER_IMAGE}"; then
150- exit 0
151- fi
152-
153- # NB: This part requires a full checkout. Otherwise, the merge base will
154- # be empty. The default action would be to continue rebuild the image
155- if [[ "$BASE_REVISION" = "$(git rev-parse HEAD)" ]]; then
156- # if we're on the base branch then use the parent commit
157- MERGE_BASE=$(git rev-parse HEAD~)
158- else
159- # otherwise we're on a PR, so use the most recent base commit
160- MERGE_BASE=$(git merge-base HEAD "$BASE_REVISION")
161- fi
162-
163- if [[ -z "${MERGE_BASE}" ]]; then
164- echo "Finding merge base only works with full checkout, please set fetch-depth to 0, continuing ..."
147+ check_if_image_exists_and_build_it_if_not() {
148+ pushd "${WORKING_DIRECTORY}"
149+
150+ # Check if image already exists, if it does then skip building it
151+ if docker manifest inspect "${DOCKER_IMAGE}"; then
152+ exit 0
153+ fi
154+
155+ # NB: This part requires a full checkout. Otherwise, the merge base will
156+ # be empty. The default action would be to continue rebuild the image
157+ if [[ "$BASE_REVISION" = "$(git rev-parse HEAD)" ]]; then
158+ # if we're on the base branch then use the parent commit
159+ MERGE_BASE=$(git rev-parse HEAD~)
160+ else
161+ # otherwise we're on a PR, so use the most recent base commit
162+ MERGE_BASE=$(git merge-base HEAD "$BASE_REVISION")
163+ fi
164+
165+ if [[ -z "${MERGE_BASE}" ]]; then
166+ echo "Finding merge base only works with full checkout, please set fetch-depth to 0, continuing ..."
167+ build_image
168+ fi
169+
170+ if ! git rev-parse "${MERGE_BASE}:${DOCKER_BUILD_DIR}"; then
171+ echo "Directory '${DOCKER_BUILD_DIR}' not found in commit $MERGE_BASE, you should rebase onto a more recent commit"
172+ exit 1
173+ fi
174+
175+ PREVIOUS_DOCKER_TAG=$(git rev-parse "${MERGE_BASE}:${DOCKER_BUILD_DIR}")
176+ # If no image exists but the hash is the same as the previous hash then we should error out here
177+ if [[ "${PREVIOUS_DOCKER_TAG}" == "${DOCKER_TAG}" ]]; then
178+ echo "WARNING: Something has gone wrong and the previous image isn't available for the merge-base of your branch"
179+ echo " Will re-build docker image to store in local cache, TTS may be longer"
180+ fi
165181 build_image
166- fi
167182
168- if ! git rev-parse "${MERGE_BASE}:${DOCKER_BUILD_DIR}"; then
169- echo "Directory '${DOCKER_BUILD_DIR}' not found in commit $MERGE_BASE, you should rebase onto a more recent commit"
170- exit 1
171- fi
183+ popd
184+ }
172185
173- PREVIOUS_DOCKER_TAG=$(git rev-parse "${MERGE_BASE}:${DOCKER_BUILD_DIR}")
174- # If no image exists but the hash is the same as the previous hash then we should error out here
175- if [[ "${PREVIOUS_DOCKER_TAG}" == "${DOCKER_TAG}" ]]; then
176- echo "WARNING: Something has gone wrong and the previous image isn't available for the merge-base of your branch"
177- echo " Will re-build docker image to store in local cache, TTS may be longer"
186+ if [ "${ALWAYS_REBUILD:-false}" == "true" ]; then
187+ build_image
188+ else
189+ check_if_image_exists_and_build_it_if_not
178190 fi
179- build_image
180-
181- popd
182191
183192 - name : Push to ECR
184193 if : ${{ steps.calculate-image.outputs.skip != 'true' && (inputs.always-rebuild || steps.check-and-build-image.outputs.rebuild) }}
0 commit comments