简体   繁体   中英

Unable to change a directory inside a Docker container through a Jenkins declarative pipeline

I'm trying to change the current directory using the dir command outlined here: https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#code-dir-code-change-current-directory

I've edited my pipeline to resemble something like this:

pipeline {
    agent { dockerfile true } 
    stages { 
        stage('Change working directory...') { 
            steps {
                dir('/var/www/html/community-edition') {
                    sh 'pwd'
                }
            }
        }
    }
}

It doesn't change the directory at all but instead tries to create a directory on the host and fails with java.io.IOException: Failed to mkdirs: /var/www/html/community-edition

Using sh cd /var/www/html/community-edition doesn't seem to work either. How do I change the directory in the container? Someone else seems to have had the same issue but had to change his pipeline structure to change the directory and doesn't sound like a reasonable fix. Isn't the step already being invoked in the container? https://issues.jenkins-ci.org/browse/JENKINS-46636

I had the same problem yesterday. It seems to be a bug that causes dir() not to change the directory when used inside a container. I've got it to work by executing the cd and pwd command at once, like this:

sh '(cd //var/www/html/community-edition && pwd)'

I had same issue and this worked for me when I had "ws" in jenkinsfile pipeline:

   stage('prepare') {
        steps {
            ws('/var/jenkins_home/workspace/pipeline@script/desiredDir') {
              sh ''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM