Powershell触发Azure DevOps中的构建 [英] Powershell to trigger a build in Azure DevOps
问题描述
我们刚刚将代码从现场TFS迁移到了Azure DevOps.
通过TFS,我使用Powershell脚本来构建和部署应用程序.部署部分仍然可以正常工作,但是我不知道如何触发构建.我在旧的TFS上使用的命令行是:
&"F:\ Program Files(x86)\ Microsoft Visual Studio 12.0 \ Common7 \ IDE \ TFSBuild"启动[存储库URL] [项目]"[内部定义]"
我知道DevOps具有REST API
我需要做的是将Powershell脚本中的构建排队,然后等待构建完成,即相当于我上面的TFSBuild脚本.有人可以帮忙吗?
我最终这样做了,并且有效:
功能队列构建($ definitionName,$ branchName){写主机"Building $ definitionName-$ branchName"$ build =(vsts构建队列--project [project_name] --instance [server_name] --definition-name $ definitionName --branch $ branchName)|出线ConvertFrom-Json#等待构建完成while($ build.status -ne"completed"){开始睡眠-s 5$ build =(vsts build show --id $ build.id --instance [server_name] --project [project_name])|出线ConvertFrom-Json#Write-Host $ build.status}}vsts登录-令牌PAT_created_in_DevOps$ sourceBranch = [branch_name]队列构建[build_definition_name] $ sourceBranch
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?
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
这篇关于Powershell触发Azure DevOps中的构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!