'Git tag name in Azure Devops Pipeline YAML

Summary

How do I get the name of the current git tag in an Azure Devops Pipeline YAML-file?

What Am I Trying to Do?

I am setting up a build pipeline in Azure Devops. The pipeline triggers when a new git tag is created. I then want to build docker images and tag them with the git tag's name.

My YAML pipeline looks something like this:

# Trigger on new tags.
trigger:
  tags:
    include:
    - '*'

stages:
- stage: Build
  jobs:
  - job: Build
    pool:
      vmImage: 'ubuntu-latest'

    steps:
    - script: export VERSION_TAG={{ SOMEHOW GET THE VERSION TAG HERE?? }}
      displayName: Set the git tag name as environment variable

    - script: docker-compose -f k8s/docker-compose.yml build
      displayName: 'Build docker containers'

    - script: docker-compose -f k8s/docker-compose.yml push
      displayName: 'Push docker containers'

And the docker-compose file I am referencing something like this:

version: '3'
services:
  service1:
    image: my.privaterepo.example/app/service1:${VERSION_TAG}
    build:
      [ ... REDACTED ]
  service2:
    image: my.privaterepo.example/app/service2:${VERSION_TAG}
    build:
      [ ... REDACTED ]

As you can see, the tag name in the docker-compose file is taken from the environment variable VERSION_TAG. In the YAML pipeline, I am trying to set the environment variable VERSION_TAG based on the current GIT tag. So... how do I get the name of the tag?



Solution 1:[1]

Ok, this was a bit trickier than I expected. Here's the step required to set the variable:

steps:
    - script: VERSION_TAG=`git describe --tags` && echo "##vso[task.setvariable variable=VERSION_TAG]$VERSION_TAG"
      displayName: Set the tag name as an environment variable

This script sets the variable VERSION_TAG to the name of the latest git tag. It does so in three steps:

1: git describe --tags

Prints the name of the current/latest tag

2: VERSION_TAG=`...`

Sets the output of step 1 to a local variable

3: echo "##vso[task.setvariable variable=VERSION_TAG]$VERSION_TAG"

Prints out a command that sets the variable in Azure Devops. The local variable set in step 2 is used as the value.

Solution 2:[2]

For windows vm, you can use the script below to get tag:

steps:
- powershell: |
   $CI_BUILD_TAG = git describe --tags
   Write-Host "##vso[task.setvariable variable=CI_BUILD_TAG]$CI_BUILD_TAG"
  displayName: 'Set the tag name as an environment variable'

Solution 3:[3]

My usecase: Trigger build pipeline on new git commit tag resulting an IoTHub deployment manifest with same name as git commit tag.

After some time fiddling around I concluded that it's simpler to fetch commit tag from build.sourceBranch resulting in something like: refs/tags/xxx. The tag itself can be extracted with following script:

trigger:
  branches:
    include:
      - "*"
  tags:
    include:
      - "*"
steps:
- bash: |
    export RELEASE_TAG=$(echo $(build.sourceBranch) | sed -e "s/^refs\/tags\///")
    echo "##vso[task.setvariable variable=RELEASE_TAG;]$RELEASE_TAG"
  1. Important to include trigger on wildcard tags to trigger build on new tag. According to https://docs.microsoft.com/en-us/azure/devops/pipelines/repos/github?view=azure-devops&tabs=yaml#ci-triggers
  2. echo $(build.sourceBranch) -> refs/tags/2.0.3 (the tag that triggered the build)
  3. | sed -e "s/^refs\/tags\///" -> Pipes output of echo to sed and removes the prefix refs/tags/
  4. echo "##vso[task.setvariable variable=RELEASE_TAG;]$RELEASE_TAG" -> Saves tag to pipeline variable according to https://docs.microsoft.com/en-us/azure/devops/pipelines/process/set-variables-scripts?view=azure-devops&tabs=bash#set-a-variable-as-secret

Rest of my pipeline for those who are interested in seeing where I use the RELEASE_TAG variable:

- download: current
  artifact: deployment
  patterns: "**/deployment.json"
- task: AzureIoTEdge@2
  displayName: "Deploy to 'tags.release=commit-tag'"
  inputs:
    action: "Deploy to IoT Edge devices"
    deploymentFilePath: "$(Pipeline.Workspace)/deployment/deployment.json"
    azureSubscription: "Azure-Service-Connection"
    iothubname: "iothub"
    deploymentid: "$(System.TeamProject)-devops-$(RELEASE_TAG)-$(Build.BuildId)"
    priority: "$(Build.BuildId)"
    deviceOption: "Multiple Devices"
    targetcondition: "tags.release='$(RELEASE_TAG)'"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MW.
Solution 2 SLdragon
Solution 3 Mathias Haugsbø