Powershell to trigger a build in Azure DevOps

半腔热情 提交于 2021-02-05 09:59:10

问题


We have just migrated our code from on-site TFS to Azure DevOps.

With TFS, I use a powershell script to build and deploy the application. The deployment part still works fine, but I don't know how to trigger the build. The command line I used with the old TFS is:

& "F:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\TFSBuild" start [repository URL] [project] "[build definition]" 

I know that DevOps has a REST API https://docs.microsoft.com/en-us/rest/api/azure/devops/build/builds/queue?view=azure-devops-rest-5.0 but there are many options and no examples there. I also saw this post: How to QUEUE a new build using VSTS REST API but the solution there does not wait for the build to finish and it uses API 4.1 - not sure if it's valid for DevOps? I already configured the build definitions in DevOps.

The URL to where the build is configured in DevOps is of this format:

All I need is to queue a build from a powershell script and wait for the build to complete, i.e. equivalent of my TFSBuild script above. Can someone help please?


回答1:


You can use the following script which trigger new build and waiting till build completed.

$tfsUrl = "http://{tfsServer}:{Port}/{Organization}/{Collection}/{Project}"
$buildsURI = $tfsUrl + '/_apis/build/builds?api-version=2.0'
$BuildDefsUrl = $tfsUrl + '/_apis/build/definitions?api-version=2.0'
$buildLog =  "$tfsUrl/_apis/build/builds"

$allbuildDefs = (Invoke-RestMethod -Uri ($BuildDefsUrl) -Method GET -UseDefaultCredentials).value | Where-Object {$_.name -eq "BuildName"} | select id,name ## get all relevant builds

foreach ($build in $allbuildDefs)
{
   $body = '{ "definition": { "id": '+ $build.id + '}, reason: "Manual", priority: "Normal"}' # build body

   Write-Output "Queueing $($build.name)" # print build name

   $buildOutput = Invoke-RestMethod -Method Post -Uri $buildsURI -UseDefaultCredentials -ContentType 'application/json' -Body $body -Verbose # trigger new build 

   $allBuilds = (Invoke-RestMethod -Uri $buildsURI -Method get -UseDefaultCredentials).value # get all builds

   $buildID = ($allBuilds | where {$_.definition.name -eq $build.name })[0].id # get first build id 

   $buildInfo =  (Invoke-RestMethod -Uri "$buildLog/$buildID"  -UseDefaultCredentials -Method get)  # get build info by build ID
   while($buildInfo.status -eq "inProgress") # keep checking till build completed
   {
      Write-Output "Sleep for 5 seconds.."
      Start-Sleep -Seconds 5 # Start sleep for 5 seconds
      $buildInfo =  (Invoke-RestMethod -Uri "$buildLog/$buildID"  -UseDefaultCredentials -Method get) ## get status 
   }

   Write-Output "Build Status : $($buildInfo.result)" # print build result
}

Be aware that i'm working with TFS 2017 and not Azure DevOps Services REST API 5.0.So,there might be some small changes you need to implement .




回答2:


I ended up doing this and it works:

Function Queue-Build ($definitionName, $branchName)
{
    Write-Host "Building $definitionName - $branchName"
    $build = (vsts build queue --project [project_name] --instance [server_name] --definition-name $definitionName --branch $branchName) | Out-String | ConvertFrom-Json

    #wait for the build to complete
    while ($build.status -ne "completed") {
        Start-Sleep -s 5
        $build = (vsts build show --id $build.id --instance [server_name] --project [project_name]) | Out-String | ConvertFrom-Json
        #Write-Host $build.status
    }
}

vsts login --token PAT_created_in_DevOps

$sourceBranch = [branch_name]
Queue-Build [build_definition_name] $sourceBranch 


来源:https://stackoverflow.com/questions/55461216/powershell-to-trigger-a-build-in-azure-devops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!