-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbootstrap.yml
213 lines (176 loc) · 7.51 KB
/
bootstrap.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
parameters:
- name: AZURE_DEVOPS_ORG
displayName: Azure DevOps organization
type: string
default: nanoFramework
- name: AZURE_DEVOPS_PROJECT
displayName: Azure DevOps project
type: string
- name: AZURE_DEVOPS_PIPELINE_ID
displayName: Azure DevOps pipeline ID
type: number
- name: AZURE_POOL_NAME
displayName: Azure DevOps pool name
type: string
default: TestStream
steps:
- checkout: self
clean: true
fetchDepth: 1
- script: |
# Validate required environment variables
for var in AZURE_DEVOPS_PAT BUILD_SOURCEBRANCH; do
if [ -z "${!var}" ]; then
echo "Error: $var environment variable is not set"
exit 1
fi
done
# Define the Azure DevOps organization, project, and pipeline
organization="${{ parameters.AZURE_DEVOPS_ORG }}"
project="${{ parameters.AZURE_DEVOPS_PROJECT }}"
pipelineId="${{ parameters.AZURE_DEVOPS_PIPELINE_ID }}"
poolName="${{ parameters.AZURE_POOL_NAME }}"
branch="${BUILD_SOURCEBRANCH}"
# Replace 'merge' with 'head' in the $branch variable
branch=$(echo "$branch" | sed 's/merge/head/')
echo "Updated branch: $branch"
# if this is a PR build, get the PR title
if [[ $branch == refs/pull/* ]]; then
# Fetch the PR details from GitHub to get the title
# Extract the PR ID from the branch name
auth=$(echo -n ":$GITHUBTOKEN" | base64)
auth="basic $auth"
prUrl="https://api.github.com/repos/$BUILD_REPOSITORY_NAME/pulls/$SYSTEM_PULLREQUEST_PULLREQUESTNUMBER"
response=$(curl -s -w "%{http_code}" -H "Authorization: $auth" -X GET "$prUrl")
http_code=${response: -3}
content=${response::-3}
if [ $http_code -eq 200 ]; then
prDescription=$(echo "$content" | jq -r '.body')
if [ "$prDescription" = "null" ]; then
echo "Error: Failed to extract PR description from response"
exit 1
fi
else
echo "Error: Failed to fetch PR details. Status code: $http_code"
echo "Response: $content"
exit 1
fi
echo "PR description: $prDescription"
# Get the PR creator
prCreator=$(echo "$content" | jq -r '.user.login')
echo "PR Creator: $prCreator"
# Check if the PR creator is nfbot AND description includes the version update tag,
# and set skipHardwareTest accordingly
# This is to skip the hardware test for update PRs created by nfbot
if [[ "$prCreator" == "nfbot" && "$prDescription" == *"[version update]"* ]]; then
skipHardwareTest=true
else
skipHardwareTest=false
fi
else
# build from a branch, not a PR
echo "This is not a PR build"
# don't run hardware tests on non-PR builds
skipHardwareTest=true
fi
echo "Skip Hardware Test: $skipHardwareTest"
# Encode the PAT
patEncoded=$(echo -n ":${AZURE_DEVOPS_PAT}" | base64)
# Define the headers
headers=(
-H "Authorization: Basic $patEncoded"
-H "Content-Type: application/json"
)
if [ "$skipHardwareTest" = "true" ]; then
# Set the capabilityNames to none
capabilityNames="none"
else
# Get the pool ID
url="https://dev.azure.com/${organization}/_apis/distributedtask/pools?poolName=${poolName}&api-version=7.1"
AZP_POOL_AGENTS=$(curl -s "${headers[@]}" -X GET "$url")
poolId=$(echo "$AZP_POOL_AGENTS" | jq -r '.value[0].id')
echo "Pool ID: $poolId"
# Define the URL to get all agents in the pool
url="https://dev.azure.com/${organization}/_apis/distributedtask/pools/${poolId}/agents?includeCapabilities=true&api-version=7.1"
response=$(curl -s -w "%{http_code}" "${headers[@]}" -X GET "$url")
http_code=${response: -3}
content=${response::-3}
if [ $http_code -eq 200 ]; then
# Extract all userCapabilities names for online and enabled agents as a unique list
capabilityNames=$(echo "$content" | jq -r '[.value[] | select(.status == "online" and .enabled == true) | .userCapabilities | keys] | unique | flatten | join("\n- ")')
else
echo "Failed to retrieve agent capabilities. HTTP Status Code: $http_code"
echo "Response: \"$content\""
exit 1
fi
echo "Unique userCapabilities names: \"$capabilityNames\""
fi
# Prepare the parameters
parametersJson=$(jq -n --arg appComponents "- $capabilityNames" '{templateParameters: {appComponents: $appComponents}}')
echo "Parameters: \"$parametersJson\""
echo "Branch for PR: \"$branch\""
# Define the request body
bodyJson=$(jq -n --argjson parameters "$parametersJson" --arg branch "$branch" --arg prTitle "$prTitle" '{
resources: {
repositories: {
self: {
refName: $branch
}
}
},
templateParameters: $parameters.templateParameters
}')
echo "Request body: \"$bodyJson\""
# Define the URL
url="https://dev.azure.com/${organization}/${project}/_apis/pipelines/${pipelineId}/runs?api-version=7.1"
# Trigger the pipeline
response=$(curl -s -w "%{http_code}" "${headers[@]}" -X POST -d "$bodyJson" "$url")
http_code=${response: -3}
content=${response::-3}
if [ $http_code -eq 200 ]; then
run_id=$(echo "$content" | jq -r '.id')
echo "Pipeline triggered successfully. Run ID: $run_id"
echo "##vso[task.setvariable variable=run_id]$run_id"
else
echo "Failed to trigger pipeline. HTTP Status Code: $http_code"
echo "Response: $content"
exit 1
fi
displayName: 'Trigger Azure DevOps Pipeline'
env:
BUILD_SOURCEBRANCH: $(Build.SourceBranch)
AZURE_DEVOPS_PAT: $(AZURE_DEVOPS_PAT)
- script: |
echo "Pipeline to monitor Run ID: $(run_id)"
echo "Pipeline direct link: https://dev.azure.com/${{ parameters.AZURE_DEVOPS_ORG }}/${{ parameters.AZURE_DEVOPS_PROJECT }}/_build/results?buildId=$(run_id)"
# Define the URL to get the pipeline run status
url="https://dev.azure.com/${{ parameters.AZURE_DEVOPS_ORG }}/${{ parameters.AZURE_DEVOPS_PROJECT }}/_apis/pipelines/${{ parameters.AZURE_DEVOPS_PIPELINE_ID }}/runs/$(run_id)?api-version=7.1"
# Loop to monitor the pipeline run status
while true; do
response=$(curl -s -w "%{http_code}" -H "Authorization: Basic $(echo -n ":${AZURE_DEVOPS_PAT}" | base64)" -X GET "$url")
http_code=${response: -3}
content=${response::-3}
if [ $http_code -eq 200 ]; then
state=$(echo "$content" | jq -r '.state')
result=$(echo "$content" | jq -r '.result')
echo "Pipeline run state: $state"
if [ "$state" == "completed" ]; then
echo "Pipeline run completed with result: $result"
if [ "$result" == "succeeded" ]; then
exit 0
else
exit 1
fi
fi
else
echo "Failed to get pipeline run status. HTTP Status Code: $http_code"
echo "Response: $content"
exit 1
fi
# Wait for a while before checking again
sleep 30
done
displayName: 'Monitoring Azure DevOps pipeline'
env:
run_id: $(run_id)
AZURE_DEVOPS_PAT: $(AZURE_DEVOPS_PAT)