使用Docker代理在Jenkins管道中收到“目录不存在”错误

时间:2019-01-07 15:57:34

标签: docker jenkins jenkins-pipeline

我正在使用Docker代理编写我的第一个Jenkins管道。执行管道时,在Run Docker Image阶段的日志中出现以下错误(请注意,为清楚起见已删除了管道步骤):

sh: can't create /data/jenkins/workspace/my-workspace@tmp/durable-01234567/pid: nonexistent directory

Jenkins日志

Started by user Doug R.
 > git rev-parse --is-inside-work-tree # timeout=10
Setting origin to http://my-git.example.com/scm/im/my-project.git
 > git config remote.origin.url http://my-git.example.com/scm/im/my-project.git # timeout=10
Fetching origin...
Fetching upstream changes from origin
 > git --version # timeout=10
 > git fetch --tags --progress origin +refs/heads/*:refs/remotes/origin/*
Seen branch in repository origin/master
Seen 1 remote branch
Obtained Jenkinsfile from 01234567890abcdef01234567890abcdef
Running in Durability level: MAX_SURVIVABILITY
Running on Jenkins in /data/jenkins/workspace/my-workspace
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url http://my-git.example.com/scm/im/my-project.git # timeout=10
Fetching without tags
Fetching upstream changes from http://my-git.example.com/scm/im/my-project.git
 > git --version # timeout=10
 > git fetch --no-tags --progress http://my-git.example.com/scm/im/my-project.git +refs/heads/*:refs/remotes/origin/*
Checking out Revision 01234567890abcdef01234567890abcdef (master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 01234567890abcdef01234567890abcdef
Commit message: "Yet another test"
 > git rev-list --no-walk 01234567890abcdef01234567890abcdef # timeout=10
[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] done
Cloning the remote Git repository
Cloning with configured refspecs honoured and without tags
Cloning repository http://my-git.example.com/scm/im/my-project.git
 > git init /data/jenkins/workspace/my-workspace # timeout=10
Fetching upstream changes from http://my-git.example.com/scm/im/my-project.git
 > git --version # timeout=10
 > git fetch --no-tags --progress http://my-git.example.com/scm/im/my-project.git +refs/heads/*:refs/remotes/origin/*
 > git config remote.origin.url http://my-git.example.com/scm/im/my-project.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url http://my-git.example.com/scm/im/my-project.git # timeout=10
Fetching without tags
Fetching upstream changes from http://my-git.example.com/scm/im/my-project.git
 > git fetch --no-tags --progress http://my-git.example.com/scm/im/my-project.git +refs/heads/*:refs/remotes/origin/*
Checking out Revision 01234567890abcdef01234567890abcdef (master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 01234567890abcdef01234567890abcdef
Commit message: "Running without reuseNode true"
Running on Jenkins in /data/jenkins/workspace/my-workspace@2
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url http://my-git.example.com/scm/im/my-project.git # timeout=10
Fetching without tags
Fetching upstream changes from http://my-git.example.com/scm/im/my-project.git
 > git --version # timeout=10
 > git fetch --no-tags --progress http://my-git.example.com/scm/im/my-project.git +refs/heads/*:refs/remotes/origin/*
Checking out Revision 01234567890abcdef01234567890abcdef (master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 01234567890abcdef01234567890abcdef
Commit message: "Running without reuseNode true"
Wrote authentication to /root/.docker/config.json
[my-workspace@2] Running shell script
+ docker inspect -f . alpine:3.8
.
Jenkins does not seem to be running inside a container
$ docker run -t -d -u 0:0 -w /data/jenkins/workspace/my-workspace@2 -v /data/jenkins/workspace/my-workspace@2:/data/jenkins/workspace/my-workspace@2:rw,z -v /data/jenkins/workspace/my-workspace@2@tmp:/data/jenkins/workspace/my-workspace@2@tmp:rw,z -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** --entrypoint cat alpine:3.8
[my-workspace@2] Running shell script
sh: can't create /data/jenkins/workspace/my-workspace@2@tmp/durable-0123457/pid: nonexistent directory
sh: can't create /data/jenkins/workspace/my-workspace@2@tmp/durable-0123457/jenkins-log.txt: nonexistent directory
sh: can't create /data/jenkins/workspace/my-workspace@2@tmp/durable-0123457/jenkins-result.txt: nonexistent directory
$ docker stop --time=1 01234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef
$ docker rm -f 01234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef
ERROR: script returned exit code -2
Finished: FAILURE

Jenkinsfile

根据我的判断,Jenkinsfile似乎写得正确,但是我确定不是。

import groovy.json.*
pipeline {

    environment {
        COMPOSE_TLS_VERSION = "TLSv1_2"
        DOCKER_TLS_VERIFY = 1
        DOCKER_CERT_PATH = "/opt/ucp"
        DOCKER_HOST = "tcp://my-docker-host.example.com:443"
    }

    agent any
    stages {
        stage('Prepare Workspace') {
            steps {
                script {
                    step([$class: 'WsCleanup'])
                    checkout scm
                }
            }
        }

        stage('Run Docker Image') {
            agent {
                docker {
                    reuseNode true
                    image 'alpine:3.8'
                    registryUrl 'https://my-docker-registry.example.com'
                    registryCredentialsId 'my-docker-credentials'
                }
            }
            steps {
                sh 'ls -la'
                sh 'printenv'
            }
        }
    }
}

更新

我找到了这个链接,该链接讨论的是Docker-in-Docker场景,但是我没有运行Docker-in-Docker。

2 个答案:

答案 0 :(得分:0)

我的问题有不同的根本原因,但与Docker-in-Docker(见上文)是相同的基本问题。

在我的情况下,Jenkins在与Jenkins所在的位置不同的 节点上运行docker映像,因此它无法访问我的工作区目录。因此,本文中确定的解决方案也可能会起作用:

  

使其工作的一种方法是从主机共享代理工作空​​间,在您的情况下,使用/ jenkins / workspace作为代理中的卷,这样,由代理写入的数据实际上就写入了主机,然后可以与新容器共享。您还可以将代理定义中的工作区位置更改为默认詹金斯以外的其他位置。

但是,我没有执行此操作所需的特权,因此我目前正在使用一种解决方法,即构建一个临时映像,然后在其中执行构建的那一部分。虽然这对我来说适用,但不太可能在所有情况下都适用。

答案 1 :(得分:0)

我有一个类似的问题,也许我的发现可以帮助您确定根本原因。 我在Windows主机上运行Jenkins Master,在此Master上我想使用带有管道的Linux Docker容器。我使用的管道是official Jenkins Examples中的管道:

pipeline {
agent {
    docker { image 'node:7-alpine' }
}
stages {
    stage('Test') {
        steps {
            sh 'node --version'
        }
    }
}

}

我收到的错误消息是这样的:

sh: can't create /d/Sandbox_GIT/Jenkins_Plugin/docker-workflow-plugin-master/work/workspace/test@tmp/durable-f0f56139/jenkins-log.txt: nonexistent directory

但是其他在带有Linux Master的Windows Slave上运行此命令的人似乎正在为他们运行。 对此有一个官方的错误报告:JENKINS-60473,此报告已部分解决。 要以与我相同的方式重现该行为,必须使用docker工作流插件的此分支:docker workflow plugin,否则您将获得不同的Error行为。

相关问题