1

I have the following Jenkinfile content that is able to create the tag name as I want and stored in the varibale 'tag'. How can I use that variable in a batch command here?

Note that Jenkins is on a Windows machine thus using bat command. Am all ears if there is a simple way I could switch to bash. But the main question is as follows. Thank you.

How can I use that 'tag' variable (which has correct value stored before I try to use it in a batch command)? Currently it is coming out with no value with my implementation below trying to echo it.

#!/usr/bin/groovy

pipeline{
    agent any
    stages {
        stage('tag stage'){
            steps {
                gitTag()
            }
        }
    }
}

def gitTag(){
    String date = new Date().format('yyyyMMddhhmmss')
    String branch = "${env.GIT_BRANCH}"
    String tag = "v${date}-${branch}"
    tag = tag.replaceAll('/', '-')
    String message = "tagged via jenkins - ${tag}"
    print message

    bat 'echo Hello test'
    bat 'echo from bat before tag %tag% after tag'
    bat 'git tag -a %tag% -m "tagging with %message%"'
    bat 'git push origin %tag%'
}

2 Answers 2

3

Seems due to single quote, groovy is not able to interpolate the variable. Also, use ${var} format. Following should do the trick:

def gitTag(){
    String date = new Date().format('yyyyMMddhhmmss')
    String branch = "${env.GIT_BRANCH}"
    String tag = "v${date}-${branch}"
    tag = tag.replaceAll('/', '-')
    String message = "tagged via jenkins - ${tag}"
    print message

    bat "echo from bat before tag ${tag} after tag"
    bat "git tag -a ${tag} -m \"tagging with ${message}\""
    bat "git push origin ${tag}"
}
Sign up to request clarification or add additional context in comments.

1 Comment

%var% versus ${var} just decides who interpolates the variable. The former (how OP phrased it) has the agent handling interpolation from the environment. The latter has Groovy handle interpolation at "compile" time
0

I would probably prefer to create the tag in an environment block, then reference the environment tag in my shell script.

def gitTagName(String branch) {
    String date = new Date().format('yyyyMMddhhmmss')
    String tag = "v${date}-${branch}".replaceAll('/', '-')

    return tag
}

pipeline {
    agent any
    stages {
        stage('tag and publish') {
            // N.B. this could be inside the "pipeline" block too, depending on scope
            environment {
                tag = gitTagName(env.GIT_BRANCH)
            }
            steps {
               bat """\
echo from bat before tag ${env.tag} after tag
git tag -a ${tag} -m "tagging with tagged via jenkins - ${tag}"
git push origin ${tag}"""
            }
        }
    }
}

I would probably pull that batch script into your repository while you're at it, though that might be a little over-zealous.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.