Search code examples
javajenkinssshbuild

Jenkinsfile: varibale not "parsed" inside SSH command


I have a version of my artifact defined in the JenkinfFile which is resolved fine. I tested this with the output of the variable. However, it does not get parsed during an SSH command. What is the right syntax ?

        version = sh (
        script: './gradlew -q printVersion',
        returnStdout: true
    ).trim()

    echo "$version" 

    sshPut remote: remote, from: 'build/libs/core-\${version}-all.jar', into: '/opt/microservices/core.jar'

Output:

1.0.6 java.lang.IllegalArgumentException: /jenkins/workspace/core/build/libs/core-${version}-all.jar does not exist.


Solution

  • You need to use double quotes for String interpolation in Groovy:-

    sshPut remote: remote, from: "build/libs/core-${version}-all.jar", into: '/opt/microservices/core.jar'
    

    This is why your echo statement is working fine, but your sshPut is not.

    See: https://groovy-lang.org/syntax.html#_string_interpolation