How to automatically create a Build Pipeline TAG (not a GIT TAG) after a successful build?

Viewed 3985

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.

2 Answers

If still in the build phase, you can easily run 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

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

Related