|
76 | 76 | echo "docker-image=${DOCKER_REGISTRY}/${REPO_NAME}/${DOCKER_IMAGE_NAME}:${DOCKER_TAG}" >> "${GITHUB_OUTPUT}" |
77 | 77 | fi |
78 | 78 |
|
79 | | - - name: Check if image should be built |
80 | | - id: check-image |
81 | | - shell: bash |
82 | | - working-directory: ${{ inputs.working-directory }} |
83 | | - if: ${{ steps.calculate-image.outputs.skip != 'true' && !inputs.always-rebuild }} |
84 | | - env: |
85 | | - DOCKER_BUILD_DIR: ${{ inputs.docker-build-dir }} |
86 | | - BASE_REVISION: ${{ github.event.pull_request.base.sha || github.sha }} |
87 | | - DOCKER_IMAGE: ${{ steps.calculate-image.outputs.docker-image }} |
88 | | - DOCKER_TAG: ${{ steps.calculate-image.outputs.docker-tag }} |
89 | | - DOCKER_REGISTRY: ${{ inputs.docker-registry }} |
90 | | - run: | |
91 | | - set +e |
92 | | - set -x |
93 | | -
|
94 | | - login() { |
95 | | - aws ecr get-login-password --region us-east-1 | docker login -u AWS --password-stdin "$1" |
96 | | - } |
97 | | -
|
98 | | - retry () { |
99 | | - $* || (sleep 1 && $*) || (sleep 2 && $*) |
100 | | - } |
101 | | -
|
102 | | - retry login "${DOCKER_REGISTRY}" |
103 | | -
|
104 | | - # Check if image already exists, if it does then skip building it |
105 | | - if docker manifest inspect "${DOCKER_IMAGE}"; then |
106 | | - exit 0 |
107 | | - fi |
108 | | -
|
109 | | - # NB: This part requires a full checkout. Otherwise, the merge base will |
110 | | - # be empty. The default action would be to continue rebuild the image |
111 | | - if [[ "$BASE_REVISION" = "$(git rev-parse HEAD)" ]]; then |
112 | | - # if we're on the base branch then use the parent commit |
113 | | - MERGE_BASE=$(git rev-parse HEAD~) |
114 | | - else |
115 | | - # otherwise we're on a PR, so use the most recent base commit |
116 | | - MERGE_BASE=$(git merge-base HEAD "$BASE_REVISION") |
117 | | - fi |
118 | | -
|
119 | | - if [[ -z "${MERGE_BASE}" ]]; then |
120 | | - echo "rebuild=true" >> "${GITHUB_OUTPUT}" |
121 | | -
|
122 | | - echo "Finding merge base only works with full checkout, please set fetch-depth to 0, continuing ..." |
123 | | - exit 0 |
124 | | - fi |
125 | | -
|
126 | | - if ! git rev-parse "${MERGE_BASE}:${DOCKER_BUILD_DIR}"; then |
127 | | - echo "Directory '${DOCKER_BUILD_DIR}' not found in commit $MERGE_BASE, you should rebase onto a more recent commit" |
128 | | - exit 1 |
129 | | - fi |
130 | | -
|
131 | | - PREVIOUS_DOCKER_TAG=$(git rev-parse "${MERGE_BASE}:${DOCKER_BUILD_DIR}") |
132 | | - # If no image exists but the hash is the same as the previous hash then we should error out here |
133 | | - if [[ "${PREVIOUS_DOCKER_TAG}" == "${DOCKER_TAG}" ]]; then |
134 | | - echo "WARNING: Something has gone wrong and the previous image isn't available for the merge-base of your branch" |
135 | | - echo " Will re-build docker image to store in local cache, TTS may be longer" |
136 | | - fi |
137 | | -
|
138 | | - echo "rebuild=true" >> "${GITHUB_OUTPUT}" |
139 | | -
|
140 | 79 | - name: Login to ECR |
141 | 80 | if: ${{ steps.calculate-image.outputs.skip != 'true' && (inputs.always-rebuild || steps.check-image.outputs.rebuild) }} |
142 | 81 | shell: bash |
@@ -165,34 +104,84 @@ runs: |
165 | 104 | set -eux |
166 | 105 | aws secretsmanager get-secret-value --secret-id docker_hub_readonly_token | jq --raw-output '.SecretString' | jq -r .docker_hub_readonly_token | docker login --username pytorchbot --password-stdin |
167 | 106 |
|
168 | | - - name: Build docker image |
169 | | - if: ${{ steps.calculate-image.outputs.skip != 'true' && (inputs.always-rebuild || steps.check-image.outputs.rebuild) }} |
| 107 | + - name: Check if image exists and build it if it doesnt |
| 108 | + id: check-and-build-image |
| 109 | + if: ${{ steps.calculate-image.outputs.skip != 'true' && inputs.always-rebuild }} |
170 | 110 | env: |
171 | 111 | REPO_NAME: ${{ github.event.repository.name }} |
| 112 | + BASE_REVISION: ${{ github.event.pull_request.base.sha || github.sha }} |
| 113 | + DOCKER_BUILD_DIR: ${{ inputs.docker-build-dir }} |
| 114 | + WORKING_DIRECTORY: ${{ inputs.working-directory }} |
172 | 115 | DOCKER_IMAGE: ${{ steps.calculate-image.outputs.docker-image }} |
| 116 | + DOCKER_TAG: ${{ steps.calculate-image.outputs.docker-tag }} |
173 | 117 | DOCKER_REGISTRY: ${{ inputs.docker-registry }} |
174 | | - WORKING_DIRECTORY: ${{ inputs.working-directory }}/${{ inputs.docker-build-dir }} |
175 | 118 | # NB: Retry here as this step frequently fails with network error downloading various stuffs |
176 | 119 | uses: nick-fields/[email protected] |
177 | 120 | with: |
178 | 121 | shell: bash |
179 | 122 | timeout_minutes: 90 |
180 | 123 | max_attempts: 3 |
181 | | - retry_wait_seconds: 90 |
| 124 | + # NB: Wait for half an hour before retrying, this is used as a pooling mechanism to wait |
| 125 | + # for the docker build workflow to finish |
| 126 | + retry_wait_seconds: 1800 |
182 | 127 | command: | |
183 | 128 | set -ex |
184 | 129 |
|
185 | | - # NB: Setting working directory on the step doesn't work with nick-fields/retry https://github.com/nick-fields/retry/issues/89 |
| 130 | + build_image() { |
| 131 | + # NB: Setting working directory on the step doesn't work with nick-fields/retry https://github.com/nick-fields/retry/issues/89 |
| 132 | + pushd "${WORKING_DIRECTORY}/${DOCKER_BUILD_DIR}" |
| 133 | +
|
| 134 | + IMAGE_NAME=$(echo ${DOCKER_IMAGE#"${DOCKER_REGISTRY}/${REPO_NAME}/"} | awk -F '[:,]' '{print $1}') |
| 135 | + # Build new image |
| 136 | + ./build.sh "${IMAGE_NAME}" -t "${DOCKER_IMAGE}" |
| 137 | +
|
| 138 | + popd |
| 139 | +
|
| 140 | + echo "rebuild=true" >> "${GITHUB_OUTPUT}" |
| 141 | + # Reaching this part means that the image has been built successfully |
| 142 | + # so we can just exit from here |
| 143 | + exit 0 |
| 144 | + } |
| 145 | +
|
186 | 146 | pushd "${WORKING_DIRECTORY}" |
187 | 147 |
|
188 | | - IMAGE_NAME=$(echo ${DOCKER_IMAGE#"${DOCKER_REGISTRY}/${REPO_NAME}/"} | awk -F '[:,]' '{print $1}') |
189 | | - # Build new image |
190 | | - ./build.sh "${IMAGE_NAME}" -t "${DOCKER_IMAGE}" |
| 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 ..." |
| 165 | + build_image |
| 166 | + fi |
| 167 | +
|
| 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 |
| 172 | +
|
| 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" |
| 178 | + fi |
| 179 | + build_image |
191 | 180 |
|
192 | 181 | popd |
193 | 182 |
|
194 | 183 | - name: Push to ECR |
195 | | - if: ${{ steps.calculate-image.outputs.skip != 'true' && (inputs.always-rebuild || steps.check-image.outputs.rebuild) }} |
| 184 | + if: ${{ steps.calculate-image.outputs.skip != 'true' && (inputs.always-rebuild || steps.check-and-build-image.outputs.rebuild) }} |
196 | 185 | shell: bash |
197 | 186 | working-directory: ${{ inputs.working-directory }}/${{ inputs.docker-build-dir }} |
198 | 187 | env: |
|
0 commit comments