如何在 azure devops 上使用触发管道信息标记触发管道
how to tag triggered pipeline with triggering pipeline info on azure devops
我有在管道资源上触发的发布管道,但希望发布管道被标记为触发管道(构建)信息,这样我们就可以过滤什么时候部署的内容。
我正在尝试使用日志命令使用 apiname 变量标记发布管道,但我无法看到它们或使用它们进行过滤。
下面是我的发布管道代码
resources:
pipelines:
- pipeline: pipeline1
project: appcom
source: pipeline-api
trigger:
branches:
- develop
- feat/*
- pipeline: pipeline2
project: appcom
source: pipeline2-api
trigger:
branches:
- develop
- feat/*
variables:
- name: alias
value: $(resources.triggeringAlias)
stages:
- stage: ScanImage
jobs:
- job: ScanImage
pool:
vmImage: 'ubuntu-16.04'
steps:
- script: echo $(alias)
- task: Bash@3
inputs:
targetType: 'inline'
script: |
if [ "$(alias)" == "pipeline1" ]; then
echo "##vso[task.setvariable
variable=apiname]$(resources.pipeline.pipeline1.pipelineName)"
echo "##vso[task.setvariable
variable=dockertag]$(resources.pipeline.pipeline1.sourceCommit)
| cut -c -7"
echo "##vso[task.setvariable variable=helmpath]P02565Mallorca/pipeline1-api"
elif [ "$(alias)" = "pipeline2" ]; then
echo "##vso[task.setvariable
variable=apiname]$(resources.pipeline.pipeline2.pipelineName)"
echo "##vso[task.setvariable
variable=dockertag]$(resources.pipeline.pipeline2.sourceCommit)
| cut -c -7"
echo "##vso[task.setvariable variable=helmpath]P02565Mallorca/pipeline2-api"
fi
- script: echo "##vso[build.addbuildtag]$(apiname)"
发布管道运行:
[![在此处输入图片描述][1]][1]
过滤时不显示任何标签,只是说没有标签
下面是设置标签的bash脚本的截图
[![在此处输入图片描述][2]][2]
但无法过滤正在使用此标签。
[1]: https://i.stack.imgur.com/BVjGS.png
[2]: https://i.stack.imgur.com/YaXJJ.png
您可以使用 Rest Api 更新标签:Tags - Add Build Tag
带有 powershell 任务的 Yaml 示例:
pool:
vmImage: 'ubuntu-16.04'
steps:
- task: PowerShell@2
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
inputs:
targetType: 'inline'
script: |
$user = ""
$token = $env:SYSTEM_ACCESSTOKEN
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$org = "$(System.CollectionUri)"
$teamProject = "$(System.TeamProject)"
$buildId = "$(Build.BuildId)"
$tagName = "test_tag"
$restApiUpdateBuild = "$org/$teamProject/_apis/build/builds/$buildId/tags/$tagName`?api-version=6.0"
function InvokePutReques ($PutUrl)
{
return Invoke-RestMethod -Uri $PutUrl -Method Put -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
}
$result = InvokePutReques $restApiUpdateBuild
pwsh: true
脚本中的最后一条指令 - script: git tag $(apiname)
可能不起作用,因为您在构建代理存储库上创建了标签。您必须发布您的标签。
检查此文档以Run Git commands in a script. Publish tags to a remote repo: How do you push a tag to a remote repository using Git?
is it possible to tag with apiname variable in above pipeline?
根据您的 yaml 示例,您已在 PowerShell 任务中设置了 apiname 变量。
因此您可以在下一个任务中直接使用格式为 $(apiname)
的变量
https://dev.azure.com/{organizationname}/{projectname}/_apis/build/builds/$(build.buildid)/tags/$(apiname)?api-version=6.0
这是一个例子:
steps:
- script: echo $(alias)
- task: Bash@3
....
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$token = "PAT"
$url="https://dev.azure.com/{organizationname}/{projectname}/_apis/build/builds/$(build.buildid)/tags/$(apiname)?api-version=6.0"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Put -ContentType application/json
这是一篇关于 the Tags - Add Build Tag and create PAT
的文档
注意 PAT 令牌需要具有范围 vso.build_execute
更新:
使用Bash脚本运行其余API:
curl -X POST \
-u :{PAT} https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/build/builds/$(build.buildid)/tags?api-version=6.0 \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d ' ["$(apiname)"]'
更新2:
curl -X POST \
-u :$(System.AccessToken) https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/build/builds/$(build.buildid)/tags?api-version=6.0 \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d ' ["$(apiname)"]'
注意:您需要select选项:允许脚本在代理作业中访问 OAuth 令牌。
如果报无权限的错误,需要为projectname build service(organizationname)[=61]授予Manage build queue权限=] 账户
更新3:
您也可以使用 logging command 来设置构建标签:
这是 bash 脚本:
echo "##vso[build.addbuildtag]$(apiname)"
我不需要将任何内容转换为 base64 以获得身份验证信息,这很有效:
- powershell: |
$url="https://dev.azure.com/{YOUR_ORG_HARDOCDED_IF_YOU_WANT}/$(System.TeamProject)/_apis/build/builds/$(build.buildid)/tags/${{tag_name}}?api-version=6.0"
$result = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $(System.AccessToken)"} -Method Put
我有在管道资源上触发的发布管道,但希望发布管道被标记为触发管道(构建)信息,这样我们就可以过滤什么时候部署的内容。
我正在尝试使用日志命令使用 apiname 变量标记发布管道,但我无法看到它们或使用它们进行过滤。
下面是我的发布管道代码
resources:
pipelines:
- pipeline: pipeline1
project: appcom
source: pipeline-api
trigger:
branches:
- develop
- feat/*
- pipeline: pipeline2
project: appcom
source: pipeline2-api
trigger:
branches:
- develop
- feat/*
variables:
- name: alias
value: $(resources.triggeringAlias)
stages:
- stage: ScanImage
jobs:
- job: ScanImage
pool:
vmImage: 'ubuntu-16.04'
steps:
- script: echo $(alias)
- task: Bash@3
inputs:
targetType: 'inline'
script: |
if [ "$(alias)" == "pipeline1" ]; then
echo "##vso[task.setvariable
variable=apiname]$(resources.pipeline.pipeline1.pipelineName)"
echo "##vso[task.setvariable
variable=dockertag]$(resources.pipeline.pipeline1.sourceCommit)
| cut -c -7"
echo "##vso[task.setvariable variable=helmpath]P02565Mallorca/pipeline1-api"
elif [ "$(alias)" = "pipeline2" ]; then
echo "##vso[task.setvariable
variable=apiname]$(resources.pipeline.pipeline2.pipelineName)"
echo "##vso[task.setvariable
variable=dockertag]$(resources.pipeline.pipeline2.sourceCommit)
| cut -c -7"
echo "##vso[task.setvariable variable=helmpath]P02565Mallorca/pipeline2-api"
fi
- script: echo "##vso[build.addbuildtag]$(apiname)"
发布管道运行:
[![在此处输入图片描述][1]][1]
过滤时不显示任何标签,只是说没有标签
下面是设置标签的bash脚本的截图 [![在此处输入图片描述][2]][2]
但无法过滤正在使用此标签。 [1]: https://i.stack.imgur.com/BVjGS.png [2]: https://i.stack.imgur.com/YaXJJ.png
您可以使用 Rest Api 更新标签:Tags - Add Build Tag
带有 powershell 任务的 Yaml 示例:
pool:
vmImage: 'ubuntu-16.04'
steps:
- task: PowerShell@2
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
inputs:
targetType: 'inline'
script: |
$user = ""
$token = $env:SYSTEM_ACCESSTOKEN
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$org = "$(System.CollectionUri)"
$teamProject = "$(System.TeamProject)"
$buildId = "$(Build.BuildId)"
$tagName = "test_tag"
$restApiUpdateBuild = "$org/$teamProject/_apis/build/builds/$buildId/tags/$tagName`?api-version=6.0"
function InvokePutReques ($PutUrl)
{
return Invoke-RestMethod -Uri $PutUrl -Method Put -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
}
$result = InvokePutReques $restApiUpdateBuild
pwsh: true
脚本中的最后一条指令 - script: git tag $(apiname)
可能不起作用,因为您在构建代理存储库上创建了标签。您必须发布您的标签。
检查此文档以Run Git commands in a script. Publish tags to a remote repo: How do you push a tag to a remote repository using Git?
is it possible to tag with apiname variable in above pipeline?
根据您的 yaml 示例,您已在 PowerShell 任务中设置了 apiname 变量。
因此您可以在下一个任务中直接使用格式为 $(apiname)
https://dev.azure.com/{organizationname}/{projectname}/_apis/build/builds/$(build.buildid)/tags/$(apiname)?api-version=6.0
这是一个例子:
steps:
- script: echo $(alias)
- task: Bash@3
....
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$token = "PAT"
$url="https://dev.azure.com/{organizationname}/{projectname}/_apis/build/builds/$(build.buildid)/tags/$(apiname)?api-version=6.0"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Put -ContentType application/json
这是一篇关于 the Tags - Add Build Tag and create PAT
的文档注意 PAT 令牌需要具有范围 vso.build_execute
更新:
使用Bash脚本运行其余API:
curl -X POST \
-u :{PAT} https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/build/builds/$(build.buildid)/tags?api-version=6.0 \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d ' ["$(apiname)"]'
更新2:
curl -X POST \
-u :$(System.AccessToken) https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/build/builds/$(build.buildid)/tags?api-version=6.0 \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d ' ["$(apiname)"]'
注意:您需要select选项:允许脚本在代理作业中访问 OAuth 令牌。
如果报无权限的错误,需要为projectname build service(organizationname)[=61]授予Manage build queue权限=] 账户
更新3:
您也可以使用 logging command 来设置构建标签:
这是 bash 脚本:
echo "##vso[build.addbuildtag]$(apiname)"
我不需要将任何内容转换为 base64 以获得身份验证信息,这很有效:
- powershell: |
$url="https://dev.azure.com/{YOUR_ORG_HARDOCDED_IF_YOU_WANT}/$(System.TeamProject)/_apis/build/builds/$(build.buildid)/tags/${{tag_name}}?api-version=6.0"
$result = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $(System.AccessToken)"} -Method Put