【问题标题】:how to tag triggered pipeline with triggering pipeline info on azure devops如何在 azure devops 上使用触发管道信息标记触发管道
【发布时间】:2021-02-17 08:22:20
【问题描述】:

我有在管道资源上触发的发布管道,但希望发布管道被标记为触发管道(构建)信息,因此我们可以过滤何时部署的内容。

我正在尝试使用日志命令使用 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

【问题讨论】:

  • 您能分享构建结果以查看失败的步骤吗?
  • 但我的要求是我需要用 apiname 标记这个发布管道。在上面的代码中,你可以将 apiname 设置为带有预定义变量的变量。是否可以用 apiname 标记???
  • @pranathi。当然。你能行的。由于您已将 apiname 设置为变量,因此您可以直接在 Rest API URL 中使用它,格式为 $(apiname)。请参考我的回答

标签: azure-devops azure-pipelines azure-pipelines-release-pipeline


【解决方案1】:

是否可以在上述管道中使用 apiname 变量进行标记?

从您的 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 Tagcreate PAT 的文档

注意 PAT Token需要有vso.build_execute的范围

更新:

使用 Bash 脚本运行 Rest 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)"]'

注意:您需要在代理作业中选择选项:允许脚本访问 OAuth 令牌

如果报没有权限的错误,你需要授予projectname build service (organizationname)账号的Manage build queue权限

更新3:

您也可以使用logging command 设置构建标签:

这是 bash 脚本:

echo "##vso[build.addbuildtag]$(apiname)"

【讨论】:

  • 我应该在 bash 脚本中使用相同的吗,因为我的代理上的 powershell 脚本会有问题
  • 嗨@pranathi。当然,您可以在 Bash Script 中执行此操作。请检查我的更新。
  • 嗨@pranathi。这张票有更新吗?如果答案有帮助,请随时告诉我。
  • 谢谢 Kevin,但我不能在我的组织中使用 PAT,你能提供给我同样的系统访问令牌和 bash 脚本吗?
  • 感谢凯文的支持
【解决方案2】:

可以使用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

【讨论】:

  • 是否可以使用预定义变量进行设置,我想使用管道资源信息更改管道或标签发布的描述
  • @pranathi,如果您想使用$(apiname) 作为标签,请在脚本中将$tagName = "test_tag" 更新为$tagName = "$(apiname)"
  • 我可以在 bash 脚本中获得与在我们的代理上运行 powershell 脚本时遇到的问题相同的内容吗?
【解决方案3】:

脚本中的最后一条指令- script: git tag $(apiname) 可能不起作用,因为您在构建代理存储库上创建了标签。你必须发布你的标签。

将此文档发送至Run Git commands in a script。将标签发布到远程仓库:How do you push a tag to a remote repository using Git?

【讨论】:

  • 是否可以在上述管道中使用 apiname 变量进行标记?
【解决方案4】:

我不必将任何内容转换为 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-13
    • 2022-10-25
    • 2020-04-24
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多