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

How can I set an environment variable based on value of user passed parameter?

    Details

    • Type: Improvement
    • Status: Resolved (View Workflow)
    • Priority: Major
    • Resolution: Fixed
    • Component/s: envinject-plugin
    • Labels:
      None
    • Environment:
      Redhat Linux 5.5
    • Similar Issues:

      Description

      I want to set value for an environment variable based on user provided input during the build and use this env variable to set workspace in a jenkins project.

      Ex: User has to select MODULE_NAME when performing a build. MODULE_NAME is a choice parameter. Based on the value selected by the user I want to set MODULE_FOLDER environment variable and use it for setting the WORKSPACE.

      Below is the functionality I am expecting.

      if MODULE_NAME is 'A' then set MODULE_FOLDER = FOLDER1
      else if MODULE_NAME is 'B' then set MODULE_FOLDER = FOLDER2
      else if MODULE_NAME is 'C' then set MODULE_FOLDER = FOLDER3

      Now set WORKSPACE=/usr/modules/$MODULE_FOLDER

      Please help me achieve this functionality. I want to have only one job for all modules. Based on the module user selects I want to build it and deploy it.

      your guidance is much appreciated

        Attachments

          Activity

          Hide
          scm_issue_link SCM/JIRA link daemon added a comment -

          Code changed in jenkins
          User: Gregory Boissinot
          Path:
          src/main/java/hudson/plugins/envfile/EnvFileBuildWrapper.java
          src/main/java/hudson/plugins/setenv/SetEnvBuildWrapper.java
          src/main/java/org/jenkinsci/plugins/envinject/EnvInjectJobPropertyInfo.java
          src/main/java/org/jenkinsci/plugins/envinject/EnvInjectListener.java
          src/main/java/org/jenkinsci/plugins/envinject/service/EnvInjectEnvVars.java
          src/main/java/org/jenkinsci/plugins/envinject/service/EnvInjectScriptExecutor.java
          src/main/resources/org/jenkinsci/plugins/envinject/EnvInjectJobProperty/config.jelly
          src/main/resources/org/jenkinsci/plugins/envinject/EnvInjectJobProperty/help-groovyScriptContent.html
          src/main/resources/org/jenkinsci/plugins/envinject/EnvInjectJobProperty/help-keepBuildVariables.html
          src/main/webapp/help.html
          src/test/java/org/jenkinsci/plugins/envinject/EnvInjectBuildWrapperTest.java
          http://jenkins-ci.org/commit/envinject-plugin/257acc17f1ae1560f95270d1c24aebc6b60d0261
          Log:
          Fix JENKINS-13119

          Show
          scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: Gregory Boissinot Path: src/main/java/hudson/plugins/envfile/EnvFileBuildWrapper.java src/main/java/hudson/plugins/setenv/SetEnvBuildWrapper.java src/main/java/org/jenkinsci/plugins/envinject/EnvInjectJobPropertyInfo.java src/main/java/org/jenkinsci/plugins/envinject/EnvInjectListener.java src/main/java/org/jenkinsci/plugins/envinject/service/EnvInjectEnvVars.java src/main/java/org/jenkinsci/plugins/envinject/service/EnvInjectScriptExecutor.java src/main/resources/org/jenkinsci/plugins/envinject/EnvInjectJobProperty/config.jelly src/main/resources/org/jenkinsci/plugins/envinject/EnvInjectJobProperty/help-groovyScriptContent.html src/main/resources/org/jenkinsci/plugins/envinject/EnvInjectJobProperty/help-keepBuildVariables.html src/main/webapp/help.html src/test/java/org/jenkinsci/plugins/envinject/EnvInjectBuildWrapperTest.java http://jenkins-ci.org/commit/envinject-plugin/257acc17f1ae1560f95270d1c24aebc6b60d0261 Log: Fix JENKINS-13119
          Hide
          gbois Gregory Boissinot added a comment - - edited

          I added the ability to evaluate a Groovy script in 'Prepare environment for the build' section

          For example, you can have the following script:
          if (CASE==null)

          { return null; }

          def stringValue="StRinG";

          if ("upper".equals(CASE))

          { def map = [COMPUTE_VAR: stringValue.toUpperCase()] return map }

          if ("lower".equals(CASE))

          { def map = [COMPUTE_VAR: stringValue.toLowerCase()] return map }

          This script injects the environment COMPUTE_VAR in the following job steps.
          As you can see, this variable is computed according the CASE variable value.
          This variable comes from a parameter value (given by the user).

          The only restrictions are:

          • The script has to be in Groovy (close to Jenkins core, written in Java/Groovy)
          • The script has to return a Map Java object (all the map elements will exported as environment variables).

          Please upgrade to EnvInject 1.38.
          Let me know if it suits you.

          Show
          gbois Gregory Boissinot added a comment - - edited I added the ability to evaluate a Groovy script in 'Prepare environment for the build' section For example, you can have the following script: if (CASE==null) { return null; } def stringValue="StRinG"; if ("upper".equals(CASE)) { def map = [COMPUTE_VAR: stringValue.toUpperCase()] return map } if ("lower".equals(CASE)) { def map = [COMPUTE_VAR: stringValue.toLowerCase()] return map } This script injects the environment COMPUTE_VAR in the following job steps. As you can see, this variable is computed according the CASE variable value. This variable comes from a parameter value (given by the user). The only restrictions are: The script has to be in Groovy (close to Jenkins core, written in Java/Groovy) The script has to return a Map Java object (all the map elements will exported as environment variables). Please upgrade to EnvInject 1.38. Let me know if it suits you.
          Hide
          gbois Gregory Boissinot added a comment -

          And for your need, you have to select a custom workspace and use your computed variable.

          Show
          gbois Gregory Boissinot added a comment - And for your need, you have to select a custom workspace and use your computed variable.
          Hide
          skumar suresh kukalakuntla added a comment -

          Thank You so much Greg for your quick response, with a plugin update!!!

          I installed EnvInject 1.38, added similar groovy script to my job and tried to build it. I got the below error.

          [EnvInject] - Preparing an environment for the build.
          [EnvInject] - [ERROR] - SEVERE ERROR occurs: com/sun/xml/internal/ws/server/UnsupportedMediaException

          I think I have to add required jar to class path of JBOSS AS 7 I am using for jenkins, which I do not know how to. I am trying to find out how to do that. Could you please let me know how to do that, if you already know about it?

          I will provide an update once I am able to successfully use the updated plugin to set environment variables based on user input.

          Thank You once again for your effort.

          Show
          skumar suresh kukalakuntla added a comment - Thank You so much Greg for your quick response, with a plugin update!!! I installed EnvInject 1.38, added similar groovy script to my job and tried to build it. I got the below error. [EnvInject] - Preparing an environment for the build. [EnvInject] - [ERROR] - SEVERE ERROR occurs: com/sun/xml/internal/ws/server/UnsupportedMediaException I think I have to add required jar to class path of JBOSS AS 7 I am using for jenkins, which I do not know how to. I am trying to find out how to do that. Could you please let me know how to do that, if you already know about it? I will provide an update once I am able to successfully use the updated plugin to set environment variables based on user input. Thank You once again for your effort.
          Hide
          gbois Gregory Boissinot added a comment -

          Maybe a JBOSS issue with Groovy.
          Do you have the ability to test on another servlet container such as Tomcat?

          Show
          gbois Gregory Boissinot added a comment - Maybe a JBOSS issue with Groovy. Do you have the ability to test on another servlet container such as Tomcat?
          Hide
          skumar suresh kukalakuntla added a comment -

          I am able to to put the required jar file in the lib folder of jenkins.war and I am able to use ENVInject successfully.

          Right now I am trying to test Evaluated Groovy script. Tried the below and got and error

          [EnvInject] - Evaluation the following Groovy script content:
          if (MODULE_NAME==null)

          { return null; }

          if ("ACCT".equals(MODULE_NAME))

          { def map = ["MODULE_HOME": "SSP-AM"] return map }

          if ("FPD".equals(MODULE_NAME))

          { def map = ["MODULE_HOME": "SSP-FPD"] return map }

          [EnvInject] - [ERROR] - SEVERE ERROR occurs: startup failed:
          Script1.groovy: 3: expecting '}', found 'return' @ line 3, column 70.
          p = ["MODULE_HOME": "SSP-AM"] return map
          ^

          1 error

          Show
          skumar suresh kukalakuntla added a comment - I am able to to put the required jar file in the lib folder of jenkins.war and I am able to use ENVInject successfully. Right now I am trying to test Evaluated Groovy script. Tried the below and got and error [EnvInject] - Evaluation the following Groovy script content: if (MODULE_NAME==null) { return null; } if ("ACCT".equals(MODULE_NAME)) { def map = ["MODULE_HOME": "SSP-AM"] return map } if ("FPD".equals(MODULE_NAME)) { def map = ["MODULE_HOME": "SSP-FPD"] return map } [EnvInject] - [ERROR] - SEVERE ERROR occurs: startup failed: Script1.groovy: 3: expecting '}', found 'return' @ line 3, column 70. p = ["MODULE_HOME": "SSP-AM"] return map ^ 1 error
          Hide
          skumar suresh kukalakuntla added a comment -

          Greg,
          I am able to successfully run the groovy script that serves my purpose. I really appreciate your help and time. You are very responsive and quick

          Below is the log. MODULE_NAME is provided by user when performing build.

          [EnvInject] - Evaluation the following Groovy script content:
          if(MODULE_NAME.equals('ACCT'))

          { def map = ["MODULE_HOME": "SSP-AM"] return map; }

          if(MODULE_NAME.equals('FPD'))

          { def map = ["MODULE_HOME": "SSP-FPD"] return map; }

          Building in workspace /usr/J2EE/JENKINS/jobs/TEST_GROOVY/workspace
          [workspace] $ /bin/sh -xe /tmp/hudson50466.sh
          + echo MODULE_NAME:FPD
          MODULE_NAME:FPD
          + echo MODULE_HOME:SSP-FPD
          MODULE_HOME:SSP-FPD
          Finished: SUCCESS

          Show
          skumar suresh kukalakuntla added a comment - Greg, I am able to successfully run the groovy script that serves my purpose. I really appreciate your help and time. You are very responsive and quick Below is the log. MODULE_NAME is provided by user when performing build. [EnvInject] - Evaluation the following Groovy script content: if(MODULE_NAME.equals('ACCT')) { def map = ["MODULE_HOME": "SSP-AM"] return map; } if(MODULE_NAME.equals('FPD')) { def map = ["MODULE_HOME": "SSP-FPD"] return map; } Building in workspace /usr/J2EE/JENKINS/jobs/TEST_GROOVY/workspace [workspace] $ /bin/sh -xe /tmp/hudson50466.sh + echo MODULE_NAME:FPD MODULE_NAME:FPD + echo MODULE_HOME:SSP-FPD MODULE_HOME:SSP-FPD Finished: SUCCESS
          Hide
          gbois Gregory Boissinot added a comment -

          Try to use return line

          Copy paste the following script, it should work

          if (MODULE_NAME==null)

          { return null; }

          if ("ACCT".equals(MODULE_NAME))

          { def map = [MODULE_HOME: "SSP-AM"] return map }

          if ("FPD".equals(MODULE_NAME))

          { def map = [MODULE_HOME: "SSP-FPD"] return map }
          Show
          gbois Gregory Boissinot added a comment - Try to use return line Copy paste the following script, it should work if (MODULE_NAME==null) { return null; } if ("ACCT".equals(MODULE_NAME)) { def map = [MODULE_HOME: "SSP-AM"] return map } if ("FPD".equals(MODULE_NAME)) { def map = [MODULE_HOME: "SSP-FPD"] return map }
          Hide
          gbois Gregory Boissinot added a comment -

          Wiki removes return line.
          Otherwise, add a semicolon, for example:

          if (MODULE_NAME==null)

          { return null; }

          if ("ACCT".equals(MODULE_NAME))

          { def map = [MODULE_HOME: "SSP-AM"] ; return map }

          if ("FPD".equals(MODULE_NAME))

          { def map = [MODULE_HOME: "SSP-FPD"] ; return map }
          Show
          gbois Gregory Boissinot added a comment - Wiki removes return line. Otherwise, add a semicolon, for example: if (MODULE_NAME==null) { return null; } if ("ACCT".equals(MODULE_NAME)) { def map = [MODULE_HOME: "SSP-AM"] ; return map } if ("FPD".equals(MODULE_NAME)) { def map = [MODULE_HOME: "SSP-FPD"] ; return map }
          Hide
          anup_achina Anup Singh added a comment -

          I have this groovy script:

          if (BUILD_TYPE.equals('Trunk'))

          { def map = [SVN_REPO_PATH: "trunk/ConfigManagement"]; return map; }

          if (BUILD_TYPE.equals('Branches')) {
          def map = [SVN_REPO_PATH: "branches/${SVN_BRANCH}"]; return map;
          }
          if (BUILD_TYPE.equals('Tags')) {
          def map = [SVN_REPO_PATH: "branches/${SVN_TAG}"]; return map;
          }

          Where BUILD_TYPE is a CHOICE type user parameter which has Trunk, Branches, Tags in the drop down.

          The build keeps failing with,

          [EnvInject] - [ERROR] - SEVERE ERROR occurs: No such property: BUILD_TYPE for class: Script1
          Finished: FAILURE

          Please Help

          Show
          anup_achina Anup Singh added a comment - I have this groovy script: if (BUILD_TYPE.equals('Trunk')) { def map = [SVN_REPO_PATH: "trunk/ConfigManagement"]; return map; } if (BUILD_TYPE.equals('Branches')) { def map = [SVN_REPO_PATH: "branches/${SVN_BRANCH}"] ; return map; } if (BUILD_TYPE.equals('Tags')) { def map = [SVN_REPO_PATH: "branches/${SVN_TAG}"] ; return map; } Where BUILD_TYPE is a CHOICE type user parameter which has Trunk, Branches, Tags in the drop down. The build keeps failing with, [EnvInject] - [ERROR] - SEVERE ERROR occurs: No such property: BUILD_TYPE for class: Script1 Finished: FAILURE Please Help
          Hide
          anup_achina Anup Singh added a comment -

          Additional Info: SVN_BRANCH and SVN_TAG are other user input parameters.

          Show
          anup_achina Anup Singh added a comment - Additional Info: SVN_BRANCH and SVN_TAG are other user input parameters.
          Hide
          gbois Gregory Boissinot added a comment -

          I suggest you should replacing the condition BUILD_TYPE.equals('Trunk') by "Trunk".equals(BUILD_TYPE).
          Inverting the conditions avoids a NullPointerException.
          Could apply this principle to all conditions and let me know?

          Show
          gbois Gregory Boissinot added a comment - I suggest you should replacing the condition BUILD_TYPE.equals('Trunk') by "Trunk".equals(BUILD_TYPE). Inverting the conditions avoids a NullPointerException. Could apply this principle to all conditions and let me know?
          Hide
          anup_achina Anup Singh added a comment -

          Gregory, First let me just thank you for such a prompt response. Honestly speaking I am not a big fan of open source mainly because of support. But my view is already changing.

          Having said that, I did what you request me and the issue persists. complete details follow:

          Started by user Anup Singh
          [EnvInject] - Loading node environment variables.
          [EnvInject] - Preparing an environment for the build.
          [EnvInject] - Keeping Jenkins system variables.
          [EnvInject] - Adding build parameters as variables.
          [EnvInject] - Evaluation the following Groovy script content:
          if ("Trunk".equals(BUILD_TYPE))

          { def map = [SVN_REPO_PATH: "trunk/ConfigManagement"]; return map }

          if ("Branches".equals(BUILD_TYPE)) {
          def map = [SVN_REPO_PATH: "branches/${SVN_BRANCH}"]; return map
          }
          if ("Tags".equals(BUILD_TYPE)) {
          def map = [SVN_REPO_PATH: "branches/${SVN_TAG}"]; return map
          }

          [EnvInject] - [ERROR] - SEVERE ERROR occurs: No such property: BUILD_TYPE for class: Script1
          Finished: FAILURE

          Show
          anup_achina Anup Singh added a comment - Gregory, First let me just thank you for such a prompt response. Honestly speaking I am not a big fan of open source mainly because of support. But my view is already changing. Having said that, I did what you request me and the issue persists. complete details follow: Started by user Anup Singh [EnvInject] - Loading node environment variables. [EnvInject] - Preparing an environment for the build. [EnvInject] - Keeping Jenkins system variables. [EnvInject] - Adding build parameters as variables. [EnvInject] - Evaluation the following Groovy script content: if ("Trunk".equals(BUILD_TYPE)) { def map = [SVN_REPO_PATH: "trunk/ConfigManagement"]; return map } if ("Branches".equals(BUILD_TYPE)) { def map = [SVN_REPO_PATH: "branches/${SVN_BRANCH}"] ; return map } if ("Tags".equals(BUILD_TYPE)) { def map = [SVN_REPO_PATH: "branches/${SVN_TAG}"] ; return map } [EnvInject] - [ERROR] - SEVERE ERROR occurs: No such property: BUILD_TYPE for class: Script1 Finished: FAILURE
          Hide
          anup_achina Anup Singh added a comment -

          Problem solved...got rid of curly braces around SVN_BRANCH and SVN_TAG. However its weird that I still has to use the $ sign in front of it but I have to use BUILD_TYPE without the preceding $.

          if ("Trunk".equals(BUILD_TYPE))

          { def map = [SVN_REPO_PATH: "trunk/ConfigManagement"]; return map }

          if ("Branches".equals(BUILD_TYPE))

          { def map = [SVN_REPO_PATH: "branches/$SVN_BRANCH"]; return map }

          if ("Tags".equals(BUILD_TYPE))

          { def map = [SVN_REPO_PATH: "tags/$SVN_TAG"]; return map }
          Show
          anup_achina Anup Singh added a comment - Problem solved...got rid of curly braces around SVN_BRANCH and SVN_TAG. However its weird that I still has to use the $ sign in front of it but I have to use BUILD_TYPE without the preceding $. if ("Trunk".equals(BUILD_TYPE)) { def map = [SVN_REPO_PATH: "trunk/ConfigManagement"]; return map } if ("Branches".equals(BUILD_TYPE)) { def map = [SVN_REPO_PATH: "branches/$SVN_BRANCH"]; return map } if ("Tags".equals(BUILD_TYPE)) { def map = [SVN_REPO_PATH: "tags/$SVN_TAG"]; return map }
          Hide
          koithara Sony Koithara added a comment -

          Can somebody help me out...

          I am using :
          def matcher = manager.getLogMatcher("ServerName=(.*)")
          if(matcher != null)

          { servername = matcher.group(1) manager.addShortText("$servername") def map = ["Sony": "hello"] return map }

          And the console output is:
          C:\Users+++++++++\.jenkins\jobs\servernamepoc\workspace>powershell d:\scripts\servername.ps1
          ServerName=hahaha

          C:\Users++++++++\.jenkins\jobs\servernamepoc\workspace>exit 0
          [EnvInject] - Injecting environment variables from a build step.
          Finished: SUCCESS

          However on checking the respective job's environment variables - it doesn't shows up...

          Any help is appreciated.

          Show
          koithara Sony Koithara added a comment - Can somebody help me out... I am using : def matcher = manager.getLogMatcher("ServerName=(.*)") if(matcher != null) { servername = matcher.group(1) manager.addShortText("$servername") def map = ["Sony": "hello"] return map } And the console output is: C:\Users+++++++++\.jenkins\jobs\servernamepoc\workspace>powershell d:\scripts\servername.ps1 ServerName=hahaha C:\Users++++++++\.jenkins\jobs\servernamepoc\workspace>exit 0 [EnvInject] - Injecting environment variables from a build step. Finished: SUCCESS However on checking the respective job's environment variables - it doesn't shows up... Any help is appreciated.
          Hide
          danielbeck Daniel Beck added a comment -

          This is an issue tracker, not a support site.

          Show
          danielbeck Daniel Beck added a comment - This is an issue tracker, not a support site.

            People

            • Assignee:
              koithara Sony Koithara
              Reporter:
              skumar suresh kukalakuntla
            • Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: