jenkinsgroovy

Jenkinsfile won't substitute ${USERNAME} with actual username


I have created a very simple parameterized project using Jenkins. Its parameter name is USERNAME and its default value is John.

parameterized project

Also, I have created a pipeline script as follows:

pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                echo 'Hello ${USERNAME}'
            }
        }
    }
}

pipeline

This is the output:

Started by user
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in C:\ProgramData\Jenkins\.jenkins\workspace\TCMS\test\test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] echo
Hello ${USERNAME}
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Unfortunately as you can see the variable ${USERNAME} is not substituted with John.

Also, I tried to use ${params.USERNAME} instead of ${USERNAME} but the problem persists.

What am I fixing?


Solution

  • I had to add the parameters section in the pipeline script and use double quotes "" instead of '' in echo command:

    pipeline {
        agent any
        parameters {
            string(name: 'USERNAME', defaultValue: 'John')
        }
        stages {
            stage('Hello') {
                steps {
                    echo "Hello ${params.USERNAME}"
                }
            }
        }
    }