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

Set default value for environment variable when not already set

    XMLWordPrintable

    Details

    • Epic Link:
    • Similar Issues:

      Description

      We'd like to set an environment variable for a pipeline job to inherit from a global environment variable when present, but to default to a reasonable value when not. This was possible in scripted pipeline using the following snippet:

       

      def foo = env.FOO ?: 'foo'
      withEnv(["BAR=${foo}"]) {
        // build
      }

      My attempts to recreate this in declarative pipelines have proven unsuccessful. If this is already possible, please provide some documentation. If it's not possible, please consider this a feature request.

       

        Attachments

          Issue Links

            Activity

            Hide
            davehunt Dave Hunt added a comment -

            A use case for this is specifying the number of parallel processes for pytest. This is done via a command line option set in an environment variable named PYTEST_ADDOPTS. A reasonable default is 'auto', which determines the number of processes based on the CPUs available on the system, however it would be beneficial to override this on a per-instance basis. The idea is that we'd set a PYTEST_PROCESSES environment variable, which takes precedence over the default of 'auto' defined in the pipeline.

            Show
            davehunt Dave Hunt added a comment - A use case for this is specifying the number of parallel processes for pytest. This is done via a command line option set in an environment variable named PYTEST_ADDOPTS. A reasonable default is 'auto', which determines the number of processes based on the CPUs available on the system, however it would be beneficial to override this on a per-instance basis. The idea is that we'd set a PYTEST_PROCESSES environment variable, which takes precedence over the default of 'auto' defined in the pipeline.
            Hide
            abayer Andrew Bayer added a comment -

            Sounds like this may be covered by JENKINS-45916 and JENKINS-42753 - I'll add a test to https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/174 to verify that something like FOO = ${FOO ?: "some value"}" works.

            Show
            abayer Andrew Bayer added a comment - Sounds like this may be covered by JENKINS-45916 and JENKINS-42753 - I'll add a test to https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/174 to verify that something like FOO = ${FOO ?: "some value"}" works.
            Hide
            abayer Andrew Bayer added a comment -

            Ok, doesn't work, but I'll fix it in that PR. =)

            Show
            abayer Andrew Bayer added a comment - Ok, doesn't work, but I'll fix it in that PR. =)
            Hide
            abayer Andrew Bayer added a comment -

            Ah, I overshot in the JENKINS-45916 fix. Tweaked it, tests pass now. Test added to the JENKINS-42753 PR, and this'll be included in the upcoming Declarative 1.2 release.

            Show
            abayer Andrew Bayer added a comment - Ah, I overshot in the JENKINS-45916 fix. Tweaked it, tests pass now. Test added to the JENKINS-42753 PR, and this'll be included in the upcoming Declarative 1.2 release.

              People

              • Assignee:
                abayer Andrew Bayer
                Reporter:
                davehunt Dave Hunt
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: