声明性管道作业中的 Dockerfile 失败

时间:2021-05-05 06:45:53

标签: jenkins jenkins-pipeline devops jenkins-declarative-pipeline jenkins-docker

Jenkins 版本:-

Jenkins - 2.277.1 LTS.

我的 Dockerfile:-

FROM maven:3.6.0-jdk-13
RUN useradd -m -u 1000 -s /bin/bash jenkins

我的声明式管道:-

pipeline {
    agent {
        label "VM-Linux-Agent"
    }
    environment {
        DOCKERFILE = "Dockerfile"
    }

    stages {
        stage("Checkout") {
            steps {
                  git(
                  url: 'git@gitlab.company.com:maven-prj-group/mavenapp.git',
                  branch: "master"
                )
                
            }
        }

        stage("Build") {
                agent {
                   dockerfile {
                   filename DOCKERFILE   
                   args "-v $WORKSPACE:/var/maven" 
               }
            }   
            steps {
                sh "mvn clean install"
            }
        }
    }
}

Jenkins master 我已将 Linux 服务器配置为节点 VM-Linux-Agent 并使用此节点管道作业 code checkout 正在发生并进一步使用 Dockerfile 构建一个 docker 容器然后运行构建其他人对 docker 本身的步骤不起作用。它显示以下错误。

[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/Dockerfile-Pipeline
[Pipeline] {
[Pipeline] isUnix
[Pipeline] readFile
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.nio.file.NoSuchFileException: /var/jenkins_home/workspace/Dockerfile-Pipeline/Dockerfile
    at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
    at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
    at java.nio.file.Files.newByteChannel(Files.java:361)
    at java.nio.file.Files.newByteChannel(Files.java:407)
    at java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)
    at java.nio.file.Files.newInputStream(Files.java:152)
    at hudson.FilePath.newInputStreamDenyingSymlinkAsNeeded(FilePath.java:2112)
    at hudson.FilePath.read(FilePath.java:2097)
    at hudson.FilePath.read(FilePath.java:2089)
    at org.jenkinsci.plugins.workflow.steps.ReadFileStep$Execution.run(ReadFileStep.java:104)
    at org.jenkinsci.plugins.workflow.steps.ReadFileStep$Execution.run(ReadFileStep.java:94)
    at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE

Build 步骤而不是在 docker 容器上运行,而是在 master 上运行,然后失败。因为我只想从我的节点系统(这是 Docker 主机)在 docker 容器上运行 build + test 步骤。那么我如何在我的声明性管道中解决这个问题?请让我知道如何做到这一点。提前致谢。

1 个答案:

答案 0 :(得分:0)

在管道代码部分 global agent 替换为 none 并检查特定从属设备上的代码(在我的情况下为 VM-Linux-Agent)添加为 agent label

以下代码对我有用。

pipeline {
    agent none
 
    stages {
        stage("Checkout") {
            agent {
                label "VM-Linux-Agent"
            }
            steps {
                  git(
                  url: 'git@gitlab.company.com:maven-prj-group/mavenapp.git',
                  branch: "master"
                )
            }
        }

        stage("Build") {
                agent {
                   dockerfile {
                   filename 'Dockerfile'  
                   label 'VM-Linux-Agent'
                   args "-v /home/user/maven:/var/maven" 
               }
            }   
            steps {
                sh "mvn clean install"
            }
        }
    }
}