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

Make Jenkins environment variables available to dynamic providers

      For the dynamic providers (Shell, Groovy) it should be possible to get information about the current job.

      Example:
      When triggering a parameterized build job, the System Groovy script, which is executed to collect the choices, should be able to get the name of the current job. With the prefix of the current job we want to find all jobs that have the same prefix and make them available as choices.
      The job "prefix-admin-create-branch-job" executes a System Groovy script to collect all jobs with the same "prefix". The user selects one of the jobs to copy a job for a new subversion branch.

      I could not find a way how to get the name during the extensible choice provider execution. Later during the build (Ant, Maven, ...), the environment variable "JOB_NAME" is accessible, but it has not been set when the extensible choice parameter plugin is executed.

      Jenkins has a longer list of environment variables:
      https://wiki.jenkins-ci.org/display/JENKINS/Building+a+software+project#Buildingasoftwareproject-JenkinsSetEnvironmentVariables

      Environment variables could not be used for the System Groovy scripts as the script is not forked into a separate process and thus there is no chance to set the variables. For a shell provider this is possible, as the shell script is forked out into a new process anyway.

          [JENKINS-17875] Make Jenkins environment variables available to dynamic providers

          ikedam added a comment -

          It may be difficult to pass information about the current job to a groovy script.

          ParameterDefinition, which extensible-choice-parameter is based on,  has no interface to retreive information about the job it runs on.
          http://javadoc.jenkins-ci.org/hudson/model/ParameterDefinition.html

          Would you tell me why you want to get the job name, not just write it in the script?
          How about a following feature to meet your requirement?

          • Groovy scripts in a system scope, that is, multiple jobs can share a same script.
            • Just like Global Choice Parameter.
          • Each job can pass its own parameter to the groovy script.
            • In your case, pass the prefix of the name of jobs. 
            • You must write the parameter value in the job configuration. It may be annoying when you create many jobs...

          ikedam added a comment - It may be difficult to pass information about the current job to a groovy script. ParameterDefinition, which extensible-choice-parameter is based on,  has no interface to retreive information about the job it runs on. http://javadoc.jenkins-ci.org/hudson/model/ParameterDefinition.html Would you tell me why you want to get the job name, not just write it in the script? How about a following feature to meet your requirement? Groovy scripts in a system scope, that is, multiple jobs can share a same script. Just like Global Choice Parameter. Each job can pass its own parameter to the groovy script. In your case, pass the prefix of the name of jobs.  You must write the parameter value in the job configuration. It may be annoying when you create many jobs...

          Michael Rumpf added a comment - - edited

          We have a large list of projects (with role strategy plugin which assigns a LDAP role to projects with a certain prefix):

          • LDAP: ROLE_PREFIX1
            • prefix1-projA-trunk
            • prefix1-projA-branchI
            • prefix1-projA-branchII
            • prefix1-projA-branchIII
            • prefix1-projB-trunk
            • prefix1-projB-branchI
            • prefix1-projB-branchII
            • prefix1-projB-branchIII
          • LDAP: ROLE_PREFIX2
            • prefix2-projA-trunk
            • prefix2-projA-branchI
            • prefix2-projA-branchII
            • prefix2-projA-branchIII
            • prefix2-projB-trunk
            • prefix2-projB-branchI
            • prefix2-projB-branchII
            • prefix2-projB-branchIII

          For those project groups I now want to create an admin job for each prefix:

          • prefix1-admin-create-branch-job
          • prefix2-admin-create-branch-job

          Those admin jobs share the same System Groovy script CreateBranchJobFromTrunk.groovy, which clones a job and modifies the Subversion URL and the Sonar branch name.
          The admin job is configured to have an Extensible Choice Parameter system Groovy script provider. The system Groovy script collects all jobs with the same prefix as the admin job itself and with a "trunk" suffix. For example: The "prefix1-admin-create-branch-job" collects the two projects "prefix1-projA-trunk" and "prefix1-projB-trunk".

          Having the job name available in the System Groovy List Provider we could just clone the "prefix1-admin-create-branch-job" for new prefixes "prefix3" ... "prefixN" without having to worry that the prefix does not match.

          Michael Rumpf added a comment - - edited We have a large list of projects (with role strategy plugin which assigns a LDAP role to projects with a certain prefix): LDAP: ROLE_PREFIX1 prefix1-projA-trunk prefix1-projA-branchI prefix1-projA-branchII prefix1-projA-branchIII prefix1-projB-trunk prefix1-projB-branchI prefix1-projB-branchII prefix1-projB-branchIII LDAP: ROLE_PREFIX2 prefix2-projA-trunk prefix2-projA-branchI prefix2-projA-branchII prefix2-projA-branchIII prefix2-projB-trunk prefix2-projB-branchI prefix2-projB-branchII prefix2-projB-branchIII For those project groups I now want to create an admin job for each prefix: prefix1-admin-create-branch-job prefix2-admin-create-branch-job Those admin jobs share the same System Groovy script CreateBranchJobFromTrunk.groovy, which clones a job and modifies the Subversion URL and the Sonar branch name. The admin job is configured to have an Extensible Choice Parameter system Groovy script provider. The system Groovy script collects all jobs with the same prefix as the admin job itself and with a "trunk" suffix. For example: The "prefix1-admin-create-branch-job" collects the two projects "prefix1-projA-trunk" and "prefix1-projB-trunk". Having the job name available in the System Groovy List Provider we could just clone the "prefix1-admin-create-branch-job" for new prefixes "prefix3" ... "prefixN" without having to worry that the prefix does not match.

          ikedam added a comment -

          Thanks for details.
          So the points are:

          • Job names are used as an access control.
          • Users shouldn't copy jobs that they are not allowed to access to.

          I agree that the best way is to use a job name in a groovy script for this case.

          But as mentioned before, there seems no proper way to access the job information from ParameterDefinition...

          ikedam added a comment - Thanks for details. So the points are: Job names are used as an access control. Users shouldn't copy jobs that they are not allowed to access to. I agree that the best way is to use a job name in a groovy script for this case. But as mentioned before, there seems no proper way to access the job information from ParameterDefinition...

          Michael Rumpf added a comment -

          The only chance I see is that an extension to the ParameterDefinition type is introduced where the job context is passed upon instantiation. The ParameterDefinition is instantiated in the context of a job, so that should not be an issue. But overall that would require some change in the Jenkins internal APIs.
          I think I will stick to manually setting the jobname in the System Groovy List Provider for now.

          Michael Rumpf added a comment - The only chance I see is that an extension to the ParameterDefinition type is introduced where the job context is passed upon instantiation. The ParameterDefinition is instantiated in the context of a job, so that should not be an issue. But overall that would require some change in the Jenkins internal APIs. I think I will stick to manually setting the jobname in the System Groovy List Provider for now.

          ikedam added a comment -

          I found the project can be retrieved by Stapler.getCurrentRequest().findAnscestorObject(AbstractProject.class).
          I try introduce this feature.

          ikedam added a comment - I found the project can be retrieved by Stapler.getCurrentRequest().findAnscestorObject(AbstractProject.class) . I try introduce this feature.

          Code changed in jenkins
          User: ikedam
          Path:
          src/main/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider.java
          src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-scriptText.html
          src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-scriptText_ja.html
          src/test/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/ExtensibleChoiceParameterJenkinsRule.java
          src/test/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProviderJenkinsTest.java
          http://jenkins-ci.org/commit/extensible-choice-parameter-plugin/3862977e8ea7e1557e2ca1b42e313fff5d057ebb
          Log:
          [FIXED JENKINS-17875] added variables jenkins, project in SystemGroovyChoiceListProvider.

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: ikedam Path: src/main/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider.java src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-scriptText.html src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-scriptText_ja.html src/test/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/ExtensibleChoiceParameterJenkinsRule.java src/test/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProviderJenkinsTest.java http://jenkins-ci.org/commit/extensible-choice-parameter-plugin/3862977e8ea7e1557e2ca1b42e313fff5d057ebb Log: [FIXED JENKINS-17875] added variables jenkins, project in SystemGroovyChoiceListProvider.

          Code changed in jenkins
          User: ikedam
          Path:
          src/main/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/ExtensibleChoiceParameterDefinition.java
          src/main/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider.java
          src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-scriptText.html
          src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-scriptText_ja.html
          src/test/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProviderJenkinsTest.java
          http://jenkins-ci.org/commit/extensible-choice-parameter-plugin/3fed072bbdf1b1fdec9a4c8f53af6645dac067d6
          Log:
          JENKINS-17875 Improved the way to retrieve (and keep) project.

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: ikedam Path: src/main/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/ExtensibleChoiceParameterDefinition.java src/main/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider.java src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-scriptText.html src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-scriptText_ja.html src/test/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProviderJenkinsTest.java http://jenkins-ci.org/commit/extensible-choice-parameter-plugin/3fed072bbdf1b1fdec9a4c8f53af6645dac067d6 Log: JENKINS-17875 Improved the way to retrieve (and keep) project.

          Code changed in jenkins
          User: ikedam
          Path:
          src/main/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider.java
          src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/config.jelly
          src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/config_ja.properties
          src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-scriptText.html
          src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-scriptText_ja.html
          src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-usePredefinedVariables.html
          src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-usePredefinedVariables_ja.html
          src/test/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProviderJenkinsTest.java
          http://jenkins-ci.org/commit/extensible-choice-parameter-plugin/9bf438c64e7ee4a4405645ac36b43a4f753dcbb4
          Log:
          JENKINS-17875 Added option whether to use predefined variables. This can allow users to have a workaround when a problem happened retrieving project.

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: ikedam Path: src/main/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider.java src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/config.jelly src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/config_ja.properties src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-scriptText.html src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-scriptText_ja.html src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-usePredefinedVariables.html src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-usePredefinedVariables_ja.html src/test/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProviderJenkinsTest.java http://jenkins-ci.org/commit/extensible-choice-parameter-plugin/9bf438c64e7ee4a4405645ac36b43a4f753dcbb4 Log: JENKINS-17875 Added option whether to use predefined variables. This can allow users to have a workaround when a problem happened retrieving project.

          Code changed in jenkins
          User: ikedam
          Path:
          src/main/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/ExtensibleChoiceParameterDefinition.java
          src/main/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider.java
          src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/config.jelly
          src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/config_ja.properties
          src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-usePredefinedVariables.html
          src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-usePredefinedVariables_ja.html
          src/test/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProviderJenkinsTest.java
          http://jenkins-ci.org/commit/extensible-choice-parameter-plugin/df8a84e7c430994be673651cc7085bbeaf66ed8c
          Log:
          Merge pull request #5 from ikedam/feature/JENKINS-17875_VariablesInGroovy

          JENKINS-17875 Referencing project from System Groovy Script.

          Compare: https://github.com/jenkinsci/extensible-choice-parameter-plugin/compare/fbbbd48d18f4...df8a84e7c430

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: ikedam Path: src/main/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/ExtensibleChoiceParameterDefinition.java src/main/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider.java src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/config.jelly src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/config_ja.properties src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-usePredefinedVariables.html src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-usePredefinedVariables_ja.html src/test/java/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProviderJenkinsTest.java http://jenkins-ci.org/commit/extensible-choice-parameter-plugin/df8a84e7c430994be673651cc7085bbeaf66ed8c Log: Merge pull request #5 from ikedam/feature/ JENKINS-17875 _VariablesInGroovy JENKINS-17875 Referencing project from System Groovy Script. Compare: https://github.com/jenkinsci/extensible-choice-parameter-plugin/compare/fbbbd48d18f4...df8a84e7c430

          ikedam added a comment -

          Added in 1.2.2.

          ikedam added a comment - Added in 1.2.2.

          jerryshea added a comment -

          Hi, I appreciate that this issue is closed but can you advise how do I actually use an environment variable in a groovy script? Neither of the below work for me:

          "${BUILD_NUMBER}"
          System.env["BUILD_NUMBER"]
          

          Thanks!

          jerryshea added a comment - Hi, I appreciate that this issue is closed but can you advise how do I actually use an environment variable in a groovy script? Neither of the below work for me: "${BUILD_NUMBER}" System .env[ "BUILD_NUMBER" ] Thanks!

          ikedam added a comment -

          Unfortunately, you never get BUILD_NUMBER as the script runs BEFORE builds start.

          ikedam added a comment - Unfortunately, you never get BUILD_NUMBER as the script runs BEFORE builds start.

          jerryshea added a comment -

          Ah... thanks. Should the above syntax work to get other environment variables such as jenkins global environment variables?

          jerryshea added a comment - Ah... thanks. Should the above syntax work to get other environment variables such as jenkins global environment variables?

          ikedam added a comment -

          ikedam added a comment - No. You can use project and jenkins in scripts instead. See the help for usePredefinedVariables: https://github.com/jenkinsci/extensible-choice-parameter-plugin/blob/master/src/main/resources/jp/ikedam/jenkins/plugins/extensible_choice_parameter/SystemGroovyChoiceListProvider/help-usePredefinedVariables.html

          jerryshea added a comment -

          ok thanks!

          jerryshea added a comment - ok thanks!

            ikedam ikedam
            mirumpf Michael Rumpf
            Votes:
            2 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: