I have a Jenkins Multibranch project, and I need to set some credentials depending on the Git branch I'm on right now. For example:
# If I'm in master
MY_VARIABLE = credentials("master-credential")
# If I'm in develop
MY_VARIABLE = credentials("develop-credential")
# If I'm in QA
MY_VARIABLE = credentials("qa-credential")
Right now, I've tried to name my variables with a different prefix and setting them up in the following way:
pipeline {
agent any
environment {
MY_VARIABLE = credentials("${env.BRANCH_NAME}-credential")
}
stages {
stage("Start") {
steps {
# use MY_VARIABLE on my steps
}
}
But it doesn't work.
I think I might be able to use different domains for my credentials and then specify the domain when I set them, but I haven't found in the docs how to specify the domains on the Jenkinsfile.
I would really appreciate if someone could help me.
Thanks!
CodePudding user response:
While using the convenience function for credentials
in the environment
directive is cleaner, for interpolated strings resolved like this in the credentialsId
you would need to use the withCredentials
plugin and step method. Based on the question, I will assume the type bindings you want are for string
:
steps {
withCredentials([string(credentialsId: "${env.BRANCH_NAME}-credential", variable: 'MY_VARIABLE')]) {
# use MY_VARIABLE on my steps
}
}
Check the documentation for more information.