Skip to content

Instantly share code, notes, and snippets.

@heug
Created August 25, 2022 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heug/fc259e41b80580caf062756a4c956a12 to your computer and use it in GitHub Desktop.
Save heug/fc259e41b80580caf062756a4c956a12 to your computer and use it in GitHub Desktop.
create a mabl deployment and do something based on the results
#!/bin/bash
####################
# user vars #
####################
API_KEY='MY_API_KEY'
DEPLOYMENT_DATA='{"environment_id":"MY_ENV_ID-e,"application_id":"MY_APP_ID-a"}'
####################
# BEGIN TESTS #
####################
DEPLOYMENT_URL=https://api.mabl.com/events/deployment
MABL_USER=key:${API_KEY}
EXECUTION_EVENT_ID=$(curl -s $DEPLOYMENT_URL -u $MABL_USER -H 'Content-Type:application/json' -d $DEPLOYMENT_DATA | jq --raw-output '.id')
echo "Execution Event Id: '$EXECUTION_EVENT_ID'"
if [ -z "$EXECUTION_EVENT_ID" ]; then
echo "Error [No Execution Event ID]"
exit 1
fi
EVENT_URL=https://api.mabl.com/execution/result/event/${EXECUTION_EVENT_ID}
EXECUTION_RESULTS=$(curl -s $EVENT_URL -u $MABL_USER -H 'Content-Type:application/json')
TESTS_STILL_RUNNING=$(echo $EXECUTION_RESULTS | jq '[.executions[].status] | any(. != "succeeded" and . != "failed" and . != "cancelled" and . != "completed" and . != "terminated" )')
echo "Tests still running: '$TESTS_STILL_RUNNING'"
x=1
MAX_ITERATIONS=20
SLEEP_SECONDS=15
while [ "$TESTS_STILL_RUNNING" = true ]&&[ "$x" -le "$MAX_ITERATIONS" ];
do
echo "Iteration $x: TESTS_STILL_RUNNING=$TESTS_STILL_RUNNING. Sleeping for $SLEEP_SECONDS seconds"
sleep $SLEEP_SECONDS
EXECUTION_RESULTS=$(curl -s $EVENT_URL -u $MABL_USER -H 'Content-Type:application/json')
TESTS_STILL_RUNNING=$(echo $EXECUTION_RESULTS | jq '[.executions[].status] | any(. != "succeeded" and . != "failed" and . != "cancelled" and . != "completed" and . != "terminated" )')
(( x++ ))
done
TEST_FAILURES=$(curl -s $EVENT_URL -u $MABL_USER -H 'Content-Type:application/json' | jq '[.executions[].status] | any(. == "failed" or . == "cancelled" or . == "terminated" )')
if [ "$TESTS_STILL_RUNNING" = true ]; then
echo "Tests still running... failng build"
exit 1
fi
if [ "$TEST_FAILURES" = true ]; then
echo "Test failures";
exit 1
fi
echo "All tests passed";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment