10

The story so far:

  • When I have a commit that I wish to deploy, I create a GIT TAG to base the build on, eg "RC1"
  • The creation of the GIT TAG automatically triggers the PipeLine Build to start.
  • On successful completion of the PipeLine Build, it creates a new GIT TAG back on the source commit with the BuildNumber_BuildId for cross referencing later.
  • I also have a Release Pipeline that is then used to do the deploying (I like the separation) and I would like to filter on Build TAG = "RC*"
  • I can manually create the Build TAG, but....

This is where the help is needed.

I have hunted high and low for days for a way to automatically create the PipeLine Build TAG and only find answers to what I already have in place. I don't might if it is a step/task in YAML or a setting in the Build Pipeline. If anyone can point me in the right direction I'd be most grateful.

The end goal is to push the original source GIT TAG into the Build TAG if the build is successful.

Notes: I'm working only in YAML and what ever setting come nativly with Azure Devops Version Dev18.M170.1, ie no plugins.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
David
  • 113
  • 1
  • 1
  • 4

2 Answers2

13

If still in the build phase, you can add a build tag bu running the following command from a script, bash or PowerShell (write-host instead of echo) task:

echo "##vso[build.addbuildtag]My Tag"

In Yaml the simplest syntax would be:

- script: |
    echo "##vso[build.addbuildtag]My Tag"

Or for PowerShell you can use the short syntax:

steps:
- powershell: |
    $newSourceBranch = "$(Build.SourceBranch)" -replace 'refs/tags/', '' 
    $Command = "##vso[build.addbuildtag]"+$newSourceBranch 
    write-host "Create a Build TAG called $newSourceBranch" 
    write-host $Command
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • 1
    Thanks for your solution. This led me to eventually having: - task: PowerShell@2 displayName: 'Add Build Tag' condition: succeeded() # Only when all previous tasks have succeeded env: SYSTEM_ACCESSTOKEN: $(System.AccessToken) inputs: targetType: inline script: | $newSourceBranch = "$(Build.SourceBranch)" -replace 'refs/tags/', '' $Command = "##vso[build.addbuildtag]"+$newSourceBranch write-host "Create a Build TAG called $newSourceBranch" write-host $Command – David Nov 05 '21 at 15:35
  • You don't even need the access token! – jessehouwing Nov 05 '21 at 19:23
  • And the succeeded condition you can also drop, that's the default. – jessehouwing Nov 05 '21 at 19:23
  • 1
    Script option does not work as YAML treats everything after first hash as a comment. I had to move the script to the next line: - script: | echo ##vso[build.addbuildtag]My Tag. – zendu Oct 23 '22 at 16:37
  • 1
    Just an important note about the above code. You need to make sure that there is no space after `addbuildtag]`. If there is a space after that before your tag name, it won't tag the build – Newteq Developer Apr 06 '23 at 19:21
  • @NewteqDeveloper you mean like: `$Command = "##vso[build.addbuildtag]"+$newSourceBranch` ? As `$Command = "##vso[build.addbuildtag]" + $newSourceBranch` is valid ofcourse – Michael Apr 25 '23 at 08:39
  • @Michael yes, as you described. there should not be a space after the `]` (inside the quotes) – Newteq Developer Apr 25 '23 at 11:04
  • Where did you find this information? – Zoidbergseasharp Sep 01 '23 at 08:23
  • https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=bash#addbuildtag-add-a-tag-to-the-build – jessehouwing Sep 01 '23 at 08:38
8

Sure. You can use the API "Tags - Add Build Tag" to add a tag when the build is successful.

If you want to add multiple tags to the successful build at the same time, you can use the API "Tags - Add Build Tags".

Below is a demo to add a tag to the the successful build. You can reference it and set up the related steps in your build pipeline.

For classic build pipeline:

  1. Enable the option "Allow scripts to access the OAuth token" on the settings page of the build job. enter image description here

  2. Add a PowerShell task as the last one of the build job.

    • Select "Run this task Only when all previous tasks have succeeded"
    • Add the following script to the task. Replace {organization}, {project} and {tag} with the actual organization, project and tag you want.
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Authorization", "Bearer $env:SYSTEM_ACCESSTOKEN")
    $headers.Add("Content-Type", "application/json")
    $uri = "https://dev.azure.com/{organization}/{project}/_apis/build/builds/$(Build.BuildId)/tags/{tag}?api-version=6.0"
    Invoke-RestMethod -Uri $uri -Headers $headers -Method PUT
    

    enter image description here

For YAML build pipeline:

Add a PowerShell task as the last one of the build job like as below. Replace {organization}, {project} and {tag} with the actual organization, project and tag you want.

jobs:
- job: build
  displayName: 'Build job'
  . . .
  steps:
  . . .
  - task: PowerShell@2
    displayName: 'Add Build Tag'
    condition: succeeded()  # Only when all previous tasks have succeeded
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)
    inputs:
      targetType: inline
      script: |
        $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
        $headers.Add("Authorization", "Bearer $env:SYSTEM_ACCESSTOKEN")
        $headers.Add("Content-Type", "application/json")
        $uri = "https://dev.azure.com/{organization}/{project}/_apis/build/builds/$(Build.BuildId)/tags/{tag}?api-version=6.0"
        Invoke-RestMethod -Uri $uri -Headers $headers -Method PUT

Result:

enter image description here

Bright Ran-MSFT
  • 5,190
  • 1
  • 5
  • 12
  • 1
    Thank you so much for you very helpful and insightful answer covering both Classic and Yaml pipelines. At the moment I am on an on premis DevOps and the URL seems very much different for what you show for the https://dev.azure.com As I'm still in the actual pipeline build I when I'm adding the TAG, I went with "##vso[build.addbuildtag]"+$newSourceBranch" in the end. – David Nov 05 '21 at 15:28