Search code examples
azure-devopsazure-pipelinescontinuous-delivery

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


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.


Solution

  • 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