Jenkins

Jenkins TAG_NAME

Environment variables, or env vars, are variables which are set outside of a script and are used in Jenkins scripts.

Jenkins environment variables are a set of key-value pairs where the key is the variable’s name and the value is the value that is stored in the variable.

We can configure that the Jenkins environment variables have various levels such as the system level, the node level, or in a pipeline. Once defined, we can access from any script (Bash, shell, groovy, etc.) which is run by Jenkins.

In this tutorial, we will learn how to use one of the Jenkins environment variables called tag_name.

Jenkins TAG_NAME

In Jenkins, the tag_name environment variable is automatically set when you create a job with a git tag. This is very popular when working with multibranch pipelines.

The environment variable contains the name of the git tag which is triggered during the build process.

For example, if we have a Jenkins job that is configured to build when a git tag is pushed, and we push a tag named v0.1.0 to the repository, the tag_name environment variable is set to v0.1.0 during the build.

How to Access the TAG_NAME in A Jenkins Pipeline

To access the value of the tag_name variable, we can use the dollar symbol ($) followed by the variable’s name. For example, the following should print the name of the tag_name variable:

echo $tag_name

An example Jenkinsfile to accomplish this is as shown in the following:

pipeline {
  agent any
  stages {
    stage("Print tag name") {
      steps {
        echo "The value of tag_name is: $tag_name"
      }
    }
  }

}

This Jenkinsfile defines a pipeline with a single stage that contains a single step to print the value of the tag_name variable. When we run the pipeline, Jenkins executes the specified stage and returns the value of the tag_name variable.

You can combine the environment variables with the other Jenkins features. For example, you can use the when clause to perform an action when the value of the tag_name is equal to a given value. Feel free to check our tutorial on the when clause to learn how to do this.

Conclusion

This tutorial taught us what the Jenkins tag_name environment represents and how we can access it in a given build.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list