1+ name : Test Reusable Workflow
2+
3+ on :
4+ workflow_call :
5+ inputs :
6+ target_repo :
7+ description : ' Target repository in owner/repo format (e.g., octocat/hello-world)'
8+ required : true
9+ type : string
10+
11+ workflow_file :
12+ description : ' Workflow file name in the target repo (e.g., deploy.yml)'
13+ required : true
14+ type : string
15+
16+ ref :
17+ description : ' Git ref (branch, tag, or commit SHA) to run the workflow on'
18+ required : true
19+ type : string
20+
21+ use_pat :
22+ description : ' Whether to use PAT instead of GITHUB_TOKEN'
23+ required : false
24+ default : false
25+ type : boolean
26+
27+ wait_timeout_seconds :
28+ description : ' Max seconds to wait for the workflow run to start'
29+ required : false
30+ default : ' 300'
31+ type : string
32+
33+ run_timeout_seconds :
34+ description : ' Max seconds to wait for the workflow run to complete'
35+ required : false
36+ default : ' 3600'
37+ type : string
38+
39+ poll_frequency_seconds :
40+ description : ' How often (in seconds) to poll for workflow status'
41+ required : false
42+ default : ' 10'
43+ type : string
44+
45+ secrets :
46+ PAT :
47+ description : ' Personal Access Token for cross-repo workflow dispatch (optional)'
48+ required : false
49+
50+ jobs :
51+ trigger :
52+ runs-on : ubuntu-latest
53+
54+ steps :
55+ - name : Trigger Target Workflow
56+
57+ run : |
58+ echo "Triggering ${{ inputs.workflow_file }} in ${{ inputs.target_repo }} on ref ${{ inputs.ref }}..."
59+
60+ curl -s -o /dev/null -w "%{http_code}" -X POST \
61+ -H "Accept: application/vnd.github+json" \
62+ -H "Authorization: Bearer ${{ secrets.PAT }}" \
63+ https://api.github.com/repos/${{ inputs.target_repo }}/actions/workflows/${{ inputs.workflow_file }}/dispatches \
64+ -d "{\"ref\":\"${{ inputs.ref }}\"}" | grep -q 204
65+
66+ - name : Wait for Workflow Run
67+ env :
68+ GH_TOKEN : ${{ secrets.PAT }}
69+ WAIT_TIMEOUT : ${{ inputs.wait_timeout_seconds }}
70+ RUN_TIMEOUT : ${{ inputs.run_timeout_seconds }}
71+ POLL_FREQ : ${{ inputs.poll_frequency_seconds }}
72+ TARGET_REPO : ${{ inputs.target_repo }}
73+ WORKFLOW_FILE : ${{ inputs.workflow_file }}
74+ REF : ${{ inputs.ref }}
75+ run : |
76+ OWNER=$(echo "$TARGET_REPO" | cut -d'/' -f1)
77+ REPO=$(echo "$TARGET_REPO" | cut -d'/' -f2)
78+
79+ echo "Waiting up to $WAIT_TIMEOUT seconds for workflow run to appear..."
80+
81+ WAIT_ITER=$((WAIT_TIMEOUT / POLL_FREQ))
82+ for ((i=0; i<WAIT_ITER; i++)); do
83+ set +e
84+
85+ RESPONSE=$(curl -s -H "Authorization: Bearer $GH_TOKEN" \
86+ "https://api.github.com/repos/$OWNER/$REPO/actions/workflows/$WORKFLOW_FILE/runs?branch=$REF&event=workflow_dispatch")
87+
88+ RUN_ID=$(echo "$RESPONSE" | jq -r '.workflow_runs | sort_by(.created_at) | last | .id' 2>/dev/null)
89+
90+ set -e
91+
92+ if [[ "$RUN_ID" != "null" && -n "$RUN_ID" ]]; then
93+ echo "✅ Found workflow run ID: $RUN_ID"
94+ break
95+ fi
96+
97+ TOTAL_WAIT=$((i * POLL_FREQ))
98+
99+ echo "⏳ Waiting for workflow run... (${TOTAL_WAIT}s)"
100+ sleep $POLL_FREQ
101+ done
102+
103+ if [[ -z "$RUN_ID" || "$RUN_ID" == "null" ]]; then
104+ echo "❌ Workflow run did not start in time"
105+ exit 1
106+ fi
107+
108+ echo "Polling workflow run status for up to $RUN_TIMEOUT seconds..."
109+
110+ # Poll until run completes
111+ RUN_ITER=$((RUN_TIMEOUT / POLL_FREQ))
112+ for ((i=0; i<RUN_ITER; i++)); do
113+ # Temporarily disable exit-on-error
114+ set +e
115+
116+ RESPONSE=$(curl -s -H "Authorization: Bearer $GH_TOKEN" \
117+ "https://api.github.com/repos/$OWNER/$REPO/actions/runs/$RUN_ID")
118+
119+ STATUS=$(echo "$RESPONSE" | jq -r '.status' 2>/dev/null)
120+ CONCLUSION=$(echo "$RESPONSE" | jq -r '.conclusion' 2>/dev/null)
121+
122+ set -e
123+
124+ if [[ "$STATUS" == "completed" ]]; then
125+ echo "🎯 Workflow completed with conclusion: $CONCLUSION"
126+
127+ if [[ "$CONCLUSION" == "success" ]]; then
128+ exit 0
129+ else
130+ exit 1
131+ fi
132+ elif [[ -z "$STATUS" || "$STATUS" == "null" ]]; then
133+ echo "⚠️ Warning: Could not get workflow status. Retry in $POLL_FREQ sec..."
134+ else
135+ echo "⏳ Still running ($STATUS)..."
136+ fi
137+
138+ sleep $POLL_FREQ
139+ done
140+
141+ echo "❌ Workflow run did not complete in allotted time."
142+ exit 1
0 commit comments