Dockerfile in Declarative pipeline job fails

1.1k views Asked by At

Jenkins Version:-

Jenkins - 2.277.1 LTS.

My Dockerfile:-

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

My Declarative Pipeline:-

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

    stages {
        stage("Checkout") {
            steps {
                  git(
                  url: '[email protected]:maven-prj-group/mavenapp.git',
                  branch: "master"
                )
                
            }
        }

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

From Jenkins master i have configured Linux server as node VM-Linux-Agent and using this node pipeline job code checkout is happening and further using Dockerfile building a docker container then to run build and others steps on docker itself steps are not working. it shows below errors.

[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 step instead of running on docker container it is running on master then it fails. Since i want to run only build + test steps on docker container from my node system(This is Docker Host). So how do i fix this in my declarative pipeline? please let me know the way to do this. Thanks in advance.

1

There are 1 answers

0
user4948798 On BEST ANSWER

In the pipeline code section global agent replaced with none and to checkout the code on specific slave(In my case VM-Linux-Agent) added as agent label.

The below code working for me.

pipeline {
    agent none
 
    stages {
        stage("Checkout") {
            agent {
                label "VM-Linux-Agent"
            }
            steps {
                  git(
                  url: '[email protected]: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"
            }
        }
    }
}