0

I am using bash script inside Groovy in the Jenkins pipeline. Jenkins job is parameterized and using a password parameter option named "PASSWORD". The issue is when password contains "$" sign, then it is getting escaped in the bash script. For example: Actual password inputed is "ppa$$worddd" but in the bash script $PASSWORD value is "ppa$worddd". As we could see one "$" is removed. Please look at the code below. Please help

def password = params.get('PASSWORD')
stage('execute') {
            steps {
                script {
                    println password // - Here value is "ppa$$worddd" as expected
                    withCredentials([string(credentialsId: 'ID', variable: 'C_ID'),string(credentialsId: 'SE', variable: 'C_SE')]) {
                        if (!starsValue.isEmpty()) {
                            sh '''
                              cd Scripts
                              echo $PASSWORD // - Here value is "ppa$worddd" .As seen one "$" is removed
                            '''
                        } 
                    }
                }
            }
        }
4
  • 1
    Use single quotes, replace "ppa$worddd" by 'ppa$worddd'! Commented Dec 27, 2024 at 18:12
  • How to do that, $PASSWORD is the variable Commented Dec 27, 2024 at 22:31
  • 1
    @Karthick, where you are setting PASSWORD environment variable? your code snippet is not stowing this. according to documentation you can do something like environment { PASSWORD=password } before sh command to set env variable jenkins.io/doc/book/pipeline/jenkinsfile/… Commented Dec 28, 2024 at 21:28
  • something along: def PASSWORD = System.getenv("PASSWORD") if (PASSWORD) { println "The value of PASSWORD is: ${PASSWORD}"} else { println "PASSWORD is not set." } should suffice unless you've a specific use case for spawning to the shell Commented Dec 29, 2024 at 23:20

1 Answer 1

1

Under some very specific circumstances Jenkins does variable expansion. Syntax is arbitrary, but somewhat documented here. I think at some point they wanted to ignore $$ combinations, but then some random patch "improved" on that idea and now we have what we have.

There is no way to disable this variable expansion, you can only avoid it by doing seemingly equivalent things that for some reason bypass it:

withEnv(["PASSWORD=${params.PASSWORD}"]){
    sh 'echo $PASSWORD'
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.