Powershell to trigger a build in Azure DevOps

前端 未结 2 778
耶瑟儿~
耶瑟儿~ 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 .

    0 讨论(0)
  • 2021-01-28 09:32

    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 
    
    0 讨论(0)
提交回复
热议问题