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

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

    • Icon: Improvement Improvement
    • Resolution: Fixed
    • Icon: Major Major
    • envinject-plugin
    • None
    • Redhat Linux 5.5

      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

          [JENKINS-13119] How can I set an environment variable based on value of user passed parameter?

          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

          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

          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
          }

          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 }

          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 }

          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 }

          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

          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

          Anup Singh added a comment -

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

          Anup Singh added a comment - Additional Info: SVN_BRANCH and SVN_TAG are other user input parameters.

          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?

          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?

          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

          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

          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
          }

          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 }

          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.

          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.

          Daniel Beck added a comment -

          This is an issue tracker, not a support site.

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

            koithara Sony Koithara
            skumar suresh kukalakuntla
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: