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

`dir` does not change working directory for contained steps

    XMLWordPrintable

Details

    • docker-workflow 1.19

    Description

      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.

       

      Attachments

        Activity

          mslattery 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 + "'; \"$@\"", "--"));
          
          mslattery 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 + "' ; \" $@\ "", " --"));
          imdahmd Imdad Ahmed added a comment -

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

          imdahmd Imdad Ahmed added a comment - mslattery Is your fix merged into master then? Can you point me to a PR if there is one?
          mslattery Michael Slattery added a comment - imdahmd - https://github.com/jenkinsci/docker-workflow-plugin/pull/156
          oleg_nenashev 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 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
          dnusbaum Devin Nusbaum added a comment -

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

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

          People

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

            Dates

              Created:
              Updated:
              Resolved: