-
Notifications
You must be signed in to change notification settings - Fork 674
/
Run_Azdo_Pipeline.ps1
92 lines (75 loc) · 3.12 KB
/
Run_Azdo_Pipeline.ps1
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
# This script is used to run a pipeline in azure devops.
# It uses the run-pipeline rest api. Docs are at https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/run-pipeline?view=azure-devops-rest-7.1
# Required Inputs
# token: The azdo personal access token (PAT) with "Build (Read & execute)" permissions
# You can either pass the token in, or store it in the "AZDO_PAT" environment variable
# organization: The azdo organization where the pipeline lives
# project: The azdo project where the pipeline lives
# pipelineId: The id of the pipeline you want to run.
# This is called "definitionId" in the url. For example, https://devdiv.visualstudio.com/DevDiv/_build?definitionId=14121&_a=summary
# Optional Inputs
# branch: the branch that the pipeline should run against. Defaults to main.
# runtimeParams: A comma separated list of runtime parameters
# Each param should be a key:value pair. For example, "p1Name:p1Value,p2Name:p2Value"
Param(
[string] $token = $env:AZDO_PAT,
[string] $organization = $(throw "organization is required"),
[string] $project = $(throw "project is required"),
[int] $pipelineId = $(throw "pipelineId is required"),
[string] $branch = "main",
[string[]] $runtimeParams
)
# stop on all errors
$ErrorActionPreference = "Stop"
# use strict syntax
Set-StrictMode -Version Latest
if (!$token) {
throw "You must specify a PAT or set the AZDO_PAT environment variable"
}
$user = ""
$restApiUrl = "https://dev.azure.com/$organization/$project/_apis/pipelines/$pipelineId/runs?api-version=7.1-preview.1"
# build the request headers
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $token)))
$headers = @{Authorization = ("Basic {0}" -f $base64AuthInfo) }
# build the resources section of the body, since the branch is required for launching a pipeline
$branchStr = "refs/heads/$branch"
$resources = @{
repositories = @{
self = @{
refName = $branchStr
}
}
}
# if there are runtime params, build the template parameters section
$templateParameters = @{}
if ($runtimeParams) {
foreach ($runtimeParam in $runtimeParams) {
# split on colon to get key value pair
$keyValuePair = $runtimeParam -split ":"
# ignore invalid params
if ($keyValuePair.Length -le 1) {
continue
}
# store the pair in the template parameters dict
$key = $keyValuePair[0]
$value = $keyValuePair[1]
$templateParameters[$key] = $value
}
}
# build the request body
$body = @{}
$body["resources"] = $resources
if ($templateParameters.Count -gt 0) {
$body["templateParameters"] = $templateParameters
}
$bodyAsJson = $body | ConvertTo-Json -Depth 10
# run the request
$result = Invoke-RestMethod -Uri $restApiUrl -Method Post -ContentType "application/json" -Headers $headers -Body $bodyAsJson
$result
if (!$result) {
throw "Invoke-RestMethod failed, check output for details"
}
# the state should always be "inProgress" if the request was successful
if ($result.state -ne "inProgress") {
throw "Pipeline failed to start, check output for details"
}