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

          Michael Wu added a comment - - edited

          I have worked it around by using `withDockerContainer` step.

          stage('change directory') {
              steps {
                  dir('new-dir') {
                      withDockerContainer(image: 'my-image') {
                          sh 'pwd'
                      }
                  }
              }
           }
          

          Basically you use dir to change directory before launching the container, and the when booting up a container, the current path will be made the working directory inside the container.
           

           

          Michael Wu added a comment - - edited I have worked it around by using `withDockerContainer` step. stage( 'change directory' ) {     steps {         dir( ' new -dir' ) {             withDockerContainer(image: 'my-image' ) {                 sh 'pwd'             }         }     } } Basically you use dir to change directory before launching the container, and the when booting up a container, the current path will be made the working directory inside the container.    

          Dave P added a comment -

          I haven't tested Michael Wu's work-around, but regardless, it's still a work-around – it is not a fix.

          Should fix the bug so that It works as documented (preferred) or update documentation with the work-around.

          Dave P added a comment - I haven't tested Michael Wu's work-around, but regardless, it's still a work-around – it is not a fix. Should fix the bug so that It works as documented (preferred) or update documentation with the work-around.

          I'd like to see this too. Currently, I'm running every one of my sh commands like this:

          ```

          sh "cd dir && command"

          sh "cd dir && othercommand"

          ```

          As you can see, this is quite annoying. It would be great if I could just surround it by a `dir` block and have it just, work.

          Christopher Smith added a comment - I'd like to see this too. Currently, I'm running every one of my sh commands like this: ``` sh "cd dir && command" sh "cd dir && othercommand" ``` As you can see, this is quite annoying. It would be great if I could just surround it by a `dir` block and have it just, work.

          Logs:

          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

           

          Output of docker version:

           

          Docker version 17.12.0-ce, build c97c6d6

          Host ubuntu server 16.04 LTS

           

          Jenkins Blue Ocean running as a docker container version:

           

          1.4.1  · Core 2.89.3  · 61988a2
          

          This is a annoying specially because my repository has multprojects Angular and Java, and I need to change directory for start the builds.

          Giovanni Silva added a comment - Logs: 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   Output of docker version:   Docker version 17.12.0-ce, build c97c6d6 Host ubuntu server 16.04 LTS   Jenkins Blue Ocean running as a docker container version:   1.4.1  · Core 2.89.3  · 61988a2 This is a annoying specially because my repository has multprojects Angular and Java, and I need to change directory for start the builds.

          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: