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

Dockerfile in Declarative pipeline job fails

詹金斯版本:-

Jenkins - 2.277.1 LTS.

我的Docker文件:-

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[=13=](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 容器上的 运行ning 它是 运行ning 在主容器上然后失败。因为我只想 运行 从我的节点系统(这是 Docker 主机)docker 容器上 build + test 步骤。那么我该如何在我的声明性管道中解决这个问题呢?请让我知道如何做到这一点。提前致谢。

在管道代码部分 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"
            }
        }
    }
}