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

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

    XMLWordPrintable

    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
          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: