gpt4 book ai didi

azure-devops - Azure DevOps 多阶段管道等待批准

转载 作者:行者123 更新时间:2023-12-04 04:18:47 25 4
gpt4 key购买 nike

我在 Azure Git Repos 中使用托管 Azure DevOps 和我们的代码。我们曾经使用“经典”基于 UI 的管道编辑器,但现在正在为我们的构建/发布阶段转向 YAML 模板。

过去我配置了 CI/CD,这样当代码通过拉取请求提交到主分支时,它会触发构建,然后是开发部署。其他发布阶段将在代码移动到该阶段之前等待批准。新版本将取消尚未部署到各自环境的任何先前版本。

在 YAML 部署阶段,我发现当 master 分支触发构建时,它会部署到开发环境,但管道停留在等待状态,因为其他阶段尚未获得批准。因此,运行不会被标记为“完成”,最终其他阶段将超时并被标记为失败。此外,管道的先前运行不会被取消,因此多次运行会堆积在等待状态。

理想情况下,我希望看到的是新构建将取消管道的所有先前运行。我希望在部署到 Development 后将运行标记为“完成”,并且能够在事后手动部署到其他阶段。

有没有其他人想做同样的事情?我只是想这一切都是错误的,应该以不同的方式做吗?

最佳答案

目前 yaml 管道不支持手动部署到阶段。请检查此 open issue .

您可以尝试添加 dependsOncondition对于每个阶段。对于下面的示例 yaml 管道。舞台构建 只有在阶段 之后才会开始运行开始 成功完成,然后 Stage Build 将等待批准,直到 Stage Build 被批准并成功完成才会触发 Stage Release。

您可以定义 pr trigger并设置 autocancel=true (默认为 true)如果新的更改被推送到同一个 pr,则取消之前的运行。

batch trigger 的属性(property)可以达到类似的效果。如果当前 pr 仍在构建中,它将不会开始新的运行。

trigger:
batch: boolean # batch changes if true (the default); start a new build for every push if false
branches:
include:

_
pr:
autoCancel: true
branches:
include:
- master

stages:
- stage: Start
jobs:
- job: A
pool:
vmImage: windows-latest
steps:
- powershell: |
echo "i am job a"

- stage: Build
dependsOn: Start
condition: succeeded()
jobs:
- deployment: Dev
displayName: deploy Web App
pool:
vmImage: 'Ubuntu-16.04'
# creates an environment if it doesn't exist
environment: 'Dev'
strategy:
# default deployment strategy, more coming...
runOnce:
deploy:
steps:
- script: echo "i am dev environment"


- stage: Release
dependsOn: Build
condition: succeeded()
jobs:
- deployment: Environ
displayName: deploy Web App
pool:
vmImage: 'Ubuntu-16.04'
# creates an environment if it doesn't exist
environment: 'Environment'
strategy:
# default deployment strategy, more coming...
runOnce:
deploy:
steps:
- script: echo "i am Environment environment"

Update: Cancel in progress builds via powershell scripts.



您可以在管道顶部添加一个 powershell 任务以调用 build api .下面的脚本获取所有正在进行的构建并取消它们(当前构建除外)。
- task: PowerShell@2

inputs:
targetType: inline
script: |

$header = @{ Authorization = "Bearer $(system.accesstoken)" }
$buildsUrl = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds?api-version=5.1"
echo $buildsUrl
$builds = Invoke-RestMethod -Uri $buildsUrl -Method Get -Header $header

$buildsToStop = $builds.value.Where({ ($_.status -eq 'inProgress') -and ($_.definition.name -eq "$(Build.DefinitionName)") -and ($_.id -ne $(Build.BuildId))})

ForEach($build in $buildsToStop)
{
echo $build.id
$build.status = "cancelling"
$body = $build | ConvertTo-Json -Depth 10
$urlToCancel = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds/$($build.id)?api-version=5.1"
echo $urlToCancel
Invoke-RestMethod -Uri $urlToCancel -Method Patch -ContentType application/json -Body $body -Header $header
}

In order your pipeline to have the permission to cancel the current running build. You need go to your pipeline, click on the 3dots and choose Manage security



enter image description here

Then set the Stop builds permission to Allow for user Project Collection Build Service(projectName),



enter image description here

关于azure-devops - Azure DevOps 多阶段管道等待批准,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59977600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com