ligi
ligi

Reputation: 39519

Jenkinsfile get current tag

Is there a way to get the current tag ( or null if there is none ) for a job in a Jenkinsfile? The background is that I only want to build some artifacts ( android APKs ) when this commit has a tag. I tried:

env.TAG_NAME

and

binding.variables.get("TAG_NAME")

both are always null - even though this ( https://issues.jenkins-ci.org/browse/JENKINS-34520 ) indicates otherwise

Upvotes: 25

Views: 70395

Answers (8)

rbellamy
rbellamy

Reputation: 5833

Returning last tag on current branch, not necessarily on last commit:

sh "git tag --sort version:refname | tail -1 > version.tmp"
String tag = readFile 'version.tmp'

Upvotes: 1

Bob
Bob

Reputation: 1597

An example of declarative pipeline following the OP usecase: "do something if this particular commit has a tag attached":

def gitTag = null
pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps {
        checkout(...)
        script {
          gitTag=sh(returnStdout: true, script: "git tag --contains | head -1").trim()
        }
      }
    }
    stage('If tagged') {
      when {
        expression {
          return gitTag;
        }
      }
      steps {
        // ... do something only if there's a tag on this particular commit
      }
    }
  }
}

In my case, I have:

  • one repository for multiple projects
  • each one with their own version tags such as 'MYPROJECT_1.4.23'
  • and I want to use the second part of the tag '1.4.23' to tag my images for example

I need to analyze the current tag to check if it concerns my pipeline project (using a PROJECT_NAME variable per project):

def gitTag = null
def gitTagVersion = null
pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps {
        checkout(...)
        script {
          gitTag=sh(returnStdout: true, script: "git tag --contains | head -1").trim()
          if(gitTag) {
            def parts = gitTag.split('_')
            if( parts.size()==2 && parts[0]==PROJECT_NAME ) {
              gitTagVersion=parts[1]
            }
          }
        }
      }
    }
    stage('If tagged') {
      when {
        expression {
          return gitTagVersion;
        }
      }
      steps {
        // ... do something only if there's a tag for this project on this particular commit
      }
    }
  }
}

Note that I'm new at Jenkins and Groovy and that this may be written more simply/cleanly (suggestions welcome).

(with Jenkins 2.268)

Upvotes: 1

Christoph Schwalbe
Christoph Schwalbe

Reputation: 81

best way from my site is:

git tag --sort=-creatordate | head -n 1

with:

latestTag = sh(returnStdout:  true, script: "git tag --sort=-creatordate | head -n 1").trim()

Than you can handle with simple regex, for prefix/suffix/version_number what is to do with the tag.

other solution:

git describe --tags --abbrev=0

of course this is the current/latest tag in git. Independent from writing.

sh(returnStdout: true, script: "git describe --tags --abbrev=0").trim()

Upvotes: 6

kuhnroyal
kuhnroyal

Reputation: 7553

The TAG_NAME should work now at least in declarative pipelines.

When condition actually filters on this property. BRANCH_NAME has the same value.

stage('release') {
   when {
     tag 'release-*'
   }
   steps {
     echo "Building $BRANCH_NAME"
     echo "Building $TAG_NAME"
   }
}

See https://jenkins.io/doc/book/pipeline/syntax/#when

Upvotes: 26

Raphael
Raphael

Reputation: 10549

If the current build is a tag build -- as in, when { buildingTag() } was "true" -- then the (undocumented) environment variable BRANCH_NAME contains the name of the tag being build.

Upvotes: 11

K Beiske
K Beiske

Reputation: 41

I believe the git command that does what you want is git tag --points-at=HEAD this will list all tags pointing to the current commit or HEAD as it usually called in git. Since HEAD is also the default argument you can also do just git tag --points-at.

The pipeline step for executing and returning the git tags one for each line, then becomes:

sh(returnStdout: true, script: "git tag --points-at")

Upvotes: 4

Florian
Florian

Reputation: 1056

All the other answers yield an output in any case even if HEAD is not tagged. The question was however to return the current tag and "null" if there is nothing like that.

git tag --contains yields the tag name name if and only if HEAD is tagged.

For Jenkins Pipelines it should look like this:

sh(returnStdout: true, script: "git tag --contains").trim()

Upvotes: 35

Víctor Romero
Víctor Romero

Reputation: 5115

I'd consider returnStdout rather than writing to a file:

sh(returnStdout: true, script: "git tag --sort version:refname | tail -1").trim()

Upvotes: 29

Related Questions