I have a jenkins stage which requires different credentials based on some parameters. The contents stays the same for each stage, the only change is the credentials, so I could achieve this by just having multiple stages with single credentials then just using those, but obviously not ideal.
I'm trying it essentially like this but jenkins doesn't like it
stage('test execute') {
steps {
withCredentials([
if (params.Env == 'env1') {
usernamePassword(credentialsId: 'creds1', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')
} else if (params.Env == 'env2') {
usernamePassword(credentialsId: 'creds2', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')
}
])
}
}
I get the below error though
WorkflowScript: 25: unexpected token: if @ line 25, column 21.
if (params.Env == 'env1') {
^
so I'm not sure if there's any way to add conditional logic to withcredentials, or if I just have to have multiple stages with essentially the same code