skip to Main Content

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


  1. Chosen as BEST ANSWER

    I found a workable solution. If you use Pipeline script from SCM, the agent {dockerfile true} command can find the dockerfile.

    Instructions to change:

    1. In the Jenkins web GUI, select the Pipeline
    2. Click Configure
    3. Copy text of Pipeline script from the form window into a new file called Jenkinsfile in your repo
    4. Change the Pipeline Definition from 'Pipeline script' to 'Pipeline script from SCM'
    5. Click Save
    6. Click Build Now

    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.


  2. You can use dir() to specific workdir on jenkins machine, for example

    stage ("Checkout") {
        steps {
            dir('source'){
               checkout scmGit(branches: [[name: '*/master']], 
                        extensions: [], 
                        userRemoteConfigs: [[url: '/myPath/myRepo/']])
            }
        }
    }
    

    make it easy to handle task and less of confuse for Jenkins machine.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search