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

The 'jenkinsProject' variable is not set in the binding after restarting Jenkins

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Critical Critical
    • active-choices-plugin
    • None
    • Jenkins ver. 1.620 and 1.615;
      Active Choices Plugin version 1.1;
      Java 1.7.0_79-b14;
      OS.version 3.2.0-4-amd64 Debian Wheezy

      I've experienced a strange behavior, when working with the Active Choices Reactive Reference Parameter.
      The use case was to set the parameter value to the actual Jenkins job name, as a fallback for missing referenced parameter value.
      The Groovy code:

      import hudson.model.*
      import jenkins.model.*
      
      def job = binding.jenkinsProject
      def artifactName = binding.variables.get("1_ARTIFACT")
      if (!artifactName) {
      	artifactName = job.name
      }
      return "<input name=\"value\" value=\"${artifactName}\" class=\"setting-input\" type=\"text\">"
      

      .. where the '1_ARTIFACT' is the referenced variable.
      The above works as expected, when one creates a project and saves it in Jenkins UI.
      It stops working though, after Jenkins restart.
      After some investigation I've realized that the 'jenkinsProject' variable is missing in the binding after fresh Jenkins start (or restart).
      When one goes then into the job configuration UI and presses the 'Save' button (no changes required), the above code starts working again, because the 'jenkinsProject' variable is injected into the binding.
      It doesn't matter if the Groovy script is embedded in the parameter config or stored and referenced as Scriptler script, the bahavior is the same.
      Apparently the 'jenkinsProject' variable is not being initialized for the org.biouno.unochoice.DynamicReferenceParameter during the Jenkins start.

          [JENKINS-29476] The 'jenkinsProject' variable is not set in the binding after restarting Jenkins

          Ioannis Moutsatsos added a comment - - edited

          I have not been able to use any build environment parameters directly in the groovy script. They seem to be null. I agree that this is a high priority issue. It is very useful to have all of the build environment variables in the script binding context (for both groovy scripts and scriptlets)

          Ioannis Moutsatsos added a comment - - edited I have not been able to use any build environment parameters directly in the groovy script. They seem to be null. I agree that this is a high priority issue. It is very useful to have all of the build environment variables in the script binding context (for both groovy scripts and scriptlets)

          Ack, including this one in the list of issues for the next dev cycle.

          Bruno P. Kinoshita added a comment - Ack, including this one in the list of issues for the next dev cycle.

          This will be the last issue fixed before the 1.3 release (probably before x-mas)

          Bruno P. Kinoshita added a comment - This will be the last issue fixed before the 1.3 release (probably before x-mas)

          By the fault, the jenkinsProject variable will always be present in the binding context, and can be used in any kind of parameter.

          The value is not being persisted. The strategy to get the project is as follows:

          1. When the project is saved, we get the current HTTP Request (StaplerRequest) which contains the parameter parameters, and some helper methods
          2. From the current request, find an ancestor to the parameter definition which class derives from Project
          3. Store *the name* of the project
          4. When a parameter is evaluated
          4.1 If the project name is set, get the Project object from Jenkins by giving the project name (more efficient)
          4.2 Otherwise, get the parameter unique UUID and search all Jenkins projects for one that contains this parameter. Then get the project object (slower)
          5. Get the parameters map just before evaluating the script, inject the helper parameters (at the moment it consists of only jenkinsProject=...)

          Bruno P. Kinoshita added a comment - By the fault, the jenkinsProject variable will always be present in the binding context, and can be used in any kind of parameter. The value is not being persisted. The strategy to get the project is as follows: 1. When the project is saved, we get the current HTTP Request (StaplerRequest) which contains the parameter parameters, and some helper methods 2. From the current request, find an ancestor to the parameter definition which class derives from Project 3. Store * the name * of the project 4. When a parameter is evaluated 4.1 If the project name is set, get the Project object from Jenkins by giving the project name (more efficient) 4.2 Otherwise, get the parameter unique UUID and search all Jenkins projects for one that contains this parameter. Then get the project object (slower) 5. Get the parameters map just before evaluating the script, inject the helper parameters (at the moment it consists of only jenkinsProject=...)

          Screen shots, generated using two parameters. A simple cascade parameter with an array of numbers, and another cascade parameter with the following script:

          import hudson.model.*
          import jenkins.model.*
          
          def job = binding.jenkinsProject
          return [job.name, Param001]
          

          Bruno P. Kinoshita added a comment - Screen shots, generated using two parameters. A simple cascade parameter with an array of numbers, and another cascade parameter with the following script: import hudson.model.* import jenkins.model.* def job = binding.jenkinsProject return [job.name, Param001]

          Comments added to the Pull Request, adding credit to PR submitter in the plug-in Wiki, alongside this issue number

          Bruno P. Kinoshita added a comment - Comments added to the Pull Request, adding credit to PR submitter in the plug-in Wiki, alongside this issue number

          Code changed in jenkins
          User: Bruno P. Kinoshita
          Path:
          src/main/java/org/biouno/unochoice/AbstractScriptableParameter.java
          src/main/java/org/biouno/unochoice/util/Utils.java
          http://jenkins-ci.org/commit/active-choices-plugin/920bd258ce5801e3ddabb776bb2cfb8114658a08
          Log:
          [FIXED JENKINS-29476] Add jenkinsProject variable to binding context

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Bruno P. Kinoshita Path: src/main/java/org/biouno/unochoice/AbstractScriptableParameter.java src/main/java/org/biouno/unochoice/util/Utils.java http://jenkins-ci.org/commit/active-choices-plugin/920bd258ce5801e3ddabb776bb2cfb8114658a08 Log: [FIXED JENKINS-29476] Add jenkinsProject variable to binding context

          Fixed in 1.3

          Bruno P. Kinoshita added a comment - Fixed in 1.3

            kinow Bruno P. Kinoshita
            valdemon Waldek Kozba
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: