substituting different credentials() in a multibranch pipeline depending on branch

126 views
Skip to first unread message

Victor Sudakov

unread,
Nov 28, 2022, 1:05:07 AM11/28/22
to jenkins...@googlegroups.com
Dear Colleagues,

Can you please suggest a way to use different credentials depending on the Git
branch in a multi-branch pipeline? In other words, I would like to have

pipeline {
environment {
CLIENT_ID = credentials('PROD_CLIENT_ID')
}
...
}

if the Git branch is "main" and

pipeline {
environment {
CLIENT_ID = credentials('DEV_CLIENT_ID')
}
...
}

if the Git branch is "dev", or even "*".

What would be the correct Groovy syntax to substitute "PROD_CLIENT_ID" or
"DEV_CLIENT_ID" depending on the branch name?

--
Victor Sudakov
Systems Administrator
Streamline - Property Management Software
Website: www.streamlinevrs.com

John Patrick

unread,
Nov 28, 2022, 4:39:31 PM11/28/22
to jenkins...@googlegroups.com
Due to similar issues, I changed how I was doing it from a single MultiBranch project.
I've now separate projects for each environment, also use https://plugins.jenkins.io/cloudbees-folder to create prod and dev folders which container the environment specific values.
Have a groovy script that creates all the MultiBranch projects, prod folder and projects, dev folder and projects.

John


--
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-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/Y4ROJK3SmJsTTxhz%40vrs6.4vrs.com.

Victor Sudakov

unread,
Nov 28, 2022, 10:27:51 PM11/28/22
to jenkins...@googlegroups.com
Hello John,

I use folders too to organize jobs together and to keep credentials in separate
domains. However, the automatic creation of jobs based on git branches is
important to me therefore I use MultiBranch pipelines.

Do I understand correctly that you have scripted your own implementation of
MultiBranch which creates jobs and folders for your Git branches and tags
dynamically?
> To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAH9u10%3DxZf5Z8Ucq95mHm81GWKTDofYa3UkaET%2BBc%2B5Pf%2BJgTQ%40mail.gmail.com.

James Nord

unread,
Nov 29, 2022, 6:37:24 PM11/29/22
to jenkins...@googlegroups.com
Try 

credentials(env.BRANCH_NAME='main' ? 'PROD_CLIENT_ID' : 'DEV...')

or some syntax like that..

Victor Sudakov

unread,
Nov 29, 2022, 9:53:43 PM11/29/22
to jenkins...@googlegroups.com

Have you tested your suggestion?

For me,

SERVICE_KEY_S = credentials(env.BRANCH_NAME == 'STAGE-NOV28' ? 'SERVICE_KEY' : 'PROD_SERVICE_KEY')

thows the error:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 40: Internal function call parameters must be strings. @ line 40, column 32.
       SERVICE_KEY_S = credentials(env.BRANCH_NAME == 'STAGE-NOV28' ? 'SERVICE_KEY' : 'PROD_SERVICE_KEY')


I have also tried this syntax (found in the Internet somewhere):

MYTESTVAR = "${env.BRANCH_NAME == "main" ? "credentials('PROD_SERVICE_KEY')" : "credentials('SERVICE_KEY')"}"

and it does not work either. Maybe the issue is with single or double quotes. Anyway, a real-life working example is very welcome.

Victor Sudakov

unread,
Nov 29, 2022, 11:24:53 PM11/29/22
to jenkins...@googlegroups.com

This syntax may be operational, but I need more testing:

environment {

  SERVICE_KEY = credentials("${env.BRANCH_NAME == 'master' ? 'PROD_SERVICE_KEY' : 'SERVICE_KEY'}")

Arnaud bourree

unread,
Dec 1, 2022, 3:47:24 PM12/1/22
to jenkins...@googlegroups.com
Hi,

env.master'.equals(env.BRANCH_NAME) is String as 'master' is.
Try 'master'.equals(env.BRANCH_NAME) instead of 
env.BRANCH_NAME == 'master'

Victor Sudakov

unread,
Dec 1, 2022, 9:05:34 PM12/1/22
to jenkins...@googlegroups.com

Hello Arnaud,

Can you please post the whole line within environment{} as you suggest it should look?

Arnaud bourree

unread,
Dec 2, 2022, 1:33:37 AM12/2/22
to jenkins...@googlegroups.com

environment {

  SERVICE_KEY = credentials("${env.BRANCH_NAME.equals('master') ? 'PROD_SERVICE_KEY' : 'SERVICE_KEY'}")


Victor Sudakov

unread,
Dec 5, 2022, 4:11:54 AM12/5/22
to jenkins...@googlegroups.com

What would you do if you needed multiple branches with multiple choices of variables, like a "case" or "if ... else ... elif" operators?

Arnaud bourree

unread,
Dec 5, 2022, 7:21:36 AM12/5/22
to jenkins...@googlegroups.com
Hi,

So many ways to do the same:
- have credentials name with branch name in (my personal choise)
- define a pipeline method to switch/read map
- imbrication of ternary operator

Arnaud 

Reply all
Reply to author
Forward
0 new messages