From 9e7fd01e2f0ae5866c332529d5ff187e5ed5510c Mon Sep 17 00:00:00 2001 From: Julien Jerphanion Date: Mon, 9 Jan 2023 12:01:11 +0100 Subject: [PATCH 1/4] CI Handle Circle CI REST API response Follow-up of #25338. Circle CI REST API return HTTP response with 202 status code even when the POST requests fail. This proposes adding some handling so that errors are reported on GitHub. Co-authored-by: Olivier Grisel --- build_tools/github/trigger_hosting.sh | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/build_tools/github/trigger_hosting.sh b/build_tools/github/trigger_hosting.sh index 2a8e28ff164ff..a38bcf3f4718d 100755 --- a/build_tools/github/trigger_hosting.sh +++ b/build_tools/github/trigger_hosting.sh @@ -31,10 +31,29 @@ else BRANCH=$HEAD_BRANCH fi -curl --request POST \ +# Circle CI REST API might return HTTP response with 202 status code +# even when the POST requests fail. +CIRCLE_CI_RESPONSE=$(curl --request POST \ --url https://circleci.com/api/v2/project/gh/$GITHUB_REPOSITORY/pipeline \ --header "Circle-Token: $CIRCLE_CI_TOKEN" \ --header "content-type: application/json" \ --header "x-attribution-actor-id: github_actions" \ --header "x-attribution-login: github_actions" \ - --data \{\"branch\":\"$BRANCH\",\"parameters\":\{\"GITHUB_RUN_URL\":\"$GITHUB_RUN_URL\"\}\} + --data \{\"branch\":\"$BRANCH\",\"parameters\":\{\"GITHUB_RUN_URL\":\"$GITHUB_RUN_URL\"\}\}) + +echo CIRCLE_CI_RESPONSE + +CIRCLE_CI_RESPONSE_MESSAGE=$(cat $CIRCLE_CI_RESPONSE | jq ".message") + +if [[ "$CIRCLE_CI_RESPONSE_MESSAGE" =~ .*"Not Found".* ]]; then + echo "The endpoint has not been found on Circle CI REST API. Please check the request correctness." + exit 1 +fi + +if [[ "$CIRCLE_CI_RESPONSE_MESSAGE" =~ .*"Permission denied".* ]]; then + echo "Circle CI is blocking the start of the pipeline." + echp "Please check for correct registration of tokens on Circle CI on: https://app.circleci.com/settings/user/tokens" + echo "See: https://support.circleci.com/hc/en-us/articles/360050351292-How-to-Trigger-a-Workflow-via-CircleCI-API-v2" + # "Permission Denied" exit code. + exit 13 +fi From cd1125c346a75760b0f46d74d31c32f156b28e1e Mon Sep 17 00:00:00 2001 From: Julien Jerphanion Date: Mon, 9 Jan 2023 14:12:23 +0100 Subject: [PATCH 2/4] fixup! CI Handle Circle CI REST API response --- build_tools/github/trigger_hosting.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build_tools/github/trigger_hosting.sh b/build_tools/github/trigger_hosting.sh index a38bcf3f4718d..9f8ff5842ce99 100755 --- a/build_tools/github/trigger_hosting.sh +++ b/build_tools/github/trigger_hosting.sh @@ -31,8 +31,8 @@ else BRANCH=$HEAD_BRANCH fi -# Circle CI REST API might return HTTP response with 202 status code -# even when the POST requests fail. +# Circle CI REST API return HTTP response with 202 status code even when the POST requests fail. +# Hence we add some handling so that errors are reported on GitHub. CIRCLE_CI_RESPONSE=$(curl --request POST \ --url https://circleci.com/api/v2/project/gh/$GITHUB_REPOSITORY/pipeline \ --header "Circle-Token: $CIRCLE_CI_TOKEN" \ From 76893f1831423045dd060f945b0a05d5977667ce Mon Sep 17 00:00:00 2001 From: Julien Jerphanion Date: Thu, 12 Jan 2023 09:05:46 +0100 Subject: [PATCH 3/4] DEV Adapt handling according to CircleCI's API docs See: https://circleci.com/docs/api/v2/index.html#operation/triggerPipeline Co-authored-by: Thomas J. Fan --- build_tools/github/trigger_hosting.sh | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/build_tools/github/trigger_hosting.sh b/build_tools/github/trigger_hosting.sh index 9f8ff5842ce99..ecbbd5f1aeefc 100755 --- a/build_tools/github/trigger_hosting.sh +++ b/build_tools/github/trigger_hosting.sh @@ -33,6 +33,7 @@ fi # Circle CI REST API return HTTP response with 202 status code even when the POST requests fail. # Hence we add some handling so that errors are reported on GitHub. +# For details see: https://circleci.com/docs/api/v2/index.html#operation/triggerPipeline CIRCLE_CI_RESPONSE=$(curl --request POST \ --url https://circleci.com/api/v2/project/gh/$GITHUB_REPOSITORY/pipeline \ --header "Circle-Token: $CIRCLE_CI_TOKEN" \ @@ -41,19 +42,22 @@ CIRCLE_CI_RESPONSE=$(curl --request POST \ --header "x-attribution-login: github_actions" \ --data \{\"branch\":\"$BRANCH\",\"parameters\":\{\"GITHUB_RUN_URL\":\"$GITHUB_RUN_URL\"\}\}) -echo CIRCLE_CI_RESPONSE +echo $CIRCLE_CI_RESPONSE CIRCLE_CI_RESPONSE_MESSAGE=$(cat $CIRCLE_CI_RESPONSE | jq ".message") +if [[ "$CIRCLE_CI_RESPONSE_MESSAGE" == "null" ]]; then + exit 0 # No message means there was no error. +fi + if [[ "$CIRCLE_CI_RESPONSE_MESSAGE" =~ .*"Not Found".* ]]; then echo "The endpoint has not been found on Circle CI REST API. Please check the request correctness." - exit 1 fi if [[ "$CIRCLE_CI_RESPONSE_MESSAGE" =~ .*"Permission denied".* ]]; then echo "Circle CI is blocking the start of the pipeline." - echp "Please check for correct registration of tokens on Circle CI on: https://app.circleci.com/settings/user/tokens" + echo "Please check for correct registration of tokens on Circle CI on: https://app.circleci.com/settings/user/tokens" echo "See: https://support.circleci.com/hc/en-us/articles/360050351292-How-to-Trigger-a-Workflow-via-CircleCI-API-v2" - # "Permission Denied" exit code. - exit 13 fi + +exit 1 From 97ecd62d597d0c181822db26ef469972fe6a6a81 Mon Sep 17 00:00:00 2001 From: Julien Jerphanion Date: Thu, 19 Jan 2023 09:13:25 +0100 Subject: [PATCH 4/4] Adapt instructions and reword comment Co-authored-by: Thomas J. Fan --- build_tools/github/trigger_hosting.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/build_tools/github/trigger_hosting.sh b/build_tools/github/trigger_hosting.sh index ecbbd5f1aeefc..e05b03366d868 100755 --- a/build_tools/github/trigger_hosting.sh +++ b/build_tools/github/trigger_hosting.sh @@ -31,7 +31,7 @@ else BRANCH=$HEAD_BRANCH fi -# Circle CI REST API return HTTP response with 202 status code even when the POST requests fail. +# Circle CI REST API returns HTTP responses with 20x status code even when the POST requests fail. # Hence we add some handling so that errors are reported on GitHub. # For details see: https://circleci.com/docs/api/v2/index.html#operation/triggerPipeline CIRCLE_CI_RESPONSE=$(curl --request POST \ @@ -42,9 +42,8 @@ CIRCLE_CI_RESPONSE=$(curl --request POST \ --header "x-attribution-login: github_actions" \ --data \{\"branch\":\"$BRANCH\",\"parameters\":\{\"GITHUB_RUN_URL\":\"$GITHUB_RUN_URL\"\}\}) -echo $CIRCLE_CI_RESPONSE -CIRCLE_CI_RESPONSE_MESSAGE=$(cat $CIRCLE_CI_RESPONSE | jq ".message") +CIRCLE_CI_RESPONSE_MESSAGE=$(echo $CIRCLE_CI_RESPONSE | jq ".message") if [[ "$CIRCLE_CI_RESPONSE_MESSAGE" == "null" ]]; then exit 0 # No message means there was no error.