generated from ni/github-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 2
196 lines (162 loc) · 6.77 KB
/
trigger-azure-pipeline.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
name: PR Triggered Tests
on:
pull_request_target:
types: [opened, synchronize, reopened]
workflow_dispatch:
permissions:
contents: read
env:
DEBUG_MODE: "false" # Set to "false" to reduce verbosity
jobs:
trigger-azure-pipeline:
runs-on: ubuntu-latest
steps:
- name: Trigger Azure DevOps Pipeline
id: trigger
uses: Azure/pipelines@v1.2
with:
azure-devops-project-url: 'https://dev.azure.com/sergiovelderrain/sergiovelderrain'
azure-pipeline-name: 'ni.labview-icon-editor-test'
azure-devops-token: ${{ secrets.AZURE_DEVOPS_PAT }}
# - name: Wait for pipeline to start
# run: |
# echo "Waiting 2 minutes to allow the pipeline to start..."
# sleep 120
- name: Find the in-progress pipeline run
id: find-run
env:
ORGANIZATION: sergiovelderrain
PROJECT_NAME: sergiovelderrain
PIPELINE_DEFINITION_ID: "3" # Replace with your pipeline definition ID
DEBUG_MODE: ${{ env.DEBUG_MODE }}
AZURE_DEVOPS_PAT: ${{ secrets.AZURE_DEVOPS_PAT }}
run: |
set -euo pipefail
if [ "$DEBUG_MODE" = "true" ]; then
echo "::debug::Debug mode enabled."
fi
if [ -z "${AZURE_DEVOPS_PAT}" ]; then
echo "Error: AZURE_DEVOPS_PAT not set." >&2
exit 1
fi
# Use printf and base64 -w 0 to avoid newlines in the encoded value.
AUTH_HEADER=$(printf ":%s" "${AZURE_DEVOPS_PAT}" | base64 -w 0)
# Use queryOrder=queueTimeDescending to ensure we get the newest runs first
API_URL="https://dev.azure.com/${ORGANIZATION}/${PROJECT_NAME}/_apis/build/builds?definitions=${PIPELINE_DEFINITION_ID}&\$top=10&queryOrder=queueTimeDescending&api-version=6.0"
MAX_ATTEMPTS=10
SLEEP_SECONDS=5
ATTEMPT=1
BUILD_ID=""
while [ $ATTEMPT -le $MAX_ATTEMPTS ]; do
if [ "$DEBUG_MODE" = "true" ]; then
echo "::debug::Attempt $ATTEMPT/$MAX_ATTEMPTS to find in-progress build from $API_URL"
fi
rm -f response.json || true
if ! wget --quiet --header="Authorization: Basic ${AUTH_HEADER}" -O response.json "$API_URL"; then
echo "Error: Failed to fetch build runs." >&2
exit 1
fi
if [ "$DEBUG_MODE" = "true" ]; then
echo "::debug::Full response:"
cat response.json | jq .
fi
BUILD_ID=$(jq -r '.value[] | select(.status == "inProgress") | .id' response.json | head -n 1 || true)
if [ -n "$BUILD_ID" ]; then
echo "BUILD_ID=$BUILD_ID" >> $GITHUB_ENV
if [ "$DEBUG_MODE" = "true" ]; then
echo "::debug::Found in-progress BUILD_ID=$BUILD_ID"
fi
break
else
if [ "$DEBUG_MODE" = "true" ]; then
echo "::debug::No in-progress build found this attempt."
fi
echo "No in-progress build found yet. Waiting $SLEEP_SECONDS seconds before retry..."
sleep $SLEEP_SECONDS
ATTEMPT=$((ATTEMPT+1))
fi
done
if [ -z "$BUILD_ID" ]; then
echo "Error: No in-progress build found after multiple attempts." >&2
cat response.json
exit 1
fi
- name: Poll pipeline status until completion
id: poll-status
env:
ORGANIZATION: sergiovelderrain
PROJECT_NAME: sergiovelderrain
DEBUG_MODE: ${{ env.DEBUG_MODE }}
AZURE_DEVOPS_PAT: ${{ secrets.AZURE_DEVOPS_PAT }}
run: |
set -euo pipefail
if [ "$DEBUG_MODE" = "true" ]; then
echo "::debug::Debug mode enabled for polling."
fi
if [ -z "${AZURE_DEVOPS_PAT}" ]; then
echo "Error: AZURE_DEVOPS_PAT not set." >&2
exit 1
fi
if [ -z "${BUILD_ID:-}" ]; then
echo "Error: BUILD_ID not set." >&2
exit 1
fi
AUTH_HEADER=$(printf ":%s" "${AZURE_DEVOPS_PAT}" | base64 -w 0)
STATUS_URL="https://dev.azure.com/${ORGANIZATION}/${PROJECT_NAME}/_apis/build/builds/${BUILD_ID}?api-version=6.0"
TIMELINE_URL="https://dev.azure.com/${ORGANIZATION}/${PROJECT_NAME}/_apis/build/builds/${BUILD_ID}/timeline?api-version=6.0"
MAX_ATTEMPTS=1000
SLEEP_SECONDS=10
ATTEMPT=1
while [ $ATTEMPT -le $MAX_ATTEMPTS ]; do
if [ "$DEBUG_MODE" = "true" ]; then
echo "::debug::Attempt $ATTEMPT of $MAX_ATTEMPTS"
fi
rm -f status.json || true
if ! wget --quiet --header="Authorization: Basic ${AUTH_HEADER}" -O status.json "$STATUS_URL"; then
echo "Error: Failed to fetch build status." >&2
exit 1
fi
if [ "$DEBUG_MODE" = "true" ]; then
echo "::debug::Pipeline status response:"
cat status.json | jq .
fi
STATUS=$(jq -r '.status // empty' status.json)
RESULT=$(jq -r '.result // empty' status.json)
if [ -z "$STATUS" ]; then
echo "Error: Status not found in pipeline response." >&2
if [ "$DEBUG_MODE" = "true" ]; then
echo "::debug::Parsed response:"
cat status.json
fi
exit 1
fi
echo "Current Status: $STATUS"
if [ "$DEBUG_MODE" = "true" ]; then
echo "::debug::Current Result: $RESULT"
fi
# Fetch and report timeline for more detailed feedback
rm -f timeline.json || true
if ! wget --quiet --header="Authorization: Basic ${AUTH_HEADER}" -O timeline.json "$TIMELINE_URL"; then
echo "Error: Failed to fetch timeline." >&2
exit 1
fi
# Print detailed timeline feedback
echo "---- Pipeline Detailed Timeline ----"
cat timeline.json | jq '.records[] | {Type: .type, Name: .name, State: .state, Result: .result, StartTime: .startTime, FinishTime: .finishTime, Issues: .issues}'
echo "-----------------------------------"
if [ "$STATUS" = "completed" ]; then
# If completed, check the final result
break
fi
sleep $SLEEP_SECONDS
ATTEMPT=$((ATTEMPT+1))
done
if [ "$STATUS" != "completed" ]; then
echo "Error: Build not completed within the timeout." >&2
exit 1
fi
if [ "$RESULT" != "succeeded" ]; then
echo "Error: Build completed but did not succeed. Result: $RESULT" >&2
exit 1
fi
echo "Build succeeded!"