Issue
I am in need of modifying a Jenkins Pipeline script. This is a snippet. It works down to the Def in the rename. The error I get is
groovy.lang.MissingPropertyException: No such property: newFileName for class: groovy.lang.Binding
I am looking to inject the version number into the filename which is then processed by a homegrown app to move it where it needs to go and some other stuff. I have tried passing the concatenated ${version} into the Upload executable but it then errors as a literal instead of a variable.
Update: I predefine the variable and get a new error
powershell.exe : newFileName : The term 'newFileName' is not recognized as the name of a cmdlet, function, script file, or operable At C:\workspace\workspace\Centurion Dashboard@tmp\durable-eb5db1ae\powershellWrapper.ps1:3 char:1 & powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Comm
- CategoryInfo : NotSpecified: (newFileName : T...e, or >operable :String) [], RemoteException
- FullyQualifiedErrorId : NativeCommandError
Thoughts?
stage('Rename') {
powershell """
rename-item -Path "C:\\Users\\Administrator\\Documents\\Advanced Installer\\Projects\\Proj\\Setup Files\\SetupCenturionDashboard.exe" -NewName "OtherProject-${version}.exe"
def newFileName = "C:\\Users\\Administrator\\Documents\\Advanced Installer\\Projects\\Proj\\Setup Files\\OtherProject-" + ${version} + ".exe"
echo ${newFileName}
"""
}
stage('Upload') {
bat label: 'Upload', script: 'C:\\Static\\BuildHelper\\BuildHelper.exe ${newFileName} "develop" "home" "${version}"'
}
Solution
powershell != groovy
You cannot run groovy inside of powershell block
powershell """
"""
and def newFileName
is groovy, that is why you get the error:
The term 'newFileName' is not recognized as the name of a cmdlet...
As you can see, newFileName executable does not exist on windows.
variables on windows
If you just need to pass the version and newFileName variables to your powershell or bat code, you need to use the windows syntax %var%
for variables, not the unix ${var}
and use global variables if it is possible in your pipeline:
def version = "1.0.0";
def newFileName;
stage('Rename') {
powershell """
rename-item -Path "C:\\Users\\foo.exe" -NewName "OtherProject-%version%.exe"
"""
newFileName = "C:\\Users\\OtherProject-${version}.exe"
}
stage('Upload') {
bat label: 'Upload', script: 'C:\\Static\\BuildHelper\\BuildHelper.exe %newFileName% "develop" "home" "%version%"'
}
Answered By - JRichardsz