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

Run Groovy scripts from files and environment variables

    XMLWordPrintable

Details

    Description

      Currently there is no way to reference external Groovy scripts, they have to be typed in on the job configuration page. It may lead to the same script being duplicated among many jobs, making it difficult to maintain.

      Attachments

        Activity

          bjagri Balazs Jagri created issue -
          bjagri Balazs Jagri added a comment -

          I've sent a request to pull my solution, please review.

          bjagri Balazs Jagri added a comment - I've sent a request to pull my solution, please review.

          Please could you tell me what do you mean about referencing an external Groovy script?
          And currently, you can give an absolute path to your Groovy script, so the file can be shared through many jobs.
          Moreover, there is the SharedObjects Jenkins plugins to share more advanced objects and injected them in the EnvInject plugin.

          gbois Gregory Boissinot added a comment - Please could you tell me what do you mean about referencing an external Groovy script? And currently, you can give an absolute path to your Groovy script, so the file can be shared through many jobs. Moreover, there is the SharedObjects Jenkins plugins to share more advanced objects and injected them in the EnvInject plugin.
          gbois Gregory Boissinot made changes -
          Field Original Value New Value
          Status Open [ 1 ] In Progress [ 3 ]
          bjagri Balazs Jagri added a comment -

          Please could you tell me what do you mean about referencing an external Groovy script?

          I need a way to invoke Groovy scripts that are managed globally, e.g. with the SharedObjects plugin.

          And currently, you can give an absolute path to your Groovy script, so the file can be shared through many jobs.

          The "Script File Path" field is for shell scripts and its help states that they cannot contribute environment variables which is another requirement in my case.

          Moreover, there is the SharedObjects Jenkins plugins to share more advanced objects and injected them in the EnvInject plugin.

          My solution is is to run Groovy scripts defined in the SharedObjects plugin (by content or path), and have those scripts add environment variables to the job.

          bjagri Balazs Jagri added a comment - Please could you tell me what do you mean about referencing an external Groovy script? I need a way to invoke Groovy scripts that are managed globally, e.g. with the SharedObjects plugin. And currently, you can give an absolute path to your Groovy script, so the file can be shared through many jobs. The "Script File Path" field is for shell scripts and its help states that they cannot contribute environment variables which is another requirement in my case. Moreover, there is the SharedObjects Jenkins plugins to share more advanced objects and injected them in the EnvInject plugin. My solution is is to run Groovy scripts defined in the SharedObjects plugin (by content or path), and have those scripts add environment variables to the job.

          It sound like this change is the same as what I am currently trying to do:

          I would like to have a shared groovy script which could calculate environment variables.
          I would then like to be able to call this script from a job to set the environment variables for that job, instead of pasting the same groovy script into each job.

          gcummings Geoff Cummings added a comment - It sound like this change is the same as what I am currently trying to do: I would like to have a shared groovy script which could calculate environment variables. I would then like to be able to call this script from a job to set the environment variables for that job, instead of pasting the same groovy script into each job.

          Switch the issue to shared-objects component

          gbois Gregory Boissinot added a comment - Switch the issue to shared-objects component
          gbois Gregory Boissinot made changes -
          Component/s shared-objects [ 16525 ]
          Component/s envinject [ 15893 ]

          Code changed in jenkins
          User: Gregory Boissinot
          Path:
          src/main/java/org/jenkinsci/plugins/sharedobjects/type/GroovySharedObjectType.java
          src/main/resources/org/jenkinsci/plugins/sharedobjects/type/GroovySharedObjectType/config.jelly
          src/main/resources/org/jenkinsci/plugins/sharedobjects/type/GroovySharedObjectType/help-content.html
          http://jenkins-ci.org/commit/shared-objects-plugin/d295b675c16fabc939f3453e7cc79c1498c8f681
          Log:
          Fix JENKINS-14056

          Compare: https://github.com/jenkinsci/shared-objects-plugin/compare/f3b7183fd19c...d295b675c16f

          scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: Gregory Boissinot Path: src/main/java/org/jenkinsci/plugins/sharedobjects/type/GroovySharedObjectType.java src/main/resources/org/jenkinsci/plugins/sharedobjects/type/GroovySharedObjectType/config.jelly src/main/resources/org/jenkinsci/plugins/sharedobjects/type/GroovySharedObjectType/help-content.html http://jenkins-ci.org/commit/shared-objects-plugin/d295b675c16fabc939f3453e7cc79c1498c8f681 Log: Fix JENKINS-14056 Compare: https://github.com/jenkinsci/shared-objects-plugin/compare/f3b7183fd19c...d295b675c16f

          Does SharedObjects 0.35 suits you?

          gbois Gregory Boissinot added a comment - Does SharedObjects 0.35 suits you?
          gbois Gregory Boissinot made changes -
          Resolution Fixed [ 1 ]
          Status In Progress [ 3 ] Resolved [ 5 ]
          bjagri Balazs Jagri added a comment -

          We need to access build parameters from the Groovy script, apart from that it looks good.

          bjagri Balazs Jagri added a comment - We need to access build parameters from the Groovy script, apart from that it looks good.
          gcummings Geoff Cummings added a comment - - edited

          Hi, thanks for getting the changes in so quickly.

          I also need to access build parameters and environment variables.
          I am still learning about Jenkins and Groovy, but I have found I can access them with the following:

          def thr = Thread.currentThread()
          def build = thr.executable
          // get build parameters
          def buildVariablesMap = build.buildVariables
          // get all environment variables for the build
          def buildEnvVarsMap = build.envVars

          String jobName = buildEnvVarsMap?.JOB_NAME

          Please advise if there is a better way to access this information

          many thanks
          Geoff

          gcummings Geoff Cummings added a comment - - edited Hi, thanks for getting the changes in so quickly. I also need to access build parameters and environment variables. I am still learning about Jenkins and Groovy, but I have found I can access them with the following: def thr = Thread.currentThread() def build = thr.executable // get build parameters def buildVariablesMap = build.buildVariables // get all environment variables for the build def buildEnvVarsMap = build.envVars String jobName = buildEnvVarsMap?.JOB_NAME Please advise if there is a better way to access this information many thanks Geoff

          It is perfect.

          gbois Gregory Boissinot added a comment - It is perfect.
          rtyler R. Tyler Croy made changes -
          Workflow JNJira [ 144524 ] JNJira + In-Review [ 191131 ]

          People

            gbois Gregory Boissinot
            bjagri Balazs Jagri
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: