difference between sh 'docker login -u ${env.myUsername} -p ${env.myPassword}' and sh "docker login -u ${env.myUsername} -p ${env.myPassword}" in decl

In Jenkins Declarative Pipeline syntax, there is a difference between using single quotes (') and double quotes (") when defining a shell command.

When using single quotes ('), Groovy treats the entire string as a literal and does not perform variable expansion. This means that any variables or environment variables enclosed in single quotes will not be expanded and will be treated as plain text.

On the other hand, when using double quotes ("), Groovy will perform variable expansion and replace any variable or environment variable enclosed in ${} with its corresponding value. This allows you to include variables and environment variables in your shell command.

Therefore, in the context of a Declarative Pipeline script, the difference between the following two lines:

sh 'docker login -u ${env.myUsername} -p ${env.myPassword}'
sh "docker login -u ${env.myUsername} -p ${env.myPassword}"

The first line uses single quotes ('), which means that ${env.myUsername} and ${env.myPassword} will not be expanded, and will be passed to the shell command as literal strings. This is likely to result in a failed login attempt because the actual values of ${env.myUsername} and ${env.myPassword} will not be passed to the docker login command.

The second line uses double quotes ("), which means that ${env.myUsername} and ${env.myPassword} will be expanded and replaced with their corresponding values before the docker login command is executed. This allows the docker login command to use the actual values of ${env.myUsername} and ${env.myPassword} during the login attempt. Therefore, the second line is the correct way to pass the values of ${env.myUsername} and ${env.myPassword} to the docker login command.

Did you find this article valuable?

Support Gaurav-Jethuri by becoming a sponsor. Any amount is appreciated!