-1

I have a shell script inside my jenkins pipeline which will call mvn. For that i have to pass variable value to mvn. The variable is not passing inside the Jenkins pipeline's shell. But when trying from local machine shell it is working fine as expected.

ARTIFACT_NAME="Sample_Artifact"

pipeline{
    agent {
        node{
            label "${AGENT}"
    
    }
    }
   stages{
         
        
        stage("Setting MultiJob Properties"){
            steps{
                      sh '''set +x



export VERSION=$(mvn -B -q -Dexec.executable=echo -Dexec.args=\${${ARTIFACT_NAME}} )

echo $VERSION
'''

            }
        }
   }
}

Expected Process: export VERSION=$(mvn -B -q -Dexec.executable=echo -Dexec.args=${Sample_Artifact} )

Expected Output: 1.0001

ARTIFACT_NAME - I am passing it from Jenkins UI.

${${ARTIFACT_NAME}} - This variable is perfectly replace value in Freestyle jobs and it is throwing error in the Pipeline jobs.

Error Message: script.sh: 3: Bad substitution

Can Anyone please help me to resolve the issue?

  • 1
    What kind of error? Closing `}` is missing by `${${ARTIFACT_NAME}` – Melkjot Oct 20 '21 at 09:10
  • Show your actual pipeline. Probably needs " instead of '. [Explanation,](https://stackoverflow.com/a/53402569/598141) – Ian W Oct 20 '21 at 09:36
  • FYR, pipeline{ agent { node{ label "${AGENT}" } } stages{ stage("Setting MultiJob Properties"){ steps{ sh '''set +x export VERSION=$(mvn -B -q -Dexec.executable=echo -Dexec.args=\${${ARTIFACT_NAME}} ) echo $VERSION ''' } } } } – Daisy Dharshini Oct 20 '21 at 09:42
  • What does the error message say? – Melkjot Oct 20 '21 at 13:44
  • For Example: The following code running without any errors in my local machine shell but when i was trying from Jenkins pipeline it is throwing error. #!/bin/sh ARTIFACT_NAME="test" sample=$(echo \${${ARTIFACT_NAME}}) echo "$sample" shell output: ${test} Jenkins pipeline Error: script.sh: 3: Bad substitution @Melkjot – Daisy Dharshini Oct 21 '21 at 03:41

1 Answers1

0

As Ian wrote, you're passing the whole script as a literal (''') instead of an interpolated string ("""), so the variable name doesn't get substituted with its value:

pipeline{
  agent {
    node {
      label AGENT
    }
  }
  stages {
    stage("Setting MultiJob Properties") {
      steps {
        sh """set +x
           export VERSION=\$(mvn -B -q -Dexec.executable=echo -Dexec.args=\${$ARTIFACT_NAME})
           echo \$VERSION"""
      }
    }
  }
}
towel
  • 2,094
  • 1
  • 20
  • 30