简体   繁体   中英

Jenkins groovy.lang.MissingPropertyException: No such property Bash

when running a Jenkins Groovy Piepline script that executes bash shell, a locally set variable can't be found when echo'ed out.

It reports error -

groovy.lang.MissingPropertyException: No such property: md5Value for class: WorkflowScript

Any ideas / pointer would be much appreciated.

#!groovy

node {

    try {

      stage('Test-Echo') {

          sh """
                #!/usr/bin/env bash

                md5Value='y'

                echo 'md5Value : ${md5Value}'

           """
       }

    } catch (e) {
        println (e.getMessage())
    }
}

Reply to comment 1

Yes I've tried these different permutations, all report the same error.

echo 'md5Value : ${md5Value}'
echo "md5Value : ${md5Value}"
echo 'md5Value : ' ${md5Value}
echo "md5Value : " ${md5Value}
echo 'md5Value :  $md5Value'
echo 'md5Value : ' $md5Value
echo "md5Value :  $md5Value"
echo "md5Value : " $md5Value

If the exception is handled locally it produces error -

hudson.AbortException: script returned exit code 1

Reply to comment 2

By updating the variable to

echo 'md5Value : " \${md5Value}

It's now producing error

hudson.AbortException: script returned exit code 126 //: Permission denied

Workaround

So the workaround is to put the bash shell into its own script and then the local variable assignment works as seen here -

#!groovy

node {

    try {

      stage('Test-Echo') {
          
        git poll: true,
          branch: "develop",
          credentialsId: 'xxx',
          url: 'https://yyy.org/here/and-there.git'

    
        sh """
            chmod +x ./jenkins/test/echo-test.sh
            
            ./jenkins/test/echo-test.sh
            
            exit "\${?}"
            
        """

       }
    } catch (e) {
        println (e.getMessage());
    }
}

And the bash script

#!/bin/bash

md5Value="y"

echo "md5Value : ${md5Value}"

change the triple double quote to single quote, and replace the single quote to double quote inside the script as following:

          sh '''
                #!/usr/bin/env bash

                md5Value='y'

                echo "md5Value : ${md5Value}"

           '''

I've tried the workaround suggested on serverfault.com and it worked. Try

echo "md5Value : ${'$'}md5Value"

For me, this works well.

sh """
   #!/usr/bin/env bash
   md5Value='y'
   echo md5Value : \$md5Value
"""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM