0

I've been coming here for years and usually find the answer i seek but this time I have a rather specific question.

I am building a pipeline that runs through a set of steps in one pipeline with a 3 tier path path to prod, using a choice, couple of string, and withCredentials param. Which work fine, up until my prod deploy where it's failing an "if else" test.

I have a (secret text) jenkins credential with a basic password, that I am attempting to compare to the string entered on build start. I have checked the spell with basic usage and it works as expected. BUT when I add it to my full pipeline it fails.

I am thinking it is on account of my not using the correct syntax with steps, script, node, or order...? This is a new space for me and I'm hoping that someone who's spent more time in this code space will see my error. Thanks! In advance!

Fails:

...
      stage('Deploy_PROD') {
          when {
            expression { params.DEPLOY_TO == 'Deploy_PROD'}
          }
          steps{
                withCredentials([string(credentialsId: '${creds}', variable: 'SECRET')]) {
                script {
                    if  ('${password}' == '$SECRET') {
                        sh 'echo yes'
                    }  else {
                        sh 'echo no'
                }
            }
        }
      }
    }

Works:

stage('example')
        node {
            withCredentials([string(credentialsId: '${creds}', variable: 'SECRET')]) {
                if  ('${password}' == '$SECRET') { 
                    sh 'echo "test"'
                }  else {
                    sh 'echo ${password}'
                }
            }
        }
iscsi
  • 1
  • 1
  • 2
    Your `if` is checking if the literal string `${password}` equals the literal string `$SECRET`, which will always be false. I would start with fixing that, since your "Works" example will not even work until you do that. – Matthew Schuchard Feb 12 '19 at 17:03

2 Answers2

0

Solution would be

if  (password == SECRET) { 

Also a recommended read - What's the difference of strings within single or double quotes in groovy?

hakamairi
  • 4,464
  • 4
  • 30
  • 53
0

I ended up using the withCredentials option with our AD server that allowed finer control over users' access to deploy to controlled environments. thanks for the assist.

iscsi
  • 1
  • 1