0
votes

i have release pipelines which gets triggered on pipeline resources but would like to have release pipeline gets tagged with triggering pipeline(build) info, so we can have filter about what deployed on when.

I am trying to tag release pipeline with apiname variable using logging command, but i am unable to see them or filter with them .

below is the my release pipeline code

 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)"
     

release pipeline runs:

[![enter image description here][1]][1]

it does not show any tags while filtering and it simply says no tags

below is the screenshot of bash script which set tag [![enter image description here][2]][2]

but unable to filter is using this tag. [1]: https://i.stack.imgur.com/BVjGS.png [2]: https://i.stack.imgur.com/YaXJJ.png

3
Can you share the build results to see failed steps?Shamrai Aleksander
but my requirement here is i need to tag this release pipeline with apiname.. in above code you could apiname is set as variable with predefined variable.. is it possible to tag with apiname ???pranathi
@pranathi. Of course. You can do it. Since you have set the apiname as variable, you can directly use it in the Rest API URL with the the format $(apiname). Please refer to my answerKevin Lu-MSFT

3 Answers

0
votes

You can use Rest Api to update tags: Tags - Add Build Tag

Yaml example with the powershell task:

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
0
votes

The last instruction in your scripts - script: git tag $(apiname) may not work because you create tag on the build agent repository. You have to publish your tag.

Check this document to Run Git commands in a script. Publish tags to a remote repo: How do you push a tag to a remote repository using Git?

0
votes

is it possible to tag with apiname variable in above pipeline?

From your yaml sample, you have set the apiname variable in PowerShell task.

So you could directly use the variable in the next task with the format $(apiname)

https://dev.azure.com/{organizationname}/{projectname}/_apis/build/builds/$(build.buildid)/tags/$(apiname)?api-version=6.0

Here is an example:

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

Here is a doc about the Tags - Add Build Tag and create PAT

Note The PAT Token need to has the scope vso.build_execute enter image description here

Update:

To use Bash script to run the 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)"]'

Update2:

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)"]'

Note: you need to select the option: Allow scripts to access the OAuth token in Agent Job.

enter image description here

If it reports the error about having no permission, you need to grant the Manage build queue permission for the projectname build service (organizationname) account

enter image description here

Update3:

You can aslo use logging command to set the build tag:

Here is the bash script:

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