是否可以运行“最后阶段"?在 Azure Pipelines 中不知道总共有多少阶段? [英] Is it possible to run a "final stage" in Azure Pipelines without knowing how many stages there are in total?

查看:19
本文介绍了是否可以运行“最后阶段"?在 Azure Pipelines 中不知道总共有多少阶段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让一个阶段作为最后一个阶段运行(不包括作业后/报告构建状态)?

问题是:我有一个包含未知数量元素的每个循环,每个元素都有自己的阶段.

所以我们不能只说dependendOn";在最后阶段,因为之前的每个阶​​段在运行时都有一个唯一的名称.

各阶段应按以下顺序排列:

  1. 准备

2 - n) 使用特定的 docker 容器构建/编译/测试

last) 移除所有容器

目前这工作正常,有一个小问题,最后一个阶段将在最后一个 n 阶段结束后运行(完成成功或失败条件)问题是,当 n 阶段中的最后一个比其他所有阶段都快时.

Remove container 阶段是分离的,因为我们遇到了问题,当所有 n-stages 尝试同时删除它们的容器时,docker 无法处理.

管道在 yaml 文件上运行

解决方案

是否可以在不知道总共有多少阶段的情况下在 Azure Pipelines 中运行最终阶段"?

答案是肯定的.

解决方案的主要思想是:

在最后阶段(移除所有容器),在移除所有容器之前添加一个Inline powershell任务.此任务将调用 REST API

注意:你需要在最新阶段添加Inline powershell任务,我在stage中间添加了它,以清楚地表明inline powershell任务将在其他阶段之前监控整个发布完成了.

更新:

<块引用>

我尝试将其转换为 Build Pipelines 而不是 Release管道.但是我找不到任何关于状态的 API 参考构建阶段.阶段的文档是空的docs.microsoft.com/en-us/rest/api/azure/devops/build/... 和邮递员不想过滤构建区域中的定义

如果您想将其用于构建管道,目前没有记录在案的 REST API 来获取阶段状态.但是我们可以在浏览器中按F12,然后选择Network 来捕获请求以获得阶段结果.您可以从响应正文中捕获结果.但不同阶段的结果用不同的数字表示,即 0-> 未开始、1-> 进行中、2-> 完成等:

https://dev.azure.com/<YourOrgName>/LeoTest/_build/results?buildId=6522&__rt=fps&__ver=2

现在,我们可以用同样的思路来确定 state 的值来解决你的需求.代码我就不在这里重复分享了,如果你还有什么问题,请告诉我.

Is there a way to let a stage run as the very last stage (not including the post-job/ Report build status)?

The thing is: I have an each-loop with an unkown amount of elements, every element gets their own stage.

so we can't just say "dependendOn" on the final stage, because every previous stage has an unique name given at runtime.

The stages should sequence as followed:

  1. Prepare

2 - n) Build / Compile / Test with specific docker container

last) Remove all containers

Currently this works fine, with the small issue, that the last stage will run after the last of the n-Stages is over (done with succeededOrFailed condition) The problem is, when the last of the n-Stages is faster than all the others.

The Remove container stage is separated, because we ran into issues, when all the n-stages tried removing their containers at the same time, which docker can not handle.

The pipeline runs on yaml files

解决方案

Is it possible to run a "final stage" in Azure Pipelines without knowing how many stages there are in total?

The answer is yes.

The main idea of the solution is:

In the last stage (Remove all containers), add a Inline powershell task before Remove all containers. This task will call the REST API Definitions - Get to monitor whether all the stages in the current release pipeline have inprocess and queue states. If so, wait for 30 seconds, and then loop again until all other stages in the current release pipeline have no inprocess and queue states. Then next Remove all containers task will be executed.

The scripts:

For example, I add Inline powershell task in my Dev stage with following scripts:

$connectionToken="$(Your PAT here)"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
Start-sleep -Seconds 20 # In case this stage run as first stage.
$success = $false
$count = 0

do{
    try{

        $stageurl2 = "https://vsrm.dev.azure.com/{org name}/{project name}/_apis/release/deployments?definitionId={release definition ID}&deploymentStatus=inProgress&api-version=6.0"
        $stageinfo2 = (Invoke-RestMethod -Uri $stageurl2 -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}) 

        $stageInProgressNum= $stageinfo2.count

        $stageurl3 = "https://vsrm.dev.azure.com/{org name}/{project name}/_apis/release/deployments?definitionId={release definition ID}&deploymentStatus=notDeployed&api-version=6.0"
        $stageinfo3 = Invoke-RestMethod -Method Get -ContentType application/json -Uri $stageurl3 -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

        $stageInQueuedNum = $stageinfo3.count

        Write-Host "stage In Progress Num is = $stageInProgressNum"

        Write-Host "stage In Queued Num is = $stageInQueuedNum"

        if($stageInProgressNum -ne 1) {     #The value set to 1 is because the ststus of curret stage is in inProgress when this powershell scripts is executed.      
            Write-output "There is/are $stageInProgressNum Deployment In Progress, Waiting for it to finish!"
            Write-output "Next attempt in 30 seconds"
            Start-sleep -Seconds 30         
      } else {           
            Write-Host "No Current Deployment in Progress"
            if($stageInQueuedNum -ne 0) {
            write-output "There is/are $stageInQueuedNum Queued deployments"
            Write-output "Next attempt in 30 seconds"
            Start-sleep -Seconds 30
            }
            else{
            write-output "No Queued deployments, starting release"
            $success = $true     
            }     
      }
    }
    catch{
        Write-output "catch - Next attempt in 30 seconds"
        write-output "1"
        Start-sleep -Seconds 30
      # Put the start-sleep in the catch statemtnt so we
      # don't sleep if the condition is true and waste time
    }
   
    $count++
   
}until($count -eq 2000 -or $success)
if(-not($success)){exit}

The test result:

Dev will continue to check until all other stages are complete:

Note: You need add the Inline powershell task in the latest stage, I added it in the middle of the stages to clearly show that the inline powershell task will monitor the entire release before the other stages are completed.

Update:

I tried to convert this to Build Pipelines instead of Release Pipelines. However I couldn't find any API reference to the status of Build-Stages. The doc for stages is empty docs.microsoft.com/en-us/rest/api/azure/devops/build/… And postman doesn't want to filter on the definitions in build area

If you want use this for build pipeline, there is no documented REST API to get the stage status at this moment. But we could press F12 in the browser then select Network to capture the request to get the stage result. You can capture the result from the response body. But different stage results are represented by different numbers i.e 0->Not start, 1->inprocess, 2->complete, etc:

https://dev.azure.com/<YourOrgName>/LeoTest/_build/results?buildId=6522&__rt=fps&__ver=2

Now, we can use the same idea to determine the value of state to solve your needs. I will not share the code repeatedly here, if you have any further questions, please let me know.

这篇关于是否可以运行“最后阶段"?在 Azure Pipelines 中不知道总共有多少阶段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