1

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

pranathi
  • 87
  • 11
  • Can you share the build results to see failed steps? – Shamrai Aleksander Feb 17 '21 at 17:31
  • 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 Feb 19 '21 at 05:32
  • @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 answer – Kevin Lu-MSFT Feb 19 '21 at 06:15

4 Answers4

1

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)"
Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • 1
    shall i use same in bash script, as i will have issues with powershell script on my agents – pranathi Feb 19 '21 at 12:13
  • Hi @pranathi. Of course, you can do it in Bash Script. Please check my update. – Kevin Lu-MSFT Feb 22 '21 at 08:24
  • Hi @pranathi. Is there any update about this ticket? Feel free to let me know if the answer could help. – Kevin Lu-MSFT Feb 23 '21 at 01:42
  • Thanks Kevin, but i cannot use PAT in my organization, could you provide me same with system access token with bash script. – pranathi Feb 23 '21 at 06:09
  • @pranathi. Please refer to my update2. Feel free to let me know if you have any questions. – Kevin Lu-MSFT Feb 23 '21 at 06:21
  • Hi @pranathi. Please refer to my update3. The easier method is using logging command. You can try it too. – Kevin Lu-MSFT Feb 23 '21 at 06:40
  • 1
    Thank Kevin for your kind support – pranathi Feb 23 '21 at 08:09
  • @pranathi. It's my pleasure. If the answer could solve your issue, you may consider accepting it as answer. Thanks. – Kevin Lu-MSFT Feb 23 '21 at 08:10
  • i added script: echo "##vso[build.addbuildtag]$(apiname)" but it does not show any tags. added screenshot to question. may i know if i missing something – pranathi Feb 25 '21 at 06:55
  • You need to check if the bash script has executed. And you could the task log: Build '3613' has following tags now: testtag. Please check if you could see the message – Kevin Lu-MSFT Feb 25 '21 at 07:00
  • thanks. actually echo "##vso[build.addbuildtag]$(apiname)" command works. it sets tag but only issue is when i click on tags to list pipeline runs . it simply says no tags available. not sure any other modification needed – pranathi Feb 26 '21 at 16:36
0

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
Shamrai Aleksander
  • 13,096
  • 3
  • 24
  • 31
  • is it possible to set with predefine variabels , i would like to change description of the pipeline or tag release with pipeline resources information – pranathi Feb 17 '21 at 15:44
  • @pranathi, if you want to use `$(apiname)` as a tag, update `$tagName = "test_tag"` to `$tagName = "$(apiname)"` in the scripts – Shamrai Aleksander Feb 19 '21 at 08:50
  • can i get samething in bash script as i have issue in running powershell script on our agents ? – pranathi Feb 22 '21 at 07:32
0

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?

Shamrai Aleksander
  • 13,096
  • 3
  • 24
  • 31
0

I didnt have to convert anything to base64 for auth info, and this worked:

  - 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
crh225
  • 821
  • 18
  • 34