generated from ni/github-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 2
129 lines (106 loc) · 4.06 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
name: PR Triggered Tests
on:
pull_request_target:
types: [opened, synchronize, reopened]
workflow_dispatch:
jobs:
trigger-azure-pipeline:
runs-on: ubuntu-latest
steps:
- name: Check if authorized to run tests
id: check
run: |
echo "ok=true" >> $GITHUB_OUTPUT
- name: Trigger Azure DevOps Pipeline
id: trigger_pipeline
if: steps.check.outputs.ok == 'true'
run: |
PAT="${{ secrets.AZURE_DEVOPS_PAT }}"
ORG="sergiovelderrain"
PROJECT="sergiovelderrain"
PIPELINE_ID="3"
API_VERSION="7.0"
if [ -z "$PAT" ]; then
echo "AZURE_DEVOPS_PAT is not set or empty!"
exit 1
fi
AUTH=$(echo -n ":$PAT" | base64 | tr -d '\n')
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
PR_HEAD_REF="develop"
else
PR_HEAD_REF="${{ github.event.pull_request.head.ref }}"
fi
JSON_BODY='{
"resources": {
"repositories": {
"self": {
"refName": "refs/heads/'"${PR_HEAD_REF}"'"
}
}
}
}'
echo "Triggering Azure DevOps pipeline with JSON:"
echo "$JSON_BODY"
RESPONSE=$(curl --fail --http1.1 -s -X POST \
-H "Authorization: Basic $AUTH" \
-H "Content-Type: application/json" \
-d "$JSON_BODY" \
"https://dev.azure.com/$ORG/$PROJECT/_apis/pipelines/$PIPELINE_ID/runs?api-version=$API_VERSION")
echo "Pipeline trigger response:"
echo "$RESPONSE"
# Store the entire response as an output for the next step
# Make sure it's JSON to parse it later
if ! echo "$RESPONSE" | grep -q '^{'; then
echo "The response is not JSON, cannot parse with jq."
exit 1
fi
echo "response=$RESPONSE" >> $GITHUB_OUTPUT
- name: Extract run ID
id: extract_run_id
if: steps.check.outputs.ok == 'true'
run: |
# Parse the run_id out of the response from the previous step
RUN_ID=$(echo "${{ steps.trigger_pipeline.outputs.response }}" | jq -r '.id')
if [ "$RUN_ID" == "null" ] || [ -z "$RUN_ID" ]; then
echo "Could not extract run ID from the response."
exit 1
fi
echo "run_id=$RUN_ID" >> $GITHUB_OUTPUT
- name: Wait for Azure DevOps Pipeline to complete
if: steps.check.outputs.ok == 'true'
run: |
PAT="${{ secrets.AZURE_DEVOPS_PAT }}"
ORG="sergiovelderrain"
PROJECT="sergiovelderrain"
API_VERSION="7.0"
RUN_ID=${{ steps.extract_run_id.outputs.run_id }}
AUTH=$(echo -n ":$PAT" | base64 | tr -d '\n')
echo "Checking status for RUN_ID: $RUN_ID"
# Poll every 15s up to 30m for completion
for i in {1..120}; do
STATUS_RESPONSE=$(curl --fail --http1.1 -s \
-H "Authorization: Basic $AUTH" \
"https://dev.azure.com/$ORG/$PROJECT/_apis/pipelines/runs/$RUN_ID?api-version=$API_VERSION" || true)
if ! echo "$STATUS_RESPONSE" | grep -q '^{'; then
echo "The response is not JSON, raw response:"
echo "$STATUS_RESPONSE"
exit 1
fi
STATE=$(echo "$STATUS_RESPONSE" | jq -r '.state')
RESULT=$(echo "$STATUS_RESPONSE" | jq -r '.result')
echo "Pipeline state: $STATE"
echo "Pipeline result: $RESULT"
if [ "$STATE" = "completed" ]; then
if [ "$RESULT" = "succeeded" ]; then
echo "Pipeline completed successfully."
exit 0
else
echo "Pipeline failed or did not succeed. Result: $RESULT"
exit 1
fi
fi
echo "Pipeline not completed yet. Waiting..."
sleep 15
done
echo "Pipeline did not complete within the expected time."
exit 1