首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Azure DevOps中触发构建的Powershell

在Azure DevOps中触发构建的Powershell
EN

Stack Overflow用户
提问于 2019-04-01 18:13:27
回答 2查看 6.4K关注 0票数 0

我们刚刚将代码从现场TFS迁移到Azure DevOps。

使用TFS,我使用powershell脚本构建和部署应用程序。部署部分仍然工作正常,但我不知道如何触发构建。我与旧TFS一起使用的命令行是:

代码语言:javascript
复制
& "F:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\TFSBuild" start [repository URL] [project] "[build definition]" 

我知道DevOps有一个REST https://learn.microsoft.com/en-us/rest/api/azure/devops/build/builds/queue?view=azure-devops-rest-5.0,但是有很多选项,没有示例。我还看到了这篇文章:How to QUEUE a new build using VSTS REST API,但是这里的解决方案并不是等待构建完成,而是使用API4.1--不确定它对DevOps是否有效?我已经在DevOps中配置了构建定义。

在DevOps中配置构建的URL为以下格式:

我所需要的只是从powershell脚本中排队构建并等待构建完成,即相当于我上面的TFSBuild脚本。有人能帮忙吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-02 11:53:43

最后我做了这件事,它起作用了:

代码语言:javascript
复制
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 
票数 1
EN

Stack Overflow用户

发布于 2019-04-02 11:23:38

您可以使用以下脚本触发新生成并等待生成完成。

代码语言:javascript
复制
$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: $($buildDef.id) 
        }, 
        reason: 'Manual', 
        priority: 'Normal',
        parameters: ""{
            'system.debug':'true'
        }""
    }" # 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 
   $buildID = $buildOutput.id

   $buildInfo =  (Invoke-RestMethod -Uri "$buildLog/$buildID"  -UseDefaultCredentials -Method get)  # get build info by build ID
   while($buildInfo.status -eq "inProgress" -or $buildInfo.status -eq "notStarted") # keep checking till build completed
   {
      Write-Output "Build is $($buildInfo.status)... 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.status)" # print build status
   Write-Output "Build Result : $($buildInfo.result)" # print build result
}

要知道,我正在使用TFS 2017,而不是Azure DevOps服务REST 5.0。因此,您可能需要实现一些小的更改。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55461216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档