Powershell to trigger a build in Azure DevOps

前端 未结 2 777
耶瑟儿~
耶瑟儿~ 2021-01-28 08:56

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 work

2条回答
  •  伪装坚强ぢ
    2021-01-28 09:30

    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 .

提交回复
热议问题