Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 702
MW. Avatar asked Jun 13 '19 07:06

MW.


People also ask

How do I use tags in Azure DevOps pipeline?

Go to Variables/Pipeline variables tab. Add your variable, for example EnvironmentTag and add default value. Select Settable at queue time to make sure that you can change it when you are scheduling your build.

How do I add tags to Azure DevOps?

Select Create Tag from the Tags view in the web portal to create a new annotated tag. Specify a Name, select the branch to Tag from, enter a Description (required since you are creating an annotated tag), and select Create. The new tag is displayed in the tag list.

What is tag name in git?

Tags are ref's that point to specific points in Git history. Tagging is generally used to capture a point in history that is used for a marked version release (i.e. v1. 0.1). A tag is like a branch that doesn't change. Unlike branches, tags, after being created, have no further history of commits.


2 Answers

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.

like image 195
MW. Avatar answered Oct 22 '22 00:10

MW.


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'
like image 2
SLdragon Avatar answered Oct 21 '22 22:10

SLdragon