From cd7861fea9cee80deb6c4ccae441f67c9518c630 Mon Sep 17 00:00:00 2001 From: Jarek Potiuk Date: Wed, 24 Apr 2024 15:16:07 +0200 Subject: [PATCH] Retry merge commit retrieval on failure Sometimes when merge commit retrieval fails (because of race condition most likely as pull request event is no ready yet) build-images builds all images because selective check does not know the merge commit (in pull request target vuild image failure). That was the only side effect. because otherwise build image had a fallback to github.event.pull_request.head.sha in this case. With this PR, we will retry it and fallback explicitly to github.event.pull_request.head.sha if it does not work. --- .github/workflows/build-images.yml | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build-images.yml b/.github/workflows/build-images.yml index 9c732d1166a58..bd10e73aac65c 100644 --- a/.github/workflows/build-images.yml +++ b/.github/workflows/build-images.yml @@ -91,8 +91,25 @@ jobs: - name: Discover PR merge commit id: discover-pr-merge-commit run: | + # Sometimes target-commit-sha cannot be TARGET_COMMIT_SHA="$(gh api '${{ github.event.pull_request.url }}' --jq .merge_commit_sha)" - echo "TARGET_COMMIT_SHA=$TARGET_COMMIT_SHA" >> ${GITHUB_ENV} + if [[ ${TARGET_COMMIT_SHA} == "" ]]; then + # Sometimes retrieving the merge commit SHA from PR fails. We retry it once. Otherwise we + # fall-back to github.event.pull_request.head.sha + echo + echo "Could not retrieve merge commit SHA from PR, waiting for 3 seconds and retrying." + echo + sleep 3 + TARGET_COMMIT_SHA="$(gh api '${{ github.event.pull_request.url }}' --jq .merge_commit_sha)" + if [[ ${TARGET_COMMIT_SHA} == "" ]]; then + echo + echo "Could not retrieve merge commit SHA from PR, falling back to PR head SHA." + echo + TARGET_COMMIT_SHA="${{ github.event.pull_request.head.sha }}" + fi + fi + echo "TARGET_COMMIT_SHA=${TARGET_COMMIT_SHA}" + echo "TARGET_COMMIT_SHA=${TARGET_COMMIT_SHA}" >> ${GITHUB_ENV} echo "target-commit-sha=${TARGET_COMMIT_SHA}" >> ${GITHUB_OUTPUT} if: github.event_name == 'pull_request_target' # The labels in the event aren't updated when re-triggering the job, So lets hit the API to get