From d97b1b048b8ee91f3fdd3930fde4e1f608f3ce2f Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Wed, 18 Dec 2024 00:55:26 -0330 Subject: [PATCH 1/4] ci: Improve accuracy of `wait-for-circleci-workflow-status` The GitHub Actions workflow `wait-for-circleci-workflow-status` has been updated to ensure that it waits for the correct workflow. Previously it always chose the most recent workflow for the given branch, but it may have chosen a workflow corresponding to the wrong commit. It has been updated to find one matching the same commit that triggered the GitHub Actions workflow. --- .github/workflows/wait-for-circleci-workflow-status.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/wait-for-circleci-workflow-status.yml b/.github/workflows/wait-for-circleci-workflow-status.yml index 18e5ef7825d5..0ee7dabc168c 100644 --- a/.github/workflows/wait-for-circleci-workflow-status.yml +++ b/.github/workflows/wait-for-circleci-workflow-status.yml @@ -13,8 +13,12 @@ jobs: OWNER: ${{ github.repository_owner }} REPOSITORY: ${{ github.event.repository.name }} BRANCH: ${{ github.head_ref || github.ref_name }} + # For a `push` event, the HEAD commit hash is `github.sha`. + # For a `pull_request` event, `github.sha` is instead the base branch commit hash. The + # HEAD commit hash is `pull_request.head.sha`. + HEAD_COMMIT_HASH: ${{ github.event.pull_request.head.sha || github.sha }} run: | - pipeline_id=$(curl --silent "https://circleci.com/api/v2/project/gh/$OWNER/$REPOSITORY/pipeline?branch=$BRANCH" | jq -r ".items[0].id") + pipeline_id=$(curl --silent "https://circleci.com/api/v2/project/gh/$OWNER/$REPOSITORY/pipeline?branch=$BRANCH" | jq -r ".items[] | select(.vcs.revision == \"${HEAD_COMMIT_HASH}\" ) | .id") workflow_status=$(curl --silent "https://circleci.com/api/v2/pipeline/$pipeline_id/workflow" | jq -r ".items[0].status") if [ "$workflow_status" == "running" ]; then From 412dd70530a0b132c7ea609bebea1fed2603d145 Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Wed, 18 Dec 2024 01:07:22 -0330 Subject: [PATCH 2/4] Fix jq expression Previously it was returning a list. Instead now we're filtering the list, then selecting the id from the first entry. --- .github/workflows/wait-for-circleci-workflow-status.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/wait-for-circleci-workflow-status.yml b/.github/workflows/wait-for-circleci-workflow-status.yml index 0ee7dabc168c..2d88d92669be 100644 --- a/.github/workflows/wait-for-circleci-workflow-status.yml +++ b/.github/workflows/wait-for-circleci-workflow-status.yml @@ -18,7 +18,7 @@ jobs: # HEAD commit hash is `pull_request.head.sha`. HEAD_COMMIT_HASH: ${{ github.event.pull_request.head.sha || github.sha }} run: | - pipeline_id=$(curl --silent "https://circleci.com/api/v2/project/gh/$OWNER/$REPOSITORY/pipeline?branch=$BRANCH" | jq -r ".items[] | select(.vcs.revision == \"${HEAD_COMMIT_HASH}\" ) | .id") + pipeline_id=$(curl --silent "https://circleci.com/api/v2/project/gh/$OWNER/$REPOSITORY/pipeline?branch=$BRANCH" | jq -r ".items | map(select(.vcs.revision == \"${HEAD_COMMIT_HASH}\" )) | first | .id") workflow_status=$(curl --silent "https://circleci.com/api/v2/pipeline/$pipeline_id/workflow" | jq -r ".items[0].status") if [ "$workflow_status" == "running" ]; then From 07f7857fea3f7bdc3936bb7ee4eb939355e7a059 Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Wed, 18 Dec 2024 01:14:46 -0330 Subject: [PATCH 3/4] Add debug logs --- .github/workflows/wait-for-circleci-workflow-status.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/wait-for-circleci-workflow-status.yml b/.github/workflows/wait-for-circleci-workflow-status.yml index 2d88d92669be..9fffde5bebb7 100644 --- a/.github/workflows/wait-for-circleci-workflow-status.yml +++ b/.github/workflows/wait-for-circleci-workflow-status.yml @@ -18,7 +18,9 @@ jobs: # HEAD commit hash is `pull_request.head.sha`. HEAD_COMMIT_HASH: ${{ github.event.pull_request.head.sha || github.sha }} run: | + echo "Looking for pipeline for commit ${HEAD_COMMIT_HASH}" pipeline_id=$(curl --silent "https://circleci.com/api/v2/project/gh/$OWNER/$REPOSITORY/pipeline?branch=$BRANCH" | jq -r ".items | map(select(.vcs.revision == \"${HEAD_COMMIT_HASH}\" )) | first | .id") + echo "Found pipeline ID ${pipeline_id}" workflow_status=$(curl --silent "https://circleci.com/api/v2/pipeline/$pipeline_id/workflow" | jq -r ".items[0].status") if [ "$workflow_status" == "running" ]; then From d9c66776936b9c466333c3c7c105a6d84215d5dd Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Wed, 18 Dec 2024 01:17:18 -0330 Subject: [PATCH 4/4] Improve debug log The two logs were consolidated into one, and the wording was improved. Quotes were added around the variables to make it clearer in the case where they are empty. --- .github/workflows/wait-for-circleci-workflow-status.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/wait-for-circleci-workflow-status.yml b/.github/workflows/wait-for-circleci-workflow-status.yml index 9fffde5bebb7..725a4c3b975d 100644 --- a/.github/workflows/wait-for-circleci-workflow-status.yml +++ b/.github/workflows/wait-for-circleci-workflow-status.yml @@ -18,9 +18,8 @@ jobs: # HEAD commit hash is `pull_request.head.sha`. HEAD_COMMIT_HASH: ${{ github.event.pull_request.head.sha || github.sha }} run: | - echo "Looking for pipeline for commit ${HEAD_COMMIT_HASH}" pipeline_id=$(curl --silent "https://circleci.com/api/v2/project/gh/$OWNER/$REPOSITORY/pipeline?branch=$BRANCH" | jq -r ".items | map(select(.vcs.revision == \"${HEAD_COMMIT_HASH}\" )) | first | .id") - echo "Found pipeline ID ${pipeline_id}" + echo "Waiting for pipeline '${pipeline_id}' for commit hash '${HEAD_COMMIT_HASH}'" workflow_status=$(curl --silent "https://circleci.com/api/v2/pipeline/$pipeline_id/workflow" | jq -r ".items[0].status") if [ "$workflow_status" == "running" ]; then