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

Token expansion of a config file not happening

    Details

    • Type: Bug
    • Status: Resolved (View Workflow)
    • Priority: Major
    • Resolution: Fixed
    • Labels:
      None
    • Environment:
      Jenkins version : 2.7.1
      Config file provider plugin : 2.13
    • Similar Issues:

      Description

      I have a config file with some tokens that I expected to be replaced when used with a buildWrapper. But this did not work for me. My pipeline reads like

      node {
          checkout scm
          withEnv(['INSTANCE=Something']) {
              configFileProvider(
                  [configFile(fileId: 'prescribe', variable: 'DEPLOY_FILE')]) {
                  sh "echo $env.INSTANCE" 
                  sh "cat ${env.DEPLOY_FILE}"
              }
          }
      }
      

      And my 'prescribe' id'ed file looks like

      ${INSTANCE}
      ${branch}
      
      ${ENV, var=INSTANCE}
      
      ${ENV.INSTANCE}
      ${ENV,INSTANCE}
      
      ${env, var=INSTANCE}
      

      However the file does not come out as token replaced. I have also tried providing INSTANCE as a global password and global variable with the same result

        Attachments

          Activity

          Hide
          jfreedman Jon Freedam added a comment -

          This is total nonsense - suggesting installing a plugin to copy an environment variable set by another plugin in order to get a credential injected into a file.  This should just work.

          Show
          jfreedman Jon Freedam added a comment - This is total nonsense - suggesting installing a plugin to copy an environment variable set by another plugin in order to get a credential injected into a file.  This should just work.
          Hide
          jonhattan Jonathan Araña Cruz added a comment -

          Problem here is that Token Macro only accepts predefined env vars.

          I think the point is what mdonohue stated 8 years back at https://issues.jenkins-ci.org/browse/JENKINS-3605:

          That is not the way environment variables work. When you set an environment variable in a child shell, it is not accessible by the parent. You need to have the copy path set as a Hudson parameter.

          I've tried with parameters instead of env vars and it works.

          Also, here is a recommendation to set variables via EnvInject plugin - https://issues.jenkins-ci.org/browse/JENKINS-5942

          I've not tested EnvInject yet because there're Known incompatibilities with Pipeline plugin and I'm struggling my head around to find a way on all of this.

          Show
          jonhattan Jonathan Araña Cruz added a comment - Problem here is that Token Macro only accepts predefined env vars. I think the point is what mdonohue stated 8 years back at https://issues.jenkins-ci.org/browse/JENKINS-3605: That is not the way environment variables work. When you set an environment variable in a child shell, it is not accessible by the parent. You need to have the copy path set as a Hudson parameter. I've tried with parameters instead of env vars and it works. Also, here is a recommendation to set variables via EnvInject plugin - https://issues.jenkins-ci.org/browse/JENKINS-5942 I've not tested EnvInject yet because there're Known incompatibilities with Pipeline plugin and I'm struggling my head around to find a way on all of this.
          Hide
          scm_issue_link SCM/JIRA link daemon added a comment -

          Code changed in jenkins
          User: imod
          Path:
          pom.xml
          src/main/java/org/jenkinsci/plugins/configfiles/buildwrapper/ManagedFileUtil.java
          http://jenkins-ci.org/commit/config-file-provider-plugin/222e2df8d868186577df049afbaeb11b73639328
          Log:
          [FIXED JENKINS-39998] replace tokens in pipeline jobs too

          Show
          scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: imod Path: pom.xml src/main/java/org/jenkinsci/plugins/configfiles/buildwrapper/ManagedFileUtil.java http://jenkins-ci.org/commit/config-file-provider-plugin/222e2df8d868186577df049afbaeb11b73639328 Log: [FIXED JENKINS-39998] replace tokens in pipeline jobs too
          Hide
          sathyakumars Sathyakumar Seshachalam added a comment -

          Was still not able to get it work inspite of replaceTokens: true. Feel free to close this ticket if you feel this is definitely not a bug

          Show
          sathyakumars Sathyakumar Seshachalam added a comment - Was still not able to get it work inspite of replaceTokens: true. Feel free to close this ticket if you feel this is definitely not a bug
          Hide
          imod Dominik Bartholdi added a comment - - edited

          from what I can see in your script, you seem to have forgotten to define replaceTokens: true
          e.g.:

          node {
              checkout scm
              withEnv(['INSTANCE=Something']) {
                  configFileProvider(
                      [configFile(fileId: 'prescribe', variable: 'DEPLOY_FILE'), replaceTokens: true]) {
                      sh "echo $env.INSTANCE" 
                      sh "cat ${env.DEPLOY_FILE}"
                  }
              }
          }
          
          Show
          imod Dominik Bartholdi added a comment - - edited from what I can see in your script, you seem to have forgotten to define replaceTokens: true e.g.: node { checkout scm withEnv(['INSTANCE=Something']) { configFileProvider( [configFile(fileId: 'prescribe', variable: 'DEPLOY_FILE'), replaceTokens: true]) { sh "echo $env.INSTANCE" sh "cat ${env.DEPLOY_FILE}" } } }
          Hide
          sathyakumars Sathyakumar Seshachalam added a comment -

          Thanks for the reply. I am still unable to get this working. I do have Token macro plugin installed.

          I understood the unit test case, I gave the exact string ${ENV, var="JOB_NAME"} inside my config file, and yet they came back un-replaced.
          And I got
          ${ENV, var="JOB_NAME"}
          ${ENV, var="INSTANCE"}

          Any pointers on how to debug this which log to verify etc., ? From your reply, it seems like there is a configuration issue at my end, because ${INSTANCE} never really threw any error whatsoever and am unable to figure out what configuration I am missing.

          Show
          sathyakumars Sathyakumar Seshachalam added a comment - Thanks for the reply. I am still unable to get this working. I do have Token macro plugin installed. I understood the unit test case, I gave the exact string ${ENV, var="JOB_NAME"} inside my config file, and yet they came back un-replaced. And I got ${ENV, var="JOB_NAME"} ${ENV, var="INSTANCE"} Any pointers on how to debug this which log to verify etc., ? From your reply, it seems like there is a configuration issue at my end, because ${INSTANCE} never really threw any error whatsoever and am unable to figure out what configuration I am missing.
          Hide
          imod Dominik Bartholdi added a comment - - edited

          Token expansion is done with the Token Makro Plugin: https://wiki.jenkins-ci.org/display/JENKINS/Token+Macro+Plugin

          For this to work the token have to be referenced in the correct form:

          e.g. ${ENV, var="JOB_NAME"} - don't forget the ' " ' around the value of var

          using ${INSTANCE} will actually result in an error in your logs:

          [ERROR] failed to expand variables in content of prescribe - Unrecognized macro 'INSTANCE'

          There is also a testcase verifying this really works: https://github.com/jenkinsci/config-file-provider-plugin/blob/master/src/test/java/org/jenkinsci/plugins/configfiles/buildwrapper/ConfigFileBuildWrapperTest.java#L84

          I agree, that replacing ${INSTANCE} would be nice, but thats more a feature request, then a bug.

          Show
          imod Dominik Bartholdi added a comment - - edited Token expansion is done with the Token Makro Plugin: https://wiki.jenkins-ci.org/display/JENKINS/Token+Macro+Plugin For this to work the token have to be referenced in the correct form: e.g. ${ENV, var="JOB_NAME"} - don't forget the ' " ' around the value of var using ${INSTANCE} will actually result in an error in your logs: [ERROR] failed to expand variables in content of prescribe - Unrecognized macro 'INSTANCE' There is also a testcase verifying this really works: https://github.com/jenkinsci/config-file-provider-plugin/blob/master/src/test/java/org/jenkinsci/plugins/configfiles/buildwrapper/ConfigFileBuildWrapperTest.java#L84 I agree, that replacing ${INSTANCE} would be nice, but thats more a feature request, then a bug.

            People

            • Assignee:
              domi Dominik Bartholdi
              Reporter:
              sathyakumars Sathyakumar Seshachalam
            • Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: