How to set environment variable in multibranch pipeline?

2,337 views
Skip to first unread message

Mark Allison

unread,
Jun 13, 2017, 3:53:33 AM6/13/17
to Jenkins Users
I have a Jenkins multibranch pipeline project and I want to set an environment variable for all branches. Is this possible? I could only see a way to do it with the environment injector plugin at the branch level. I want to be able to do this at the project level because I want new feature branches to automatically inherit some environment variables.

Kevin Burnett

unread,
Jun 13, 2017, 8:36:07 AM6/13/17
to Jenkins Users
here's an example Jenkinsfile that sets an environment variable (REPO_URL) that applies to all stages. you can also use the withEnv step to set an environment variable for a certain block.

pipeline {
  agent
{ label 'docker' }
  environment
{
    REPO_URL
= 'www.my.repo'
 
}
  stages
{
    stage
('build') {
      steps
{
        sh
'env | grep REPO_URL'
     
}
   
}
 
}
}

Jenkinsfiles apply to all branches in a repo, so that part is handled implicitly.

Hope this helps,
KB

Mark Allison

unread,
Jun 14, 2017, 8:33:26 AM6/14/17
to Jenkins Users
Thanks for the reply, but I need to set the variable outside the pipeline script similar to the Environment Injector plugin. Is there a way to do that?

Cuong Tran

unread,
Jun 14, 2017, 7:15:34 PM6/14/17
to Jenkins Users
Why?

Dan Tran

unread,
Jun 14, 2017, 8:19:45 PM6/14/17
to Jenkins Users

Dale Lotts

unread,
Jun 19, 2017, 4:30:12 PM6/19/17
to Jenkins Users
I need this also so any suggestions on how to get it working would be appreciated.

We are automating publishing and need to pass in npm tokens, user name, passwords, etc in order for publishing to be successful.

Aseem Patni

unread,
Jul 9, 2018, 1:47:41 PM7/9/18
to Jenkins Users
The method mentioned by Kevin Burnett should be the right way to do this.

Stephen Connolly

unread,
Jul 10, 2018, 7:15:11 PM7/10/18
to jenkins...@googlegroups.com
On Mon, 19 Jun 2017 at 21:30, Dale Lotts <dale....@gmail.com> wrote:
I need this also so any suggestions on how to get it working would be appreciated.

We are automating publishing and need to pass in npm tokens, user name, passwords, etc in order for publishing to be successful.

that sounds like credentials...
 



On Wednesday, June 14, 2017 at 6:15:34 PM UTC-5, Cuong Tran wrote:
Why?

On Wednesday, June 14, 2017 at 5:33:26 AM UTC-7, Mark Allison wrote:
Thanks for the reply, but I need to set the variable outside the pipeline script similar to the Environment Injector plugin. Is there a way to do that?

On Tuesday, 13 June 2017 13:36:07 UTC+1, Kevin Burnett wrote:
here's an example Jenkinsfile that sets an environment variable (REPO_URL) that applies to all stages. you can also use the withEnv step to set an environment variable for a certain block.

pipeline {
  agent
{ label 'docker' }
  environment
{
    REPO_URL
= 'www.my.repo'
 
}
  stages
{
    stage
('build') {
      steps
{
        sh
'env | grep REPO_URL'
     
}
   
}
 
}
}

Jenkinsfiles apply to all branches in a repo, so that part is handled implicitly.

Hope this helps,
KB


On Tuesday, June 13, 2017 at 3:53:33 AM UTC-4, Mark Allison wrote:
I have a Jenkins multibranch pipeline project and I want to set an environment variable for all branches. Is this possible? I could only see a way to do it with the environment injector plugin at the branch level. I want to be able to do this at the project level because I want new feature branches to automatically inherit some environment variables.

--
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/91218c1a-612c-452d-8c3d-a396a7d6b3a3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages