Reputation: 59
I am currently working in azure DevOps and PowerShell. I need to build an azure pipeline through API using PowerShell. I have done something to list my projects in my organization. Kindly help me to build a pipeline through API similarly using PowerShell.
function GetUrl() {
param(
[string]$orgUrl,
[hashtable]$header,
[string]$AreaId
)
# Build the URL for calling the org-level Resource Areas REST API for the RM APIs
$orgResourceAreasUrl = [string]::Format("{0}/_apis/resourceAreas/{1}?api-preview=5.0-preview.1",
$orgUrl, $AreaId)
# Do a GET on this URL (this returns an object with a "locationUrl" field)
$results = Invoke-RestMethod -Uri $orgResourceAreasUrl -Headers $header
# The "locationUrl" field reflects the correct base URL for RM REST API calls
if ("null" -eq $results) {
$areaUrl = $orgUrl
}
else {
$areaUrl = $results.locationUrl
}
return $areaUrl
}
$orgUrl = "https://dev.azure.com/<my organization>/"
$personalToken = "<my path>"
Write-Host "Initialize authentication context" -ForegroundColor Yellow
$token =
[System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
$header = @{authorization = "Basic $token"}
$coreAreaId = "79134c72-4a58-4b42-976c-04e7115f32bf"
$tfsBaseUrl = GetUrl -orgUrl $orgUrl -header $header -AreaId $coreAreaId
$projectsUrl = "$($tfsBaseUrl)_apis/projects?api-version=5.0"
$projects = Invoke-RestMethod -Uri $projectsUrl -Method Get -ContentType "application/json" -
Headers $header
$projects.value | ForEach-Object {
Write-Host $_.name
}
I have my output listing my projects while running this code.
Upvotes: 0
Views: 1615
Reputation: 30313
If you mean "queue a build" for an existing pipeline by "build a pipeline",
you can call below queue build api to queue your pipeline to run.
POST https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=5.1
Below is a simple powershell example to queue a build.
$body = '
{
"definition": {
"id": number
}
}
'
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
$header = @{authorization = "Basic $token"}
$Uri = "https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=5.1"
$buildresponse = Invoke-RestMethod -Method Post -ContentType "application/json" -Uri $Uri -Body $body -Headers $header
write-host $buildresponse
If you mean "create a new pipeline" by "build a pipeline". You can check build definition create Api.
Here is an example to create build definition via powershell scripts.
However it is suggested to create them from azure devops portal, as this thread pointed out the reason.
You can check this quickstart to get started with YAML pipelines and then customize your pipeline . There are some conceptual topics that you may need to customize your pipeline such as variables and tasks, check here for more concepts. You can also follow this learning tutorial provided by Microsoft.
Upvotes: 1
Reputation: 9123
I am not sure what you meant by pipeline here. You can use any of the APIs available in MS Documentation site.
For projects you can use :
function get_projects {
do
{
$uri="https://dev.azure.com/$Org/_apis/projects?continuationToken=$ContinuationToken&api-version=5.1"
$ProjSets=Invoke-WebRequest -Uri $Uri -Method Get -ContentType "application/json" -Headers $header
$continuationToken = $ProjSets.Headers.'x-ms-continuationtoken'
$ProjectSet=$projset.content | ConvertFrom-Json
$projects+=$ProjectSet.value
}while ($continuationToken)
write-host "$continuationToken" -ForegroundColor Cyan
$projects.name
$projects.count
}
get_projects
To get all projects in the organization that the authenticated user has access to, you can use the below:
GET https://dev.azure.com/{organization}/_apis/projects?api-version=5.1
OR this with additional prameters:
GET https://dev.azure.com/{organization}/_apis/projects?stateFilter={stateFilter}&$top={$top}&$skip={$skip}&continuationToken={continuationToken}&getDefaultTeamImageUrl={getDefaultTeamImageUrl}&api-version=5.1
You can use Invoke-RestMethod
or Invoke-WebRequest
to hit those URLs and get the results as well.
Hope it helps.
Upvotes: 0