Files
wait-for-workflow-action/scripts/wait-for-workflow.sh
2023-04-26 20:16:41 +02:00

80 lines
2.7 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# Set the maximum waiting time (in minutes) and initialize the counter
max_wait_minutes="${MAX_WAIT_MINUTES}"
counter=0
# Get the current time in ISO 8601 format
current_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
# If REF is empty, use github.ref
if [ -z "$REF" ]; then
REF="${GITHUB_REF}"
else
REF="refs/heads/$REF"
fi
echo " Organization: ${ORG_NAME}"
echo " Repository: ${REPO_NAME}"
echo " Reference: $REF"
echo " Maximum wait time: ${max_wait_minutes} minutes"
# If RUN_ID is not empty, use it directly
if [ -n "${RUN_ID}" ]; then
run_id="${RUN_ID}"
echo " Using provided Run ID: $run_id"
else
workflow_id="${WORKFLOW_ID}" # Id of the target workflow
echo " Workflow ID: $workflow_id"
# Wait for the workflow to be triggered
while true; do
echo "⏳ Waiting for the workflow to be triggered..."
response=$(curl -s -H "Accept: application/vnd.github+json" -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/${ORG_NAME}/${REPO_NAME}/actions/workflows/${workflow_id}/runs")
if echo "$response" | grep -q "API rate limit exceeded"; then
echo "❌ API rate limit exceeded. Please try again later."
exit 1
elif echo "$response" | grep -q "Not Found"; then
echo "❌ Invalid input provided (organization, repository, or workflow ID). Please check your inputs."
exit 1
fi
run_id=$(echo "$response" | \
jq -r --arg ref "$(echo "$REF" | sed 's/refs\/heads\///')" --arg current_time "$current_time" \
'.workflow_runs[] | select(.head_branch == $ref and .created_at >= $current_time) | .id')
if [ -n "$run_id" ]; then
echo "🎉 Workflow triggered! Run ID: $run_id"
break
fi
# Increment the counter and check if the maximum waiting time is reached
counter=$((counter + 1))
if [ $((counter * 30)) -ge $((max_wait_minutes * 60)) ]; then
echo "❌ Maximum waiting time for the workflow to be triggered has been reached. Exiting."
exit 1
fi
sleep 30
done
fi
# Wait for the triggered workflow to complete and check its conclusion
while true; do
echo "⌛ Waiting for the workflow to complete..."
run_data=$(curl -s -H "Accept: application/vnd.github+json" -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/${ORG_NAME}/${REPO_NAME}/actions/runs/$run_id")
status=$(echo "$run_data" | jq -r '.status')
if [ "$status" = "completed" ]; then
conclusion=$(echo "$run_data" | jq -r '.conclusion')
if [ "$conclusion" != "success" ]; then
echo "❌ The workflow has not completed successfully. Exiting."
exit 1
else
echo "✅ The workflow completed successfully! Exiting."
break
fi
fi
sleep 30
done