Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 412 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use groovy env. variable in Jenkins to pass through bat command in Jenkins pipeline

#1
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?
Reply

#2
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}"}
Reply

#3
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`
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through