|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Script from https://github.com/kamilchodola/wait-for-workflow-action with change for current_time checks |
| 4 | + |
| 5 | +# ORG_NAME=openebs |
| 6 | +# REPO_NAME=mayastor-control-plane |
| 7 | +# REF=release/2.9 |
| 8 | +# MAX_FIND_MINUTES=3 |
| 9 | +# TIMEOUT_MINUTES=60 |
| 10 | +# INTERVAL_SECS=1 |
| 11 | +# WORKFLOW_ID="nightly-ci.yml" |
| 12 | + |
| 13 | +set -u |
| 14 | + |
| 15 | +# Set the maximum waiting time (in minutes) and initialize the counter |
| 16 | +max_find_minutes="${MAX_FIND_MINUTES:-3}" |
| 17 | +timeout="${TIMEOUT_MINUTES:-360}" |
| 18 | +interval="${INTERVAL_SECS:-60}" |
| 19 | +wait="${WAIT:-"true"}" |
| 20 | +counter=0 |
| 21 | + |
| 22 | +# Get the current time in ISO 8601 format |
| 23 | +current_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ") |
| 24 | +start_time=${START_TIME:-$current_time} |
| 25 | + |
| 26 | +# Check if REF has the prefix "refs/heads/" and append it if not |
| 27 | +if [[ ! "$REF" =~ ^refs/heads/ ]]; then |
| 28 | + REF="refs/heads/$REF" |
| 29 | +fi |
| 30 | + |
| 31 | +GITHUB_AUTH= |
| 32 | +if [ -n "${GITHUB_TOKEN:-}" ]; then |
| 33 | + GITHUB_AUTH="-H \"Authorization: token $GITHUB_TOKEN\"" |
| 34 | +fi |
| 35 | + |
| 36 | +echo "ℹ️ Organization: ${ORG_NAME}" |
| 37 | +echo "ℹ️ Repository: ${REPO_NAME}" |
| 38 | +echo "ℹ️ Reference: $REF" |
| 39 | +echo "ℹ️ Timeout to find the workflow: ${max_find_minutes} minutes" |
| 40 | +echo "ℹ️ Timeout for the workflow to complete: ${timeout} minutes" |
| 41 | +echo "ℹ️ Interval between checks: ${interval} seconds" |
| 42 | + |
| 43 | +# If RUN_ID is not empty, use it directly |
| 44 | +if [ -n "${RUN_ID:-}" ]; then |
| 45 | + run_id="${RUN_ID}" |
| 46 | + echo "ℹ️ Using provided Run ID: $run_id" |
| 47 | +else |
| 48 | + workflow_id="${WORKFLOW_ID}" # Id of the target workflow |
| 49 | + echo "ℹ️ Workflow ID: $workflow_id" |
| 50 | + |
| 51 | + # Wait for the workflow to be triggered |
| 52 | + while true; do |
| 53 | + echo "⏳ Waiting for the workflow to be found..." |
| 54 | + response=$(curl -s -H "Accept: application/vnd.github+json" $GITHUB_AUTH \ |
| 55 | + "https://api.github.com/repos/${ORG_NAME}/${REPO_NAME}/actions/workflows/${workflow_id}/runs") |
| 56 | + if echo "$response" | grep -q "API rate limit exceeded"; then |
| 57 | + echo "❌ API rate limit exceeded. Please try again later." |
| 58 | + exit 1 |
| 59 | + elif echo "$response" | grep -q "Not Found"; then |
| 60 | + echo "❌ Invalid input provided (organization, repository, or workflow ID). Please check your inputs." |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | + run_id=$(echo "$response" | \ |
| 64 | + jq -r --arg ref "$(echo "$REF" | sed 's/refs\/heads\///')" --arg current_time "$current_time" --arg start_time "$start_time" \ |
| 65 | + '.workflow_runs[] | select(.head_branch == $ref and .created_at >= $start_time and .created_at <= $current_time) | .id') |
| 66 | + if [ -n "$run_id" ]; then |
| 67 | + WORKFLOW_SUB="$ORG_NAME/$REPO_NAME/actions/runs/$run_id" |
| 68 | + WORKFLOW_URL="https://github.com/$WORKFLOW_SUB" |
| 69 | + WORKFLOW_API_URL="https://api.github.com/repos/$WORKFLOW_SUB" |
| 70 | + if [ -n "${GITHUB_OUTPUT:-}" ]; then |
| 71 | + echo "workflow-url=$WORKFLOW_URL" >> "$GITHUB_OUTPUT" |
| 72 | + echo "workflow-api-url=$WORKFLOW_API_URL" >> "$GITHUB_OUTPUT" |
| 73 | + echo "workflow-id=$run_id" >> "$GITHUB_OUTPUT" |
| 74 | + fi |
| 75 | + echo "🎉 Workflow $run_id found at $WORKFLOW_URL" |
| 76 | + break |
| 77 | + fi |
| 78 | + |
| 79 | + # Increment the counter and check if the maximum waiting time is reached |
| 80 | + counter=$((counter + 1)) |
| 81 | + if [ $((counter * $interval)) -ge $((max_find_minutes * 60)) ]; then |
| 82 | + echo "❌ Maximum waiting time for the workflow to be triggered has been reached. Exiting." |
| 83 | + exit 1 |
| 84 | + fi |
| 85 | + |
| 86 | + sleep $interval |
| 87 | + done |
| 88 | +fi |
| 89 | + |
| 90 | +if [ "$wait" = "false" ] || [ "$wait" = "0" ]; then |
| 91 | + exit 0 |
| 92 | +fi |
| 93 | + |
| 94 | +# Wait for the triggered workflow to complete and check its conclusion |
| 95 | +timeout_counter=0 |
| 96 | +while true; do |
| 97 | + echo "⌛ Waiting for the workflow to complete..." |
| 98 | + run_data=$(curl -s -H "Accept: application/vnd.github+json" $GITHUB_AUTH \ |
| 99 | + "https://api.github.com/repos/${ORG_NAME}/${REPO_NAME}/actions/runs/$run_id") |
| 100 | + status=$(echo "$run_data" | jq -r '.status') |
| 101 | + |
| 102 | + if [ "$status" = "completed" ]; then |
| 103 | + conclusion=$(echo "$run_data" | jq -r '.conclusion') |
| 104 | + if [ -n "${GITHUB_OUTPUT:-}" ]; then |
| 105 | + echo "workflow-conclusion=$conclusion" >> "$GITHUB_OUTPUT" |
| 106 | + fi |
| 107 | + if [ "$conclusion" != "success" ]; then |
| 108 | + echo "❌ The workflow has not completed successfully. Exiting." |
| 109 | + exit 1 |
| 110 | + else |
| 111 | + echo "✅ The workflow completed successfully! Exiting." |
| 112 | + break |
| 113 | + fi |
| 114 | + fi |
| 115 | + |
| 116 | + # Increment the timeout counter and check if the timeout has been reached |
| 117 | + timeout_counter=$((timeout_counter + 1)) |
| 118 | + if [ $((timeout_counter * interval)) -ge $((timeout * 60)) ]; then |
| 119 | + echo "❌ Timeout waiting for the workflow to complete. Exiting." |
| 120 | + exit 1 |
| 121 | + fi |
| 122 | + |
| 123 | + sleep $interval |
| 124 | +done |
0 commit comments