Skip to content

Instantly share code, notes, and snippets.

@xenjke
Created November 14, 2022 17:57
Show Gist options
  • Save xenjke/db095717a4bfbd1f70b7db2134bbf5d7 to your computer and use it in GitHub Desktop.
Save xenjke/db095717a4bfbd1f70b7db2134bbf5d7 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -eux
declare GITHUB_TOKEN=''
declare TEST_PATH='bddly/quality-engineering'
declare TARGET_ENVIRONMENT='oea-test'
declare EXPECTED_STATUS='completed'
declare EXPECTED_CONCLUSION='success'
declare TIME_BETWEEN_API_CALLS=5
declare DISPATCHED_TIME=''
declare RUN_ID=null
# queued
# in_progress
# completed
declare STATUS=null
# failure
# success
declare CONCLUSION=null
function dispatch_event() {
echo "Dispatching event for ${TEST_PATH}"
curl -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
https://api.github.com/repos/ovotech/journey-tests-runner/actions/workflows/manual_domain.yml/dispatches \
-d '{"ref":"main","inputs":{"target_environment":'${TARGET_ENVIRONMENT}',"tests_path":"'${TEST_PATH}'"}}'
DISPATCHED_TIME=$(date -u +%Y-%m-%dT%H:%M:%SZ)
}
function get_latest_run() {
echo "Polling every ${TIME_BETWEEN_API_CALLS}s. for the latest run since ${DISPATCHED_TIME}"
while [[ ${RUN_ID} = "" || ${RUN_ID} = null ]]; do
read RUN_ID STATUS CONCLUSION < <(echo $(curl -s \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
https://api.github.com/repos/ovotech/journey-tests-runner/actions/runs?created='>='$DISPATCHED_TIME |
jq -r '.workflow_runs[0].id, .workflow_runs[0].status, .workflow_runs[0].conclusion'))
sleep $TIME_BETWEEN_API_CALLS
done
echo "Using run id: ${RUN_ID}"
}
function get_run_status() {
read STATUS CONCLUSION < <(echo $(curl -s \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
https://api.github.com/repos/ovotech/journey-tests-runner/actions/runs/$RUN_ID |
jq -r '.status, .conclusion'))
}
function wait_run_completed() {
echo "Waiting for the run ${RUN_ID} to be ${EXPECTED_STATUS}"
while [[ ${STATUS} != ${EXPECTED_STATUS} || ${CONCLUSION} == null ]]; do
sleep $TIME_BETWEEN_API_CALLS
get_run_status
done
echo "Run completed with conclusion: ${CONCLUSION}"
if [[ ${CONCLUSION} != ${EXPECTED_CONCLUSION} ]]; then
return 1
else
return 0
fi
}
run() {
dispatch_event
get_latest_run
wait_run_completed
}
run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment