如何在Azure DevOps YAML管道中发送构建后消息? [英] How to send post build message in azure DevOps YAML pipeline?
问题描述
我试图在Azure DevOps YAML管道中完成/失败作业后发送生成后的Slack消息.但是看来我找不到合适的条件设置.
I'm trying to send a post-build Slack message after the job is done/failed in a Azure DevOps YAML pipeline. But it seems I can't find a proper condition setting.
基本上,我分为三个阶段:测试,构建和通知.
Basically, I have three stages: test, build, and notification.
我终于尝试了以下操作,但是dependencies.UnitTest.result
返回null
,所以没有给我成功或失败.
I tried the following at last, but dependencies.UnitTest.result
returns null
, so not giving me Succeeded or Failed.
我也尝试了很多不同的条件,但是它们没有用.例如,普通的succeeded()
和failed()
没有依赖关系,或者succeeded('Test')
作为阶段级别,或者succeeded('UnitTest')
作为作业级别.
I also tried a bunch of different conditions but they didn't work. For example, a plain succeeded()
and failed()
without dependencies, or succeeded('Test')
as stage level or succeeded('UnitTest')
as job level.
在大多数情况下,即使在测试阶段失败,他们也会发送成功消息,或者在succeeded()
或failed()
In most cases, they send the success message even if they failed at the Test stage, or a syntax error for job names as argument in succeeded()
or failed()
发送诸如Jenkins这样的构建后消息的适当条件是什么?
What is the proper condition to send a post-build message like Jenkins?
stages:
- stage: Test
jobs:
- job: UnitTest
steps:
- script: echo UnitTest
- script: exit 1
- stage: Build
jobs:
- job: Build
steps:
- script: echo Build
- stage: Notify
dependsOn:
- Test
- Build
condition: succeededOrFailed()
jobs:
- job: Succeed
condition: eq(dependencies.UnitTest.result, 'Succeeded')
steps:
- script: echo Succeed #(slack)
- job: Fail
condition: eq(dependencies.UnitTest.result, 'Failed')
steps:
- script: echo Fail #(slack)
-编辑---
MS支持的已确认作业在多个阶段不能作为yaml语法本身来支持.
--- EDIT ---
MS support confirmed jobs in multi stages can't support as yaml syntax itself.
与预期的原始流程不同,但是您可以按以下方式将成功和失败分成不同的阶段. (如果您希望为每个作业使用不同的消息,则可能仅增加通知的阶段数.)
Not same as the original flow as expected, but you can split succeed and fail into different stages as follow. (It may increase quite number of stages just for the notification if you want different message for each jobs..)
...
- stage: Notify_Succeeded
condition: succeeded()
jobs:
- job: Succeed
steps:
- script: echo Succeed #(slack)
- stage: Notify_Fail
condition: failed()
jobs:
- job: Fail
steps:
- script: echo Fail #(slack)
推荐答案
可以,但是必须使用REST API.通过下面的YAML,您将获得您所描述的内容:
It is possible but you have to use REST API. With below YAML you will get what you described:
variables:
orgName: 'thecodemanual'
stages:
- stage: Test
jobs:
- job: UnitTest
steps:
- script: echo UnitTest
- script: exit 1
- stage: Build
jobs:
- job: Build
steps:
- script: echo Build
- stage: Notify
dependsOn:
- Test
- Build
condition: succeededOrFailed()
jobs:
- job: InitialJob
condition: always()
steps:
- pwsh: |
$url = "https://dev.azure.com/$(orgName)/$(System.TeamProject)/_apis/build/builds/$(Build.BuildId)/timeline?api-version=5.1"
$timeline = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
Write-Host "Pipeline = $($timeline | ConvertTo-Json -Depth 100)"
$test = $timeline.records | where { $_.identifier -eq "Test.UnitTest" }
$result = $test.result
Write-Host "##vso[task.setvariable variable=testResult;isOutput=true]$result"
name: initial
env:
SYSTEM_ACCESSTOKEN: $(system.accesstoken)
- job: Succeed
dependsOn: InitialJob
condition: eq(dependencies.InitialJob.outputs['initial.testResult'], 'succeeded')
steps:
- script: echo Succeed #(slack)
- job: Fail
dependsOn: InitialJob
condition: eq(dependencies.InitialJob.outputs['initial.testResult'], 'failed')
steps:
- script: echo Fail #(slack)
让我解释一下我在上面所做的事情:
Let me explain what I did above:
- 我调用了REST API,以获取上一阶段的步骤结果(因为目前无法获得任务结果)
- 我将该步骤的状态分配给输出变量
- 我将此变量用作运行特定作业
Succeed
或Fail
的条件
- I made a call to REST API to get result of step from previous stage (as the the moment is not possible to get task result)
- I assign status of that step to output variable
- I used this variable as condition to run specific job
Succeed
orFail
备注:在运行代码之前,请将orgName更改为您的
Remark: before you run code, please change orgName to your.
编辑
下面的url返回您的构建详细信息.但是您不会在其中找到有关特定任务或阶段的信息.但是您会在那里找到时间轴的网址.
Below url return you details for your build. But you will not fine there information about specific tasks or stages. But you will get there url for a timeline.
https://dev.azure.com/$(orgName)/$(System.TeamProject)/_apis/build/builds/$(Build.BuildId)?api-version=5.1
此REST端点将为您返回一个时间表,其中包含有关任务和阶段的详细信息.
This REST endpoint will return you a timeline which includes details for tasks and stages.
https://dev.azure.com/$(orgName)/$(System.TeamProject)/_apis/build/builds/$(Build.BuildId)/timeline?api-version=5.1
这篇关于如何在Azure DevOps YAML管道中发送构建后消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!