-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazure-bootstrap.yml
169 lines (143 loc) · 5.55 KB
/
azure-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
# Copyright (c) .NET Foundation and Contributors
# See LICENSE file in the project root for full license information.
trigger:
branches:
include:
- main
- develop
- release-*
paths:
exclude:
- .github_changelog_generator
- .gitignore
- CHANGELOG.md
- CODE_OF_CONDUCT.md
- LICENSE.md
- README.md
- NuGet.Config
- assets/*
- config/*
- .github/*
# PR always trigger build
pr:
autoCancel: true
jobs:
- job: Trigger
displayName: Trigger Azure Dev Ops build and test pipeline
pool:
vmImage: 'ubuntu-latest'
variables:
AZURE_DEVOPS_ORG: nanoFramework
AZURE_DEVOPS_PROJECT: nanoFramework.Protobuf
AZURE_DEVOPS_PIPELINE_ID: 117
AZURE_POOL_NAME: TestStream
steps:
- script: |
# Validate required environment variables
for var in AZURE_DEVOPS_ORG AZURE_DEVOPS_PROJECT AZURE_DEVOPS_PIPELINE_ID AZURE_POOL_NAME; do
if [ -z "${!var}" ]; then
echo "Error: Required environment variable $var is not set"
exit 1
fi
done
# Define the Azure DevOps organization, project, and pipeline
organization="${AZURE_DEVOPS_ORG}"
project="${AZURE_DEVOPS_PROJECT}"
pipelineId="${AZURE_DEVOPS_PIPELINE_ID}"
poolName="${AZURE_POOL_NAME}"
branch="${BUILD_SOURCEBRANCH}"
# Encode the PAT
patEncoded=$(echo -n ":${AZURE_DEVOPS_PAT}" | base64)
# Define the headers
headers=(
-H "Authorization: Basic $patEncoded"
-H "Content-Type: application/json"
)
# 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\""
# 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" '{
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)"
# Define the URL to get the pipeline run status
url="https://dev.azure.com/${AZURE_DEVOPS_ORG}/${AZURE_DEVOPS_PROJECT}/_apis/pipelines/${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)