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

Git plugin does not evaluate environment variables for branch option when use "Pipeline script from SCM" option

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      Hint from "Branches to build" option in "Pipeline script from SCM" option tells that:

      ${ENV_VARIABLE}
       It is also possible to use environment variables. In this case the variables are evaluated and the result is used as described above.
       E.g. ${TREEISH}, refs/tags/${TAGNAME}, ...
      

       However, environment variable not evaluated, resulting following error on script call:

      hudson.plugins.git.GitException: Command "git.exe fetch --tags --force --progress --prune -- origin +refs/heads/${SCRIPTS_REPO_BRANCH_NAME}:refs/remotes/origin/${SCRIPTS_REPO_BRANCH_NAME}" returned status code 128:
      stdout: 
      stderr: load pubkey "C:\\Windows\\TEMP\\jenkins-gitclient-ssh2322785094198445571.key": invalid format
      fatal: couldn't find remote ref refs/heads/${SCRIPTS_REPO_BRANCH_NAME}	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2450)
      	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:2051)
      	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$500(CliGitAPIImpl.java:84)
      	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:573)
      	at jenkins.plugins.git.GitSCMFileSystem$BuilderImpl.build(GitSCMFileSystem.java:365)
      	at jenkins.scm.api.SCMFileSystem.of(SCMFileSystem.java:197)
      	at jenkins.scm.api.SCMFileSystem.of(SCMFileSystem.java:173)
      	at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:115)
      	at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:69)
      	at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:309)
      	at hudson.model.ResourceController.execute(ResourceController.java:97)
      	at hudson.model.Executor.run(Executor.java:428)
      Finished: FAILURE
      

       

      Steps to reproduce:

      1. Create any pipeline script in git repo
      2. Go to "Configure System" menu in your Jenkins web app (%YOUR_URL%/configure)
      3. Create new environment variable "SCRIPTS_REPO_BRANCH_NAME", set its value to the branch name, you script lies under
      4. Create new pipeline script (%YOUR_URL%/view/all/newJob), "Pipeline"
      5. Select "Pipeline script from SCM" menu
      6. Fill all required field, set "Branches to build" = ${SCRIPTS_REPO_BRANCH_NAME}
      7. Run script, see the error, mentioned above
      8. Go back to script configuration replace "Branches to build" value with plain branch name
      9. Run script again, see it checked out and picked properly

        Attachments

          Activity

          Hide
          dethecus Brian Thompson added a comment -

          If you're on Windows wouldn't you want to use %SCRIPTS_REPO_BRANCH_NAME% instead?  Perhaps the info message could be spruced up a bit in git-plugin for Windows users.

          Show
          dethecus Brian Thompson added a comment - If you're on Windows wouldn't you want to use %SCRIPTS_REPO_BRANCH_NAME% instead?  Perhaps the info message could be spruced up a bit in git-plugin for Windows users.
          Hide
          alpanshin Alexandr Panshin added a comment - - edited

          Hello, Brian!

          Just tried, no, unfortunately result is the same.
          None of following results are working:

          SCRIPTS_REPO_BRANCH_NAME
          %SCRIPTS_REPO_BRANCH_NAME%
          %env.SCRIPTS_REPO_BRANCH_NAME%
          $SCRIPTS_REPO_BRANCH_NAME
          $env.SCRIPTS_REPO_BRANCH_NAME

          Seems, plugin just does not evaluate variables at all, when reading "Branches to build" parameter

           

          hudson.plugins.git.GitException: Command "git.exe fetch --tags --force --progress --prune -- origin +refs/heads/%SCRIPTS_REPO_BRANCH_NAME%:refs/remotes/origin/%SCRIPTS_REPO_BRANCH_NAME%" returned status code 128:
          stdout: 
          stderr: load pubkey "C:\\Windows\\TEMP\\jenkins-gitclient-ssh6501268685771990839.key": invalid format
          fatal: couldn't find remote ref refs/heads/%SCRIPTS_REPO_BRANCH_NAME%	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2450)
          	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:2051)
          	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$500(CliGitAPIImpl.java:84)
          	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:573)
          	at jenkins.plugins.git.GitSCMFileSystem$BuilderImpl.build(GitSCMFileSystem.java:365)
          	at jenkins.scm.api.SCMFileSystem.of(SCMFileSystem.java:197)
          	at jenkins.scm.api.SCMFileSystem.of(SCMFileSystem.java:173)
          	at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:115)
          	at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:69)
          	at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:309)
          	at hudson.model.ResourceController.execute(ResourceController.java:97)
          	at hudson.model.Executor.run(Executor.java:428)
          Finished: FAILURE
          

           

          Show
          alpanshin Alexandr Panshin added a comment - - edited Hello, Brian! Just tried, no, unfortunately result is the same. None of following results are working: SCRIPTS_REPO_BRANCH_NAME %SCRIPTS_REPO_BRANCH_NAME% %env.SCRIPTS_REPO_BRANCH_NAME% $SCRIPTS_REPO_BRANCH_NAME $env.SCRIPTS_REPO_BRANCH_NAME Seems, plugin just does not evaluate variables at all, when reading "Branches to build" parameter   hudson.plugins.git.GitException: Command "git.exe fetch --tags --force --progress --prune -- origin +refs/heads/%SCRIPTS_REPO_BRANCH_NAME%:refs/remotes/origin/%SCRIPTS_REPO_BRANCH_NAME%" returned status code 128: stdout: stderr: load pubkey "C:\\Windows\\TEMP\\jenkins-gitclient-ssh6501268685771990839.key" : invalid format fatal: couldn't find remote ref refs/heads/%SCRIPTS_REPO_BRANCH_NAME% at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2450) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:2051) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$500(CliGitAPIImpl.java:84) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:573) at jenkins.plugins.git.GitSCMFileSystem$BuilderImpl.build(GitSCMFileSystem.java:365) at jenkins.scm.api.SCMFileSystem.of(SCMFileSystem.java:197) at jenkins.scm.api.SCMFileSystem.of(SCMFileSystem.java:173) at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:115) at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:69) at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:309) at hudson.model.ResourceController.execute(ResourceController.java:97) at hudson.model.Executor.run(Executor.java:428) Finished: FAILURE  
          Hide
          dethecus Brian Thompson added a comment -

          Thanks for trying that out. I'll take a closer look when I get back in a few hours.

          Show
          dethecus Brian Thompson added a comment - Thanks for trying that out. I'll take a closer look when I get back in a few hours.
          Hide
          dethecus Brian Thompson added a comment -

          I was able to reproduce the issue in my docker container. I'm guessing that the plugin doesn't respect environment variables for branch names, but I see in the plugin code that it does support environment variables. I don't know the code well enough to put in a fix.

          Show
          dethecus Brian Thompson added a comment - I was able to reproduce the issue in my docker container. I'm guessing that the plugin doesn't respect environment variables for branch names, but I see in the plugin code that it does support environment variables. I don't know the code well enough to put in a fix.
          Hide
          alpanshin Alexandr Panshin added a comment -

          Thank you for investigation, Brian!

          Can you please point me to the place in code, where this evaluation should be done?

          I do not know code well enough, but as I am interested in this feature, maybe I will try to understand it

          Show
          alpanshin Alexandr Panshin added a comment - Thank you for investigation, Brian! Can you please point me to the place in code, where this evaluation should be done? I do not know code well enough, but as I am interested in this feature, maybe I will try to understand it
          Hide
          lexmaul Manfred added a comment -

          I experienced the same problem. I could solve it by deactivating the "Lightweight checkout".

          Maybe anyone can extend the documentation or even make it working for lightweight checkouts

          Show
          lexmaul Manfred added a comment - I experienced the same problem. I could solve it by deactivating the "Lightweight checkout". Maybe anyone can extend the documentation or even make it working for lightweight checkouts
          Hide
          alpanshin Alexandr Panshin added a comment - - edited

          Thank you Manfred! Works right now!
          Just in case, someone will read this thread facing same problem, right syntax is $SCRIPTS_REPO_BRANCH_NAME (for Windows server too)

          Show
          alpanshin Alexandr Panshin added a comment - - edited Thank you Manfred ! Works right now! Just in case, someone will read this thread facing same problem, right syntax is $SCRIPTS_REPO_BRANCH_NAME (for Windows server too)
          Hide
          harshitchopra0712 Harshit added a comment -

          I have opened a PR for the issue: https://github.com/jenkinsci/git-plugin/pull/1062

          Show
          harshitchopra0712 Harshit added a comment - I have opened a PR for the issue: https://github.com/jenkinsci/git-plugin/pull/1062

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            alpanshin Alexandr Panshin
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated: