5

I have stage in my jenkinsfile, that executes a bat command:

stage ('build'){
  bat '%cd%\\MySimpleProject\\bin\\Execute.bat "${env.BRANCH_NAME}"'
}

My batch command requires a parameter that is the current branch in svn.

When I use this:

echo "SVN_BRANCH_NAME is ${env.BRANCH_NAME}"

It would give the value of BRANCH_NAME but if I pass it as param to my batch file, it literally pass ${env.BRANCH_NAME} not the value.

Is their a way to do this?

user1670340
  • 129
  • 1
  • 2
  • 13

2 Answers2

10

It's because all is wrapped in single quotes and groovy won't interpolate the string. Try

stage ('build'){ bat "%cd%\\MySimpleProject\\bin\\Execute.bat ${env.BRANCH_NAME}"}
shipperizer
  • 1,641
  • 1
  • 13
  • 19
1

I had a similuar issue, when running bat in Jenkins I was not able to get the variable to pass into the command, it would just print the variable as is. e.g. ${env.P4_CHANGELIST} would just print as is, and not have the actual changelist.

My original approach:

bat 'xcopy "D://CookedBuildInProgress" "\\Path\\To\\File\\CookedBuilds\\Win64_Development_${env.P4_CHANGELIST}" /e /i /s /y'

The issue was ${env.P4_CHANGELIST}" was not passing in the changelist, and instead kept itself blank. As @shipperizer said, its due to being wrapped with the single quote. However, his solution did not work for my need.

My Solution:

bat """xcopy "D://CookedBuildInProgress" "\\Path\\To\\File\\CookedBuilds\\Win64_Development_${env.P4_CHANGELIST}" /e /i /s /y'"""

Triple quotes worked to pass in the variable so it was now Win64_Development_1234

Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36
Casterial
  • 21
  • 3