diff --git a/github-actions/orb.yml b/github-actions/orb.yml index d56cd1fa..9dd968b3 100644 --- a/github-actions/orb.yml +++ b/github-actions/orb.yml @@ -52,7 +52,7 @@ commands: } >> $BASH_ENV name: Setting variables - run: - command: "#!/bin/bash\nexecute_workflow() {\n\n required_paramater_check\n\n START_TIME=$(date -u -Iseconds)\n INITIAL_RUN_IDS=$(get_run_ids \"$START_TIME\")\n\n echo \"Run_Ids before we start workflow: ${INITIAL_RUN_IDS}\" | tr '\\n' ' ' \n echo \"\"\n\n echo \"Triggering workflow\"\n TRIGGER_WORKFLOW_HTTP_STATUS=$(trigger_workflow)\n check_http_status \"${TRIGGER_WORKFLOW_HTTP_STATUS}\"\n\n NEW_RUN_IDS=$(get_run_ids \"$START_TIME\")\n\n # Wait for GHA API to return latest run\n MAX_RETRIES=10\n while [[ $INITIAL_RUN_IDS == \"$NEW_RUN_IDS\" ]] && [[ MAX_RETRIES -gt 0 ]]\n do\n sleep 2\n MAX_RETRIES=$((MAX_RETRIES-1))\n NEW_RUN_IDS=$(get_run_ids \"$START_TIME\")\n done\n\n if [[ $INITIAL_RUN_IDS == \"$NEW_RUN_IDS\" ]] && [[ MAX_RETRIES -le 0 ]]; then\n echo \"Unable to obtain the run_id.\"\n echo \"Exiting.\"\n exit 1\n fi\n\n echo \"Run_Ids after we started the workflow: ${NEW_RUN_IDS}\" | tr '\\n' ' ' \n echo \"\"\n\n if [[ \"$PARAM_WAIT_FOR_COMPLETION\" == true ]]; then\n for RUN_ID in $NEW_RUN_IDS\n do\n # Check if $run_id is not in $INITIAL_RUN_IDS\n if [[ ! \" ${INITIAL_RUN_IDS[*]} \" =~ ${RUN_ID} ]]; then\n echo \"Run_id we triggered is: $RUN_ID\"\n wait_for_workflow_to_complete \"$RUN_ID\"\n fi\n done\n fi\n\n}\n\nwait_for_workflow_to_complete() {\n run_id=$1\n\n START_TIME=$(date +%s)\n TIMEOUT_TIME=$((START_TIME + 60 * PARAM_WAIT_TIMEOUT))\n WORKFLOW=$(get_workflow_status \"$run_id\")\n\n WORKFLOW_NAME=$(echo \"${WORKFLOW}\" | jq -r '.name')\n WORKFLOW_STATUS=$(echo \"${WORKFLOW}\" | jq -r '.conclusion')\n WORKFLOW_CONCLUSION=$(echo \"${WORKFLOW}\" | jq -r '.status')\n\n echo \"Pipeline name: $WORKFLOW_NAME\"\n echo \"Current status: ${WORKFLOW_STATUS}\"\n echo \"Current conclusion: ${WORKFLOW_CONCLUSION}\"\n\n while [[ \"${WORKFLOW_CONCLUSION}\" == \"null\" || \"${WORKFLOW_CONCLUSION}\" == \"queued\" ]] && [[ \"${WORKFLOW_STATUS}\" != \"completed\" ]] && [[ $(date +%s) -lt TIMEOUT_TIME ]]\n do\n sleep 2\n WORKFLOW=$(get_workflow_status \"$run_id\")\n WORKFLOW_CONCLUSION=$(echo \"${WORKFLOW}\" | jq -r '.conclusion')\n WORKFLOW_STATUS=$(echo \"${WORKFLOW}\" | jq -r '.status')\n\n echo \"Current status: ${WORKFLOW_STATUS}\"\n echo \"Current conclusion: ${WORKFLOW_CONCLUSION}\"\n done\n\n if [[ $(date +%s) -gt TIMEOUT_TIME ]] && [[ \"${WORKFLOW_STATUS}\" != \"completed\" ]]; then\n echo \"Timed out waiting for workflow to complete.\"\n echo \"Current status: ${WORKFLOW_STATUS}\"\n echo \"Current conclusion: ${WORKFLOW_CONCLUSION}\"\n exit 1\n fi\n\n}\n\nrequired_paramater_check() {\n if [ -z \"$PARAM_GITHUB_ACTION_TOKEN\" ]; then\n echo \"Missing GITHUB_ACTION_TOKEN. You must specify a GITHUB Action Token in order to trigger a pipeline.\"\n echo \"To create a token follow these instructions: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token\"\n exit 1\n fi\n\n if [ -z \"$PARAM_REPO_NAME\" ]; then\n echo \"Missing REPO_NAME. The name of the repository where the GitHub Action workflow is, for example team-cppe.\"\n exit 1\n fi\n\n if [ -z \"$PARAM_WORKFLOW_ID\" ]; then\n echo \"Missing WORKFLOW_ID. The Workflow ID paramater must be specified.\"\n echo \"This can be found in the URL of the github action workflow. It will end in .yml. For example trigger-circle.yml.\"\n exit 1\n fi\n\n if [ -z \"$PARAM_GIT_REF\" ]; then\n echo \"Missing PARAM_GIT_REF. The Git Reference is missing, this should be the branch or tag name that is used when executing the workflow.\"\n exit 1\n fi\n\n if [ -z \"$PARAM_WAIT_TIMEOUT\" ]; then\n PARAM_WAIT_TIMEOUT=10\n fi\n}\n\nget_run_ids() {\n CURRENT_TIME=$1\n\n GHA_URL=\"https://api.github.com/repos/ovotech/$PARAM_REPO_NAME/actions/workflows/$PARAM_WORKFLOW_ID/runs?event=workflow_dispatch&created=>=\"$CURRENT_TIME\n\n GHA_RUN_IDS=$(curl -s -X GET -H 'Accept: application/vnd.github.v3+json' -H 'X-GitHub-Api-Version: 2022-11-28' -H 'Authorization: Bearer '\"$PARAM_GITHUB_ACTION_TOKEN\" \"$GHA_URL\" | jq -r '.workflow_runs[].id' | sort )\n\n echo \"$GHA_RUN_IDS\"\n}\n\ntrigger_workflow() {\n\n GHA_URL=\"https://api.github.com/repos/ovotech/$PARAM_REPO_NAME/actions/workflows/$PARAM_WORKFLOW_ID/dispatches\"\n\n if [ -z \"$PARAM_GHA_INPUTS\" ]; then\n GHA_HTTP_STATUS=$(curl --write-out \"%{http_code}\" -s -X POST -H 'Accept: application/vnd.github.v3+json' -H 'X-GitHub-Api-Version: 2022-11-28' -H 'Authorization: Bearer '\"$PARAM_GITHUB_ACTION_TOKEN\" \"$GHA_URL\" -d '{\"ref\":\"'\"$PARAM_GIT_REF\"'\"}')\n else\n GHA_HTTP_STATUS=$(curl --write-out \"%{http_code}\" -s -X POST -H 'Accept: application/vnd.github.v3+json' -H 'X-GitHub-Api-Version: 2022-11-28' -H 'Authorization: Bearer '\"$PARAM_GITHUB_ACTION_TOKEN\" \"$GHA_URL\" -d '{\"ref\":\"'\"$PARAM_GIT_REF\"'\",\"inputs\":'\"$PARAM_GHA_INPUTS\"'}')\n fi\n\n echo \"$GHA_HTTP_STATUS\"\n}\n\nget_workflow_status() {\n RUN_ID=$1\n GHA_URL=\"https://api.github.com/repos/ovotech/$PARAM_REPO_NAME/actions/runs/$RUN_ID\"\n\n GHA_WORKFLOW=$(curl -X GET -s -H 'Accept: application/vnd.github.v3+json' -H 'X-GitHub-Api-Version: 2022-11-28' -H 'Authorization: Bearer '\"$PARAM_GITHUB_ACTION_TOKEN\" \"$GHA_URL\")\n\n echo \"$GHA_WORKFLOW\"\n}\n\ncheck_http_status() {\n HTTP_STATUS=$1\n\n if [[ \"${HTTP_STATUS}\" == *\"400\"* ]]; then\n echo \"Error calling GitHub API\"\n echo \"${HTTP_STATUS}\"\n echo \"This could be because any extra parameters passed are not correctly formatted.\"\n echo \"The extra parameters passed: $PARAM_GHA_INPUTS\"\n exit 1\n fi\n if [[ \"${HTTP_STATUS}\" == *\"401\"* ]]; then\n echo \"Error calling GitHub API\"\n echo \"${HTTP_STATUS}\"\n echo \"This could be because the Token is invalid.\"\n echo \"To create a token (ideally for a service user) follow these instructions: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token\"\n exit 1\n fi\n if [[ \"${HTTP_STATUS}\" == *\"404\"* ]]; then\n echo \"Error calling GitHub API\"\n echo \"${HTTP_STATUS}\"\n echo \"This could be because the WorkflowID that was passed is incorect.\"\n exit 1\n fi\n if [[ \"${HTTP_STATUS}\" == *\"422\"* ]] && [[ \"${HTTP_STATUS}\" == *\"additionalProperties\"* ]] ; then\n echo \"Error calling GitHub API\"\n echo \"The extra parameters passed for the workflow maynot be in the correct format.\"\n echo \"All keys and values need to be double quoted and within {}\"\n echo \"The extra parameters passed: $PARAM_GHA_INPUTS\"\n echo \"${HTTP_STATUS}\"\n exit 1\n fi\n if [[ \"${HTTP_STATUS}\" == *\"422\"* ]] && [[ \"${HTTP_STATUS}\" == *\"Unexpected inputs provided\"* ]] ; then\n echo \"Error calling GitHub API\"\n echo \"The workflow was passed paramaters that it wasn't expecting.\"\n echo \"The extra parameters passed: $PARAM_GHA_INPUTS\"\n echo \"${HTTP_STATUS}\"\n exit 1\n fi\n # Checking if returned status code is not 204 as it should return a 204 (no data)\n if [ \"${HTTP_STATUS}\" != \"204\" ]; then\n echo \"${HTTP_STATUS}: Unable to authenticate, this could be because the Token is invalid! Exiting...\"\n echo \"To create a token (ideally for a service user) follow these instructions: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token\"\n exit 1\n fi\n}\n\n# Will not run if sourced for bats-core tests.\nORB_TEST_ENV=\"bats-core\"\nif [ \"${0#*\"$ORB_TEST_ENV\"}\" == \"$0\" ]; then\n execute_workflow\nfi\n" + command: "#!/bin/bash\nexecute_workflow() {\n\n required_paramater_check\n\n START_TIME=$(date -u -Iseconds)\n INITIAL_RUN_IDS=$(get_run_ids \"$START_TIME\")\n\n echo \"Run_Ids before we start workflow: ${INITIAL_RUN_IDS}\" | tr '\\n' ' ' \n echo \"\"\n\n echo \"Triggering workflow\"\n TRIGGER_WORKFLOW_HTTP_STATUS=$(trigger_workflow)\n check_http_status \"${TRIGGER_WORKFLOW_HTTP_STATUS}\"\n\n NEW_RUN_IDS=$(get_run_ids \"$START_TIME\")\n\n # Wait for GHA API to return latest run\n MAX_RETRIES=10\n while [[ $INITIAL_RUN_IDS == \"$NEW_RUN_IDS\" ]] && [[ MAX_RETRIES -gt 0 ]]\n do\n sleep 2\n MAX_RETRIES=$((MAX_RETRIES-1))\n NEW_RUN_IDS=$(get_run_ids \"$START_TIME\")\n done\n\n if [[ $INITIAL_RUN_IDS == \"$NEW_RUN_IDS\" ]] && [[ MAX_RETRIES -le 0 ]]; then\n echo \"Unable to obtain the run_id.\"\n echo \"Exiting.\"\n exit 1\n fi\n\n echo \"Run_Ids after we started the workflow: ${NEW_RUN_IDS}\" | tr '\\n' ' ' \n echo \"\"\n\n if [[ \"$PARAM_WAIT_FOR_COMPLETION\" == true ]]; then\n for RUN_ID in $NEW_RUN_IDS\n do\n # Check if $run_id is not in $INITIAL_RUN_IDS\n if [[ ! \" ${INITIAL_RUN_IDS[*]} \" =~ ${RUN_ID} ]]; then\n echo \"Run_id we triggered is: $RUN_ID\"\n wait_for_workflow_to_complete \"$RUN_ID\"\n fi\n done\n fi\n\n}\n\nwait_for_workflow_to_complete() {\n run_id=$1\n\n START_TIME=$(date +%s)\n TIMEOUT_TIME=$((START_TIME + 60 * PARAM_WAIT_TIMEOUT))\n WORKFLOW=$(get_workflow_status \"$run_id\")\n\n WORKFLOW_NAME=$(echo \"${WORKFLOW}\" | jq -r '.name')\n WORKFLOW_STATUS=$(echo \"${WORKFLOW}\" | jq -r '.conclusion')\n WORKFLOW_CONCLUSION=$(echo \"${WORKFLOW}\" | jq -r '.status')\n\n echo \"Pipeline name: $WORKFLOW_NAME\"\n echo \"Current status: ${WORKFLOW_STATUS}\"\n echo \"Current conclusion: ${WORKFLOW_CONCLUSION}\"\n\n while [[ \"${WORKFLOW_CONCLUSION}\" == \"null\" || \"${WORKFLOW_CONCLUSION}\" == \"queued\" ]] && [[ \"${WORKFLOW_STATUS}\" != \"completed\" ]] && [[ $(date +%s) -lt TIMEOUT_TIME ]]\n do\n sleep 2\n WORKFLOW=$(get_workflow_status \"$run_id\")\n WORKFLOW_CONCLUSION=$(echo \"${WORKFLOW}\" | jq -r '.conclusion')\n WORKFLOW_STATUS=$(echo \"${WORKFLOW}\" | jq -r '.status')\n\n echo \"Current status: ${WORKFLOW_STATUS}\"\n echo \"Current conclusion: ${WORKFLOW_CONCLUSION}\"\n done\n\n if [[ $(date +%s) -gt TIMEOUT_TIME ]] && [[ \"${WORKFLOW_STATUS}\" != \"completed\" ]]; then\n echo \"Timed out waiting for workflow to complete.\"\n echo \"Current status: ${WORKFLOW_STATUS}\"\n echo \"Current conclusion: ${WORKFLOW_CONCLUSION}\"\n exit 1\n fi\n\n}\n\nrequired_paramater_check() {\n if [ -z \"$PARAM_GITHUB_ACTION_TOKEN\" ]; then\n echo \"Missing GITHUB_ACTION_TOKEN. You must specify a GITHUB Action Token in order to trigger a pipeline.\"\n echo \"To create a token follow these instructions: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token\"\n exit 1\n fi\n\n if [ -z \"$PARAM_REPO_NAME\" ]; then\n echo \"Missing REPO_NAME. The name of the repository where the GitHub Action workflow is, for example team-cppe.\"\n exit 1\n fi\n\n if [ -z \"$PARAM_WORKFLOW_ID\" ]; then\n echo \"Missing WORKFLOW_ID. The Workflow ID paramater must be specified.\"\n echo \"This can be found in the URL of the github action workflow. It will end in .yml. For example trigger-circle.yml.\"\n exit 1\n fi\n\n if [ -z \"$PARAM_GIT_REF\" ]; then\n echo \"Missing PARAM_GIT_REF. The Git Reference is missing, this should be the branch or tag name that is used when executing the workflow.\"\n exit 1\n fi\n\n if [ -z \"$PARAM_WAIT_TIMEOUT\" ]; then\n PARAM_WAIT_TIMEOUT=10\n fi\n}\n\nget_run_ids() {\n CURRENT_TIME=$1\n\n GHA_URL=\"https://api.github.com/repos/ovotech/$PARAM_REPO_NAME/actions/workflows/$PARAM_WORKFLOW_ID/runs?event=workflow_dispatch&created=>=\"$CURRENT_TIME\n\n GHA_RUN_IDS=$(curl -s -X GET -H 'Accept: application/vnd.github.v3+json' -H 'X-GitHub-Api-Version: 2022-11-28' -H 'Authorization: Bearer '\"$PARAM_GITHUB_ACTION_TOKEN\" \"$GHA_URL\" | jq -r '.workflow_runs[].id' | sort )\n\n echo \"$GHA_RUN_IDS\"\n}\n\ntrigger_workflow() {\n\n GHA_URL=\"https://api.github.com/repos/ovotech/$PARAM_REPO_NAME/actions/workflows/$PARAM_WORKFLOW_ID/dispatches\"\n\n # echo \"************** DEBUGGING ****************\"\n\n if [ -z \"$PARAM_GHA_INPUTS\" ]; then\n GHA_HTTP_STATUS=$(curl --write-out \"%{http_code}\" -s -X POST -H 'Accept: application/vnd.github.v3+json' -H 'X-GitHub-Api-Version: 2022-11-28' -H 'Authorization: Bearer '\"$PARAM_GITHUB_ACTION_TOKEN\" \"$GHA_URL\" -d '{\"ref\":\"'\"$PARAM_GIT_REF\"'\"}')\n else\n GHA_HTTP_STATUS=$(curl --write-out \"%{http_code}\" -s -X POST -H 'Accept: application/vnd.github.v3+json' -H 'X-GitHub-Api-Version: 2022-11-28' -H 'Authorization: Bearer '\"$PARAM_GITHUB_ACTION_TOKEN\" \"$GHA_URL\" -d '{\"ref\":\"'\"$PARAM_GIT_REF\"'\",\"inputs\":'\"$PARAM_GHA_INPUTS\"'}')\n fi\n\n echo \"$GHA_HTTP_STATUS\"\n}\n\nget_workflow_status() {\n RUN_ID=$1\n GHA_URL=\"https://api.github.com/repos/ovotech/$PARAM_REPO_NAME/actions/runs/$RUN_ID\"\n\n GHA_WORKFLOW=$(curl -X GET -s -H 'Accept: application/vnd.github.v3+json' -H 'X-GitHub-Api-Version: 2022-11-28' -H 'Authorization: Bearer '\"$PARAM_GITHUB_ACTION_TOKEN\" \"$GHA_URL\")\n\n echo \"$GHA_WORKFLOW\"\n}\n\ncheck_http_status() {\n HTTP_STATUS=$1\n\n if [[ \"${HTTP_STATUS}\" == *\"400\"* ]]; then\n echo \"Error calling GitHub API\"\n echo \"${HTTP_STATUS}\"\n echo \"This could be because any extra parameters passed are not correctly formatted.\"\n echo \"The extra parameters passed: $PARAM_GHA_INPUTS\"\n exit 1\n fi\n if [[ \"${HTTP_STATUS}\" == *\"401\"* ]]; then\n echo \"Error calling GitHub API\"\n echo \"${HTTP_STATUS}\"\n echo \"This could be because the Token is invalid.\"\n echo \"To create a token (ideally for a service user) follow these instructions: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token\"\n exit 1\n fi\n if [[ \"${HTTP_STATUS}\" == *\"404\"* ]]; then\n echo \"Error calling GitHub API\"\n echo \"${HTTP_STATUS}\"\n echo \"This could be because the WorkflowID that was passed is incorect.\"\n exit 1\n fi\n if [[ \"${HTTP_STATUS}\" == *\"422\"* ]] && [[ \"${HTTP_STATUS}\" == *\"additionalProperties\"* ]] ; then\n echo \"Error calling GitHub API\"\n echo \"The extra parameters passed for the workflow maynot be in the correct format.\"\n echo \"All keys and values need to be double quoted and within {}\"\n echo \"The extra parameters passed: $PARAM_GHA_INPUTS\"\n echo \"${HTTP_STATUS}\"\n exit 1\n fi\n if [[ \"${HTTP_STATUS}\" == *\"422\"* ]] && [[ \"${HTTP_STATUS}\" == *\"Unexpected inputs provided\"* ]] ; then\n echo \"Error calling GitHub API\"\n echo \"The workflow was passed paramaters that it wasn't expecting.\"\n echo \"The extra parameters passed: $PARAM_GHA_INPUTS\"\n echo \"${HTTP_STATUS}\"\n exit 1\n fi\n # Checking if returned status code is not 204 as it should return a 204 (no data)\n if [ \"${HTTP_STATUS}\" != \"204\" ]; then\n echo \"${HTTP_STATUS}: Unable to authenticate, this could be because the Token is invalid! Exiting...\"\n echo \"To create a token (ideally for a service user) follow these instructions: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token\"\n exit 1\n fi\n}\n\n# Will not run if sourced for bats-core tests.\nORB_TEST_ENV=\"bats-core\"\nif [ \"${0#*\"$ORB_TEST_ENV\"}\" == \"$0\" ]; then\n execute_workflow\nfi\n" name: Execute GitHub Actions workflow extract_artifact: description: |