I am new to Jenkins and think I missed a step when setting up Jenkins.
I have Jenkins 2.401.3 running in a local docker container. I have a git repo on my local linux machine.
Basic hello world examples, such as "Through the classic UI" from here work as expected: https://www.jenkins.io/doc/book/pipeline/getting-started/
I start having trouble when I try to pull in my source code. I want my Jenkins pipeline to use the Dockerfile in the git repo to build the rest of my code.
I would like to do something similar to the example from https://www.jenkins.io/doc/book/pipeline/docker/#dockerfile
pipeline {
agent { dockerfile true }
stages {
stage('Test') {
steps {
sh 'node --version'
sh 'svn --version'
}
}
}
}
When I try to run the example code, Jenkins does not find my dockerfile. When I add a new stage to checkout my code, it does seem to find my code, but won’t use the dockerfile in the next stage.
pipeline {
agent any
//agent { dockerfile true } //this line doesn't work
stages {
stage('Checkout') {
steps {
checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: '/myPath/myRepo/']])
}
}
stage('Build') {
agent { dockerfile true }
steps {
sh 'python3 --version' //this should output the version installed by my Dockerfile
}
}
}
}
I never set a "source repository" and can’t find an option to do so.
What do I need to do for Pipeline to recognize my Dockerfile?
2
Answers
I found a workable solution. If you use Pipeline script from SCM, the
agent {dockerfile true}
command can find the dockerfile.Instructions to change:
The 'Checkout' stage in my original post is now redundant. I don't understand why my original method didn't work, but using a Jenkinsfile is a workable solution.
You can use dir() to specific workdir on jenkins machine, for example
make it easy to handle task and less of confuse for Jenkins machine.