[JIRA] (JENKINS-47881) Declarative Pipeline: add option to set environment variables based on user passed boolean parameter in the environment directive

2 views
Skip to first unread message

tdh@nemlig.com (JIRA)

unread,
May 9, 2018, 3:49:02 AM5/9/18
to jenkinsc...@googlegroups.com
Timothy Harris commented on Improvement JENKINS-47881
 
Re: Declarative Pipeline: add option to set environment variables based on user passed boolean parameter in the environment directive

Well, what if you want to have a kind of pipeline prepare where some environment variables are set based on some parameters.

// Determine target environment based on branch and set as variable ${TARGET_ENV}
TARGET_ENV = "${params.PULL_REQUEST_TO_BRANCH.is('integration-branch-team1') ? 'team1' : 'local'}"
TARGET_ENV = "${params.PULL_REQUEST_TO_BRANCH.is('integration-branch-team2') ? 'team2' : 'local'}"
TARGET_ENV = "${params.PULL_REQUEST_TO_BRANCH.is('develop') ? 'dev' : 'local'}"
// Determine test suite based on branch and set as variable ${TEST_SUITE}
TEST_SUITE = "${params.PULL_REQUEST_TO_BRANCH.matches('^integration-branch-team(
d+)$') ? 'smoke_test.xml' : ''}"

TEST_SUITE = "${params.PULL_REQUEST_TO_BRANCH.matches('^develop$') ? 'allTests.xml' : ''}"

Or something similar. This will complain about duplicate environment variable.

It would be a nice feature to be able to have a script step in the environment directive or some way to do some logic...

Add Comment Add Comment
 
