1

詹金斯版本:-

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

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

4

1 回答 1

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"
            }
        }
    }
}
于 2021-05-05T11:40:28.470 回答