Hi all,

I am using withCredentials in pretty much every one of my stages and I 
would like to know if there is a way to only call it once.
I found a topic [JENKINS-37823] mentionning the wrapper section (now 
deprecated in favor of the options directive) but options does not 
recognize withCredentials as a valid type.

I also would like to know if the environment directives as any way of 
defining environment variables depending on a condition (move the start of 
the script inside the environment pipeline directive).

Here is a simplified version of my pipeline script.

```
#!groovy

env.VAR1='value1'
env.VAR2='value2'

if (platform ==~ /.*production-srv.*/) {
  env.VAR1='valueA'
  env.VAR2='valueB'
}

pipeline {
  environment {
    // Can't use this
  }

  parameters {
    // some params
  }

  options {
    buildDiscarder(logRotator(numToKeepStr:'10'))
  }

  agent {
    label 'master'
  }

  stages {

    stage('init') {
      steps {
        // blabla
      }
    }

    stage('Step 2') {
      steps {
        withCredentials([[$class          : 'UsernamePasswordMultiBinding', 
credentialsId: id,
                          usernameVariable: 'USERNAME', passwordVariable: 
'PASSWORD']]) {
          // Do first work
        }
      }
    }

    stage('Step 3') {
        withCredentials([[$class          : 'UsernamePasswordMultiBinding', 
credentialsId: id,
                          usernameVariable: 'USERNAME', passwordVariable: 
'PASSWORD']]) {
          // Second part
        }
      }
    }
    
    stage('Step 4') {
        withCredentials([[$class          : 'UsernamePasswordMultiBinding', 
credentialsId: id,
                          usernameVariable: 'USERNAME', passwordVariable: 
'PASSWORD']]) {
          // Last part
        }
      }
    }
  }
}
```

My script works but I would like it to be more concise and use the pipeline 
syntax maximally.

Thank for any help,
Côme

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/47c79ab8-42f4-4074-b8b1-a5f743eefc0d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to