This message was sent by Atlassian JIRA (v7.3.0#73011-sha1:3c73d0e)
Atlassian logo

tdh@nemlig.com (JIRA)

unread,
May 9, 2018, 5:51:02 AM5/9/18
to jenkinsc...@googlegroups.com
Timothy Harris updated an issue
 
Jenkins / Improvement JENKINS-47881
Change By: Timothy Harris
Comment:
Well, what if you want to have a kind of pipeline prepare where some environment variables are set based on some parameters.

{{// Determine target environment based on branch and set as variable $\{TARGET_ENV}}}
{{TARGET_ENV = "$\{params.PULL_REQUEST_TO_BRANCH.is('integration-branch-team1') ? 'team1' : 'local'}"}}
{{TARGET_ENV = "$\{params.PULL_REQUEST_TO_BRANCH.is('integration-branch-team2') ? 'team2' : 'local'}"}}
{{TARGET_ENV = "$\{params.PULL_REQUEST_TO_BRANCH.is('develop') ? 'dev' : 'local'}"}}
{{// Determine test suite based on branch and set as variable $\{TEST_SUITE}}}
{{TEST_SUITE = "$\{params.PULL_REQUEST_TO_BRANCH.matches('^integration-branch-team(\\d+)$') ? 'smoke_test.xml' : ''}"}}
{{TEST_SUITE = "$\{params.PULL_REQUEST_TO_BRANCH.matches('^develop$') ? 'allTests.xml' : ''}"}}

Or something similar. This will complain about duplicate environment variable.

It would be a nice feature to be able to have a script step in the environment directive or some way to do some logic...

koen.bruyninckx@kbc.be (JIRA)

unread,
Jun 1, 2018, 9:32:02 AM6/1/18
to jenkinsc...@googlegroups.com
Koen Bruyninckx edited a comment on Improvement JENKINS-47881
 
Re: Declarative Pipeline: add option to set environment variables based on user passed boolean parameter in the environment directive
[~abayer] I've picked up your suggestion but to my surprise the following does not seem to be working, i.e. the BRANCH_NAME which I would like to assign to TARGET in case of a CI build, always results to null:
{code:java}
environment {
  TARGET = "${changeRequest() ? CHANGE_TARGET : BRANCH_NAME}"
}{code}
 In case of a pull request (condition evaluating to true) this works perfectly.

Any help would be much appreciated!

 

 

 
K.

koen.bruyninckx@kbc.be (JIRA)

unread,
Jun 1, 2018, 9:32:02 AM6/1/18
to jenkinsc...@googlegroups.com

Andrew Bayer I've picked up your suggestion but to my surprise the following does not seem to be working, i.e. the BRANCH_NAME which I would like to assign to TARGET in case of a CI build, always results to null:

environment {
  TARGET = "${changeRequest() ? CHANGE_TARGET : BRANCH_NAME}"
}

 In case of a pull request (condition evaluating to true) this works perfectly.

Any help would be much appreciated!

 

 

 

koen.bruyninckx@kbc.be (JIRA)

unread,
Jun 1, 2018, 9:34:02 AM6/1/18
to jenkinsc...@googlegroups.com
Koen Bruyninckx edited a comment on Improvement JENKINS-47881
[~abayer] I've picked up your suggestion but to my surprise the following does not seem to be working, i.e. the BRANCH_NAME which is an environment var that I would like to assign to TARGET in case of a CI build, always results to null:
{code:java}

environment {
  TARGET = "${changeRequest() ? CHANGE_TARGET : BRANCH_NAME}"
}{code}
 In case of a pull request (condition evaluating to true)
this works perfectly , the CHANGE_TARGET is correctly assigned to TARGET .


Any help would be much appreciated!

K.

koen.bruyninckx@kbc.be (JIRA)

unread,
Jun 1, 2018, 9:45:02 AM6/1/18
to jenkinsc...@googlegroups.com
Koen Bruyninckx reopened an issue
 

There seems to be a bug in resolving environment variables in a condition:

Consider CHANGE_REQUEST environment variable filled in by a pull request, and BRANCH_NAME, the environment variable for the branch

environment {
   TARGET = "${changeRequest() ? CHANGE_TARGET:BRANCH_NAME}"
}

TARGET becomes null in case changeRequest() returns false (CI build)

In case of a pull request all works well.

It's as if when CHANGE_TARGET becomes null, no matter the value in BRANCH_NAME, the null will be assigned. 

 

 

Change By: Koen Bruyninckx
Resolution: Won't Fix
Status: Resolved Reopened

koen.bruyninckx@kbc.be (JIRA)

unread,
Jun 1, 2018, 10:02:03 AM6/1/18
to jenkinsc...@googlegroups.com
Koen Bruyninckx updated an issue
Change By: Koen Bruyninckx
Comment:
There seems to be a bug in resolving environment variables in a condition:

Consider CHANGE_REQUEST environment variable filled in by a pull request, and BRANCH_NAME, the environment variable for the branch
{code:java}

environment {
   TARGET = "${changeRequest() ? CHANGE_TARGET:BRANCH_NAME}"
}
{code}

TARGET becomes null in case changeRequest() returns false (CI build)

In case of a pull request all works well.

It's as if when CHANGE_TARGET becomes null, no matter the value in BRANCH_NAME, the null will be assigned. 

 

 

koen.bruyninckx@kbc.be (JIRA)

unread,
Jun 1, 2018, 10:34:02 AM6/1/18
to jenkinsc...@googlegroups.com
Koen Bruyninckx edited a comment on Improvement JENKINS-47881
[~abayer] I've picked up your suggestion but to my surprise the following does not seem to be working, i.e. the BRANCH_NAME which is an environment var that I would like to assign to TARGET in case of a CI build, always results to null:
{code:java}
environment {
  TARGET = "${changeRequest() ? CHANGE_TARGET : BRANCH_NAME}"
}{code}
 In case of a pull request (condition evaluating I noticed that the call to true changeRequest( ) does not seem to be working in this condition , the CHANGE_TARGET is correctly assigned but still always resolves to TARGET true .

Any help would Would be much appreciated! awesome if resolved.

K.

andrew.bayer@gmail.com (JIRA)

unread,
Oct 15, 2018, 7:13:02 AM10/15/18
to jenkinsc...@googlegroups.com

Koen Bruyninckx - looks to me like you're trying to call the changeRequest when condition inside of environment - that won't work. Try something like "${env.CHANGE_ID != null ? env.CHANGE_TARGET : env.BRANCH_NAME}"

This message was sent by Atlassian Jira (v7.11.2#711002-sha1:fdc329d)

andrew.bayer@gmail.com (JIRA)

unread,
Nov 7, 2018, 12:23:01 PM11/7/18
to jenkinsc...@googlegroups.com

koen.bruyninckx@kbc.be (JIRA)

unread,
Nov 7, 2018, 12:32:02 PM11/7/18
to jenkinsc...@googlegroups.com

Hi Andrew, sorry I missed your previous post, I did it indeed similar to your proposal, and it works thx!

andrew.bayer@gmail.com (JIRA)

unread,
Nov 7, 2018, 12:59:01 PM11/7/18
to jenkinsc...@googlegroups.com

dmladek@gmail.com (JIRA)

unread,
Apr 17, 2019, 7:34:02 PM4/17/19
to jenkinsc...@googlegroups.com
Daniel Mladek commented on Improvement JENKINS-47881
 
Re: Declarative Pipeline: add option to set environment variables based on user passed boolean parameter in the environment directive

Any update on its status? I mean as suggested  by Andrew Bayer it is working as of Jenkins v. 2.150.3, so it looks like it has been released.

dmladek@gmail.com (JIRA)

unread,
Apr 18, 2019, 6:37:05 PM4/18/19
to jenkinsc...@googlegroups.com

Just can't figure out where better to ask this question:

How can I conditionally set the triggers or options parameters? 

The same logic as suggested for environment variables section can be applied inside options section, e,g. to some values of logRotator() function like this:
numToKeepStr: "${params.IS_IT_NIGHTLY ? '4' : '2'}"

But how to make it conditional on entire call of the logRotator() function or in triggers section when I would like to call poolSCM() method with different intervals for CI and NIGHTLY builds?

I'm looking for something like this:

triggers {
  if (params.IS_IT_NIGHTLY==true) {
      pollSCM('''# Once a day, between 12:00AM and 1:59AM
H H(0-1) * * *  ''')
  } else {
      pollSCM('''# every five minutes
H/5 * * * *  ''')
  }
}

If I do that, then I'll get this error:

WorkflowScript: 25: Expected a trigger @ line 25, column 5.
       if (params.IS_IT_NIGHTLY==true) {
       ^
1 error

The whole point of having parameterized Jenkins pipeline jobs

is that most of our the Jenkinsfiles for NIGHTLY and CI builds looks almost identical, except the:

  • scheduler triggers
  • SVN checkout (which differs in checkout strategy, depth of checkout, and other minor details)
  • and of course then a true parametrized split weather to perform a limited or fuld build process with relevant tests and deployment locations.

I can't see as it is now implemented how can I achieve this.

Thanks,
-d

dmladek@gmail.com (JIRA)

unread,
Apr 18, 2019, 6:42:01 PM4/18/19
to jenkinsc...@googlegroups.com
Daniel Mladek edited a comment on Improvement JENKINS-47881
Just can't figure out where better to ask this question:
h4. How can I conditionally set the triggers or options parameters? 


The same logic as suggested for environment variables section can be applied inside {{options{}}} section, e,g. to some values of {{logRotator()}} function like this:
{{numToKeepStr: "${params.IS_IT_NIGHTLY ? '4' : '2'}"}}

But how to make it conditional on entire call of the {{logRotator()}} function or in {{triggers}} section when I would like to call {{poolSCM()}} method with different intervals for CI and NIGHTLY builds (or not to call it at all and use just {{cron()}} or whatever ?


I'm looking for something like this:
{code:java}

triggers {
  if (params.IS_IT_NIGHTLY==true) {
      pollSCM('''# Once a day, between 12:00AM and 1:59AM
H H(0-1) * * *  ''')
  } else {
      pollSCM('''# every five minutes
H/5 * * * *  ''')
  }
}
{code}
If I do that, then {color:#FF0000}*I'll get this error*{color}:
{code:java}

WorkflowScript: 25: Expected a trigger @ line 25, column 5.
       if (params.IS_IT_NIGHTLY==true) {
       ^
1 error
{code}

h3.The whole point of having parameterized Jenkins pipeline jobs

is that most of our the Jenkinsfiles for NIGHTLY and CI builds looks almost identical, except the:
* scheduler triggers
* SVN checkout (which differs in checkout strategy, depth of checkout, and other minor details)
* and of course then a true parametrized split weather to perform a limited or fuld build process with relevant tests and deployment locations.


I can't see as it is now implemented how can I achieve this.

Thanks,
-d

georg.koch@gmail.com (JIRA)

unread,
Jul 24, 2019, 4:23:03 AM7/24/19
to jenkinsc...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages