Vikram
Vikram

Reputation: 7517

How to use groovy variable in jenkins sh command

I am trying to use a groovy variable in my Jenkins file as shown below

def since='2018-06-01';
count = sh(returnStdout: true, script: 'git rev-list --all --count master --since="${since}"').trim();

The variable since is not evaluating in the Jenkins output.

+ git rev-list --all --count master --since=
[Pipeline] echo

Looks like I am doing something wrong in the syntax. Can some one help on this.

Upvotes: 1

Views: 552

Answers (1)

Szymon Stepniak
Szymon Stepniak

Reputation: 42174

If you want to interpolate Groovy variable into the string you have to use double quotes instead (single quotes create String, while double quotes create GString):

count = sh(returnStdout: true, script: "git rev-list --all --count master --since=\\\"${since}\\\"").trim();

Also, if you want to pass --since="2018-06-01" you will have to escape " and \ like in the above example.

Updated code produces following git command:

+ git rev-list --all --count master --since="2018-06-01"

Upvotes: 2

Related Questions