Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-47881

Declarative Pipeline: add option to set environment variables based on user passed boolean parameter in the environment directive

    Details

    • Similar Issues:

      Description

      Hi,

      Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

       

      pipeline{
      agent any
      environment {
      
      /*Option1*/
      condition $params.some_bool_parameter{CC = "1"}
      
      /* Option2 variable will be set to "1" only if the boolean value is true */
      CC = $params.some_bool_parameter : "1"
      
      /* This is currently supported */
      CC = $params.some_non_bool_parameter
      }
      }
      

       

        Attachments

          Activity

          progovoy Pavel Rogovoy created issue -
          progovoy Pavel Rogovoy made changes -
          Field Original Value New Value
          Description Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           

          {{}}{{pipeline \{}}

          {{agent any}}

          {{environment \{}}

          {{  /* Option1 */}}

          {{  condition $params.some_bool_parameter\{}}

          {{    CC = "1"}}

          {{   }}}

          {{   /*Option2 variable will be set to "1" onlt if the boolean value if true*/}}

          {{   CC = $params.some_bool_parameter : "1"}}

          {{  }}

          {{  /* This is currently supported */}}

          {{  CC = $params.some_*non*_bool_parameter}}

          {{}}}

           
          Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           

          {{pipeline \{}}

          {{agent any}}

          {{environment \{}}

          {{  /* Option1 */}}

          {{  condition $params.some_bool_parameter\{}}

          {{    CC = "1"}}

          {{   }}}

          {{   /*Option2 variable will be set to "1" onlt if the boolean value if true*/}}

          {{   CC = $params.some_bool_parameter : "1"}}

          {{  }}

          {{  /* This is currently supported */}}

          {{  CC = $params.some_*non*_bool_parameter}}

          {{}}}

           
          progovoy Pavel Rogovoy made changes -
          Description Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           

          {{pipeline \{}}

          {{agent any}}

          {{environment \{}}

          {{  /* Option1 */}}

          {{  condition $params.some_bool_parameter\{}}

          {{    CC = "1"}}

          {{   }}}

          {{   /*Option2 variable will be set to "1" onlt if the boolean value if true*/}}

          {{   CC = $params.some_bool_parameter : "1"}}

          {{  }}

          {{  /* This is currently supported */}}

          {{  CC = $params.some_*non*_bool_parameter}}

          {{}}}

           
          Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           

          {{pipeline \{}}

          {{agent any}}

          {{environment \{}}

          {{  /* *Option1* */}}

          {{  condition $params.some_bool_parameter\{}}

          {{    CC = "1"}}

          {{   }}}

          {{   /*}}

          {{   ** Option2 variable will* }}

          {{   ** be set to "1" onlt if the boolean value if true*}}

          {{   ***/}}

          {{   CC = $params.some_bool_parameter : "1"}}

          {{  }}

          {{  /* This is currently supported */}}

          {{  CC = $params.some_*non*_bool_parameter}}

          {{}}}

           
          progovoy Pavel Rogovoy made changes -
          Description Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           

          {{pipeline \{}}

          {{agent any}}

          {{environment \{}}

          {{  /* *Option1* */}}

          {{  condition $params.some_bool_parameter\{}}

          {{    CC = "1"}}

          {{   }}}

          {{   /*}}

          {{   ** Option2 variable will* }}

          {{   ** be set to "1" onlt if the boolean value if true*}}

          {{   ***/}}

          {{   CC = $params.some_bool_parameter : "1"}}

          {{  }}

          {{  /* This is currently supported */}}

          {{  CC = $params.some_*non*_bool_parameter}}

          {{}}}

           
          Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           

          {{pipeline \{}}

          {{agent any}}

          {{environment \{}}

          {{  /* *Option1* */}}

          {{  condition $params.some_bool_parameter\{}}

          {{    CC = "1"}}

          {{   }}}

          {{   /*}}

                ** Option2 variable will*

          {{   ** be set to "1" onlt if the boolean value if true*}}

          {{   ***/}}

          {{   CC = $params.some_bool_parameter : "1"}}

          {{  }}

          {{  /* This is currently supported */}}

          {{  CC = $params.some_*non*_bool_parameter}}

          {{}}}

           
          progovoy Pavel Rogovoy made changes -
          Description Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           

          {{pipeline \{}}

          {{agent any}}

          {{environment \{}}

          {{  /* *Option1* */}}

          {{  condition $params.some_bool_parameter\{}}

          {{    CC = "1"}}

          {{   }}}

          {{   /*}}

                ** Option2 variable will*

          {{   ** be set to "1" onlt if the boolean value if true*}}

          {{   ***/}}

          {{   CC = $params.some_bool_parameter : "1"}}

          {{  }}

          {{  /* This is currently supported */}}

          {{  CC = $params.some_*non*_bool_parameter}}

          {{}}}

           
          Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           

          {{pipeline \{}}

          {{agent any}}

          {{environment \{}}

          {{  /* *Option1* */}}

          {{  condition $params.some_bool_parameter\{}}

          {{    CC = "1"}}

          {{   }}}

          {{   /*}}

                ** Option2 variable will*

          {{   ** be set to "1" only if the boolean value if true*}}

          {{   ***/}}

          {{   CC = $params.some_bool_parameter : "1"}}

          {{  }}

          {{  /* This is currently supported */}}

          {{  CC = $params.some_*non*_bool_parameter}}

          {{}}}

           
          progovoy Pavel Rogovoy made changes -
          Description Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           

          {{pipeline \{}}

          {{agent any}}

          {{environment \{}}

          {{  /* *Option1* */}}

          {{  condition $params.some_bool_parameter\{}}

          {{    CC = "1"}}

          {{   }}}

          {{   /*}}

                ** Option2 variable will*

          {{   ** be set to "1" only if the boolean value if true*}}

          {{   ***/}}

          {{   CC = $params.some_bool_parameter : "1"}}

          {{  }}

          {{  /* This is currently supported */}}

          {{  CC = $params.some_*non*_bool_parameter}}

          {{}}}

           
          Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           

          {{pipeline \{}}

          {{agent any}}

          {{environment \{}}

          {{  /* *Option1* */}}

          {{  condition $params.some_bool_parameter\{}}{{CC = "1"}}}

          {{   /*}}

                ** Option2 variable will*

          {{   ** be set to "1" only if the boolean value if true*}}

          {{   ***/}}

          {{   CC = $params.some_bool_parameter : "1"}}

          {{  }}

          {{  /* This is currently supported */}}

          {{  CC = $params.some_*non*_bool_parameter}}

          {{}}}

           
          progovoy Pavel Rogovoy made changes -
          Description Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           

          {{pipeline \{}}

          {{agent any}}

          {{environment \{}}

          {{  /* *Option1* */}}

          {{  condition $params.some_bool_parameter\{}}{{CC = "1"}}}

          {{   /*}}

                ** Option2 variable will*

          {{   ** be set to "1" only if the boolean value if true*}}

          {{   ***/}}

          {{   CC = $params.some_bool_parameter : "1"}}

          {{  }}

          {{  /* This is currently supported */}}

          {{  CC = $params.some_*non*_bool_parameter}}

          {{}}}

           
          Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           
          {code:java}
          {{pipeline \{}}

          {{agent any}}

          {{environment \{}}

          {{  /* *Option1* */}}

          {{  condition $params.some_bool_parameter\{}}{{CC = "1"}}}

          {{   /*}}

                ** Option2 variable will*

          {{   ** be set to "1" only if the boolean value if true*}}

          {{   ***/}}

          {{   CC = $params.some_bool_parameter : "1"}}

          {{  }}

          {{  /* This is currently supported */}}

          {{  CC = $params.some_*non*_bool_parameter}}

          {{}}}
          {code}

           
          progovoy Pavel Rogovoy made changes -
          Description Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           
          {code:java}
          {{pipeline \{}}

          {{agent any}}

          {{environment \{}}

          {{  /* *Option1* */}}

          {{  condition $params.some_bool_parameter\{}}{{CC = "1"}}}

          {{   /*}}

                ** Option2 variable will*

          {{   ** be set to "1" only if the boolean value if true*}}

          {{   ***/}}

          {{   CC = $params.some_bool_parameter : "1"}}

          {{  }}

          {{  /* This is currently supported */}}

          {{  CC = $params.some_*non*_bool_parameter}}

          {{}}}
          {code}

           
          Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           
          {code:java}
          pipeline{
          agent any
          environment {
           /*Option1*/
          condition $params.some_bool_parameter{CC = "1"}

          /* Option2 variable will be set to "1" only if the boolean value if true */
          CC = $params.some_bool_parameter : "1"

          /* This is currently supported */

          CC = $params.some_*non*_bool_parameter
          {code}

           
          progovoy Pavel Rogovoy made changes -
          Description Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           
          {code:java}
          pipeline{
          agent any
          environment {
           /*Option1*/
          condition $params.some_bool_parameter{CC = "1"}

          /* Option2 variable will be set to "1" only if the boolean value if true */
          CC = $params.some_bool_parameter : "1"

          /* This is currently supported */

          CC = $params.some_*non*_bool_parameter
          {code}

           
          Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           
          {code:java}
          pipeline{
          agent any
          environment {
           /*Option1*/
          condition $params.some_bool_parameter{CC = "1"}

          /* Option2 variable will be set to "1" only if the boolean value is true */
          CC = $params.some_bool_parameter : "1"

          /* This is currently supported */

          CC = $params.some_*non*_bool_parameter
          {code}

           
          progovoy Pavel Rogovoy made changes -
          Description Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           
          {code:java}
          pipeline{
          agent any
          environment {
           /*Option1*/
          condition $params.some_bool_parameter{CC = "1"}

          /* Option2 variable will be set to "1" only if the boolean value is true */
          CC = $params.some_bool_parameter : "1"

          /* This is currently supported */

          CC = $params.some_*non*_bool_parameter
          {code}

           
          Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           
          {code:java}
          pipeline{
          agent any
          environment {

           /*Option1*/
          condition $params.some_bool_parameter{CC = "1"}

          /* Option2 variable will be set to "1" only if the boolean value is true */
          CC = $params.some_bool_parameter : "1"

          /* This is currently supported */
          CC = $params.some_*non*_bool_parameter
          {code}

           
          progovoy Pavel Rogovoy made changes -
          Description Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           
          {code:java}
          pipeline{
          agent any
          environment {

           /*Option1*/
          condition $params.some_bool_parameter{CC = "1"}

          /* Option2 variable will be set to "1" only if the boolean value is true */
          CC = $params.some_bool_parameter : "1"

          /* This is currently supported */
          CC = $params.some_*non*_bool_parameter
          {code}

           
          Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           
          {code:java}
          pipeline{
          agent any
          environment {

          /*Option1*/
          condition $params.some_bool_parameter{CC = "1"}

          /* Option2 variable will be set to "1" only if the boolean value is true */
          CC = $params.some_bool_parameter : "1"

          /* This is currently supported */
          CC = $params.some_non_bool_parameter
          {code}

           
          progovoy Pavel Rogovoy made changes -
          Description Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           
          {code:java}
          pipeline{
          agent any
          environment {

          /*Option1*/
          condition $params.some_bool_parameter{CC = "1"}

          /* Option2 variable will be set to "1" only if the boolean value is true */
          CC = $params.some_bool_parameter : "1"

          /* This is currently supported */
          CC = $params.some_non_bool_parameter
          {code}

           
          Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           
          {code:java}
          pipeline{
          agent any
          environment {

          /*Option1*/
          condition $params.some_bool_parameter{CC = "1"}

          /* Option2 variable will be set to "1" only if the boolean value is true */
          CC = $params.some_bool_parameter : "1"

          /* This is currently supported */
          CC = $params.some_non_bool_parameter
          {code}

          }
          }
           
          progovoy Pavel Rogovoy made changes -
          Description Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           
          {code:java}
          pipeline{
          agent any
          environment {

          /*Option1*/
          condition $params.some_bool_parameter{CC = "1"}

          /* Option2 variable will be set to "1" only if the boolean value is true */
          CC = $params.some_bool_parameter : "1"

          /* This is currently supported */
          CC = $params.some_non_bool_parameter
          {code}

          }
          }
           
          Hi,

          Currently there is no option to set an environment variable based on boolean value within the environment directive. I think it is crucial to implement support for something like this:

           
          {code:java}
          pipeline{
          agent any
          environment {

          /*Option1*/
          condition $params.some_bool_parameter{CC = "1"}

          /* Option2 variable will be set to "1" only if the boolean value is true */
          CC = $params.some_bool_parameter : "1"

          /* This is currently supported */
          CC = $params.some_non_bool_parameter
          }
          }
          {code}

           
          progovoy Pavel Rogovoy made changes -
          Summary  Declarative Pipeline: add option to set variables based on user passed parameter  Declarative Pipeline: add option to set environment variables based on user passed boolean parameter in the environment directive
          abayer Andrew Bayer made changes -
          Component/s pipeline-model-definition-plugin [ 21706 ]
          Component/s pipeline [ 21692 ]
          abayer Andrew Bayer made changes -
          Assignee Andrew Bayer [ abayer ]
          Hide
          abayer Andrew Bayer added a comment -

          You can do something like:

          CC = "${params.some_bool_parameter ? '1' : ''}"
          

          Which will set CC to 1 if some_bool_parameter is true, and will set it to '' otherwise. If that's not sufficient and you need to be able to have the variable be unset completely, you may need to use Scripted Pipeline instead.

          Show
          abayer Andrew Bayer added a comment - You can do something like: CC = "${params.some_bool_parameter ? '1' : ''}" Which will set CC to 1 if some_bool_parameter is true, and will set it to '' otherwise. If that's not sufficient and you need to be able to have the variable be unset completely, you may need to use Scripted Pipeline instead.
          abayer Andrew Bayer made changes -
          Status Open [ 1 ] Resolved [ 5 ]
          Resolution Won't Fix [ 2 ]
          nemligtim Timothy Harris made changes -
          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... ]
          Hide
          koen_bruyninckx Koen Bruyninckx added a comment - - edited

          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 is an environment var that I would like to assign to TARGET in case of a CI build, always results to null:

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

          I noticed that the call to changeRequest() does not seem to be working in this condition, but still always resolves to true.

          Would be awesome if resolved.

          K.

          Show
          koen_bruyninckx Koen Bruyninckx added a comment - - edited 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 is an environment var that I would like to assign to TARGET in case of a CI build, always results to null: environment {   TARGET = "${changeRequest() ? CHANGE_TARGET : BRANCH_NAME}" } I noticed that the call to changeRequest() does not seem to be working in this condition, but still always resolves to true. Would be awesome if resolved. K.
          koen_bruyninckx Koen Bruyninckx made changes -
          Resolution Won't Fix [ 2 ]
          Status Resolved [ 5 ] Reopened [ 4 ]
          koen_bruyninckx Koen Bruyninckx made changes -
          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. 

           

            ]
          Hide
          abayer Andrew Bayer added a comment -

          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}"

          Show
          abayer Andrew Bayer added a comment - 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}"
          Hide
          abayer Andrew Bayer added a comment -

          Koen Bruyninckx - are you still having problems?

          Show
          abayer Andrew Bayer added a comment - Koen Bruyninckx - are you still having problems?
          Hide
          koen_bruyninckx Koen Bruyninckx added a comment -

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

          Show
          koen_bruyninckx Koen Bruyninckx added a comment - Hi Andrew, sorry I missed your previous post, I did it indeed similar to your proposal, and it works thx!
          abayer Andrew Bayer made changes -
          Status Reopened [ 4 ] Fixed but Unreleased [ 10203 ]
          Resolution Not A Defect [ 7 ]
          Hide
          delphym Daniel Mladek added a comment -

          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.

          Show
          delphym Daniel Mladek added a comment - 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.
          Hide
          delphym Daniel Mladek added a comment - - edited

          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 (or not to call it at all and use just cron() or whatever ?

          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

          Show
          delphym Daniel Mladek added a comment - - edited 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 (or not to call it at all and use just cron() or whatever ? 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
          Hide
          codizz Georg Koch added a comment -

          have the same issue as Daniel Mladek.

          It's possible to set conditional trigger on environmental variable?

          Show
          codizz Georg Koch added a comment - have the same issue as Daniel Mladek . It's possible to set conditional trigger on environmental variable?

            People

            • Assignee:
              abayer Andrew Bayer
              Reporter:
              progovoy Pavel Rogovoy
            • Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: