Issue
I am struggling with a Jenkins pipeline and hope you can help me out. I need a Jenkins parameter (Cycle) within a batch command to call a python script.
environment {
Variant = "${params.Var}"
Cycle = "2021"
}
stages {
stage('Check for most recent project') {
steps {
script{
if ("${Variant}" == "green") {
help = bat(script: 'python.exe "somescript.py" --sourcepath \"\\Buildenv\\green\\${Cycle}\"', returnStdout: true)
}
else {
help = bat(script: 'python.exe "somescript.py" --sourcepath \"\\Buildenv\\red\\${Cycle}\"', returnStdout: true)
}
ProjectToBuild = help.split('\n')[-1]
if ("${Variant}" == "green") {
bat script: "copy \"\\Buildenv\\green\\${Cycle}\\${ProjectToBuild}.7z\" \"D:\\Workspace\""
}
else {
bat script: "copy \"\\Buildenv\\red\\${Cycle}\\${ProjectToBuild}.7z\" \"D:\\Workspace\""
}
}
}
}
}
In a pure batch command (bottom lines) using a parameter works perfectly fine. However, if I am calling a python script and want to handover a parameter this doesn't work. Unfortunately I didn't find any post helping me with this.
Thanks for your help already!
Solution
Your issue is not related to python execution, it is related to the fact that in groovy single quotes (' '
) don't support String Interpolation only double quotes (" "
) do, and therefore your parameter is not getting evaluated.
So you have two options:
- Change your
bat
script so it uses double quotes:
help = bat(script: "python.exe \"somescript.py\" --sourcepath \"\\Buildenv\\green\\${Cycle}\"", returnStdout: true)
- Because
Cycle
is set as an environment parameter it is passed to the command line environment and you can use it as a Windows environment variable with% %
:
help = bat(script: 'python.exe "somescript.py" --sourcepath "\\Buildenv\\green\\%Cycle%"', returnStdout: true)
Answered By - Noam Helmer