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

Jenkins fails on periodic build with Dynamic Choice Parameter Groovy script

XMLWordPrintable

      I'm trying to use the Dynamic Choice Parameter plugin to set up a parameterized build in Jenkins. The parameters should be the names of some directory's in a directory of the working directory of the job (i.e. `/var/lib/jenkins/workspace/JobName/dirToScan/`).

      Making the items in the drop down menu can be done in this way:

      `def list = ['ls', "/path/to/dirToScan/"].execute().text.tokenize()`

      To reach the right directory, I need the name of the job since, as opposed to when a job is executed, the directory in which the this is done is not the `/var/lib/jenkins/workspace/[Job Name]/` directory but the root dir `/`.
      The Dynamic Choice Parameter plugin allows the execution of a Groovy script to fill the choice parameter. Using the answer provided here I was able to retrieve the job name. So my final code to build the drop-down menu in the parameterized build menu in Jenkins looks like this:

      def build = Thread.currentThread().toString()
      def regexp= ".?/job/([^/])/.*"
      def match = build =~ regexp
      def jobName = match[0][1]

      def list = ['ls', "/var/lib/jenkins/workspace/" << jobName << "/dirToScan"].execute().text.tokenize()

      Clicking the Build with Parameters link in the Jenkins job opens the page with the drop-down menu (which I named Restore) and also the actual building works great!

      But now comes my problem:
      If set the job to be build periodically, the build fails with the following console output:

      Started by timer
      Building in workspace /var/lib/jenkins/workspace/JobName
      FATAL: Null value not allowed as an environment variable: Restore
      java.lang.IllegalArgumentException: Null value not allowed as an environment variable: Restore
      at hudson.EnvVars.put(EnvVars.java:356)
      at hudson.model.StringParameterValue.buildEnvironment(StringParameterValue.java:56)
      at hudson.model.ParametersAction.buildEnvVars(ParametersAction.java:88)
      at hudson.model.AbstractBuild.getEnvironment(AbstractBuild.java:929)
      at hudson.plugins.mercurial.MercurialSCM.checkout(MercurialSCM.java:533)
      at hudson.scm.SCM.checkout(SCM.java:484)
      at hudson.model.AbstractProject.checkout(AbstractProject.java:1270)
      at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:609)
      at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:86)
      at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:531)
      at hudson.model.Run.execute(Run.java:1750)
      at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
      at hudson.model.ResourceController.execute(ResourceController.java:89)
      at hudson.model.Executor.run(Executor.java:240)
      Finished: FAILURE

      This does not happen if I just populate the drop-down menu in a 'normal' way, e.g.: def list = ["Option 1", "Option 2", "Option 3"]

      This might be a bug in the plugin, or Jenkins being unable to execute the Groovy code if it starts a job by itself. The problem only occurs with a periodic build, not if I start the build myself and it it independent of the chosen option in the drop-down menu.

            Unassigned Unassigned
            koningh Chiel de Koningh
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: