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

`dir` does not change working directory for contained steps

    • docker-workflow 1.19

      It's said here, https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#code-dir-code-change-current-directory

      Change current directory. Any step inside the dir block will use this directory as current and any relative path will use it as base path.

      However, I can't seem to get it work. I use declarative syntax with a very basic Jenkinsfile

       

      pipeline {
          agent none
      
          stages {
      
              stage('Change working directory...') {
                  agent { docker 'my-image' }
      
                  steps {
                      dir('new-dir') {
                          sh 'pwd'
                      }
                  }
              }
          }
      }

      What it does is simply create a new folder called "new-dir" in the current working directory and then the pwd prints out the current working directory instead of navigating into the "new-dir".

      Am I misusing this step because I can't find many examples online. I have found some relevant issues here, but none of them is of any help to me.

       

          [JENKINS-46636] `dir` does not change working directory for contained steps

          Maybe the code that checks for version has a bug, that do not parse 17.12.0-ce

           

          Or maybe the container jenkis has no access to docker. But I'm running with docker.sock mapped correctly

          Giovanni Silva added a comment - Maybe the code that checks for version has a bug, that do not parse 17.12.0-ce   Or maybe the container jenkis has no access to docker. But I'm running with docker.sock mapped correctly

          Frederic Rousseau added a comment - - edited

          dir never worked in declarative pipelines. I didn't try in scripted pipeline. I do like Christopher, I prefix my commands with "cd my-directory &&" and it's not sexy at all to see this in the Blue Ocean UI

          Frederic Rousseau added a comment - - edited dir never worked in declarative pipelines. I didn't try in scripted pipeline. I do like Christopher, I prefix my commands with "cd my-directory &&" and it's not sexy at all to see this in the Blue Ocean UI

          I can confirm that this is not working in declarative in Jenkins 1.7.0.

          Rutger de Knijf added a comment - I can confirm that this is not working in declarative in Jenkins 1.7.0.

          Umesh Sharma added a comment -

          Any updates or solution to this issue

           

          Docker version is older than 17.12, working directory will be /var/jenkins_home/workspace/gsilva-pro_master-FNQGFOZNCGYYR7K4UGJOX5A6UTRZHRZ2ZIJHDRTUJOUALWVMZQJA not /var/jenkins_home/workspace/gsilva-pro_master-FNQGFOZNCGYYR7K4UGJOX5A6UTRZHRZ2ZIJHDRTUJOUALWVMZQJA/web-ui

          Umesh Sharma added a comment - Any updates or solution to this issue   Docker version is older than 17.12, working directory will be /var/jenkins_home/workspace/gsilva-pro_master-FNQGFOZNCGYYR7K4UGJOX5A6UTRZHRZ2ZIJHDRTUJOUALWVMZQJA not /var/jenkins_home/workspace/gsilva-pro_master-FNQGFOZNCGYYR7K4UGJOX5A6UTRZHRZ2ZIJHDRTUJOUALWVMZQJA/web-ui

          Oleg Nenashev added a comment -

          burtsevyg what do you expect from me by assigning me to this ticket?

          Oleg Nenashev added a comment - burtsevyg what do you expect from me by assigning me to this ticket?

          Michael Slattery added a comment - - edited

          I was able to fix the issue by applying the following patch to https://github.com/jenkinsci/docker-workflow-plugin, building the .hpi w/mvn, and placing it into my Jenkins server.

          diff --git a/src/main/java/org/jenkinsci/plugins/docker/workflow/WithContainerStep.java b/src/main/java/org/jenkinsci/plugins/docker/workflow/WithContainerStep.java
          index 862b454..efcb6b2 100644
          --- a/src/main/java/org/jenkinsci/plugins/docker/workflow/WithContainerStep.java
          +++ b/src/main/java/org/jenkinsci/plugins/docker/workflow/WithContainerStep.java
          @@ -255 +255,2 @@ public class WithContainerStep extends AbstractStepImpl {
          -                                    launcher.getListener().getLogger().println("Docker version is older than 17.12, working directory will be " + ws + " not " + path);
          +                                    String safePath = path.replace("'", "'\"'\"'");
          +                                    starter.cmds().addAll(0, Arrays.asList("sh", "-c", "cd '" + safePath + "'; \"$@\"", "--"));
          

          Michael Slattery added a comment - - edited I was able to fix the issue by applying the following patch to https://github.com/jenkinsci/docker-workflow-plugin , building the .hpi w/mvn, and placing it into my Jenkins server. diff --git a/src/main/java/org/jenkinsci/plugins/docker/workflow/WithContainerStep.java b/src/main/java/org/jenkinsci/plugins/docker/workflow/WithContainerStep.java index 862b454..efcb6b2 100644 --- a/src/main/java/org/jenkinsci/plugins/docker/workflow/WithContainerStep.java +++ b/src/main/java/org/jenkinsci/plugins/docker/workflow/WithContainerStep.java @@ -255 +255,2 @@ public class WithContainerStep extends AbstractStepImpl { - launcher.getListener().getLogger().println( "Docker version is older than 17.12, working directory will be " + ws + " not " + path); + String safePath = path.replace( " '" , "' \" '\ "' " ); + starter.cmds().addAll(0, Arrays.asList( "sh" , "-c" , "cd '" + safePath + "' ; \" $@\ "", " --"));

          Imdad Ahmed added a comment -

          mslattery Is your fix merged into master then? Can you point me to a PR if there is one?

          Imdad Ahmed added a comment - mslattery Is your fix merged into master then? Can you point me to a PR if there is one?

          Michael Slattery added a comment - imdahmd - https://github.com/jenkinsci/docker-workflow-plugin/pull/156

          Oleg Nenashev added a comment -

          I have unassigned the ticket, because I am not the plugin maintainer. The pull request looks good to me tho

          Oleg Nenashev added a comment - I have unassigned the ticket, because I am not the plugin maintainer. The pull request looks good to me tho

          Devin Nusbaum added a comment -

          A fix for this issue was just released in Docker Pipeline Plugin version 1.19.

          Devin Nusbaum added a comment - A fix for this issue was just released in Docker Pipeline Plugin version 1.19.

            Unassigned Unassigned
            michaelonline Michael Wu
            Votes:
            18 Vote for this issue
            Watchers:
            23 Start watching this issue

              Created:
              Updated:
              Resolved: