Issue
I'm using a declarative pipeline with sequential stages in Jenkins 2.280.
The dir
argument of dockerfile
should be obtained from ${params}
:
pipeline {
agent none
parameters {
choice(name: PLATFORM, choices: ['dir1', 'dir2', ...], description: '')
string(name: FOO, defaultValue: 1001, description: 'Interesting number')
}
stages {
stage('Top') {
agent {
dockerfile {
filename 'Dockerfile'
dir 'platforms/${params.PLATFORM}' // <-- HERE
label 'master'
additionalBuildArgs '--build-arg FOO=${params.FOO}'
args '--mount type=bind,source=/opt/tools,target=/tools,readonly'
}
} // agent
stages {
stage('Checkout') {
steps { checkout scm }
}
stage('Build') {
sh """
// call cmake
// call ninja
"""
}
...
} // stages
} // Top
} // stages
} // pipeline
This always seems to lead to a Java NoSuchFileException:
java.nio.file.NoSuchFileException: /var/jenkins_home/workspace/<ws_name>/platforms/${params.PLATFORM}/Dockerfile
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
If I hardcode the dir
argument to a valid directory name, then I run into issues with FOO
:
[Pipeline] withEnv
[Pipeline] {
[Pipeline] isUnix
[Pipeline] readFile
[Pipeline] sh
14:18:02 /var/jenkins_home/workspace/<ws_name>@tmp/durable-5884cc09/script.sh: 1: /var/jenkins_home/workspace/<ws_name>@tmp/durable-5884cc09/script.sh: Bad substitution
How can these two things be done?
Is there a better approach?
UPDATE: My problem was a "space" in the name of the pipeline project.
Solution
I tried to create an example similar to you. Please take a look How the Docker file and dockerfile agent need to be used in declarative pipeline
Dockerfile
FROM alpine
ARG PACKAGE
RUN apk add ${PACKAGE}
Jenkinsfile
pipeline {
agent {
dockerfile {
filename 'Dockerfile'
dir "${params.DOCKERFILE_PATH}"
additionalBuildArgs "--build-arg PACKAGE=${params.PACKAGE}"
args '-v /tmp:/tmp'
}
}
parameters {
string(name: 'DOCKERFILE_PATH', defaultValue: 'src01', description: 'Pick Dockerfile from a folder')
text(name: 'PACKAGE', defaultValue: 'curl', description: 'Package To Be Installed')
}
stages {
stage('build'){
steps {
sh 'git version'
}
}
}
}
groovy has to interpolate your runtime parameter and for that, you have to wrap it with ""
. ''
will not work
In your case, it will be
dockerfile {
filename 'Dockerfile'
dir "platforms/${params.PLATFORM}"
label 'master'
additionalBuildArgs "--build-arg FOO=${params.FOO}"
args '--mount type=bind,source=/opt/tools,target=/tools,readonly'
}
Answered By - Samit Kumar Patel
Answer Checked By - Cary Denson (JavaFixing Admin)