Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins declarative pipeline with Docker/Dockerfile agent from SCM

With Jenkins using the Declarative Pipeline Syntax how do i get the Dockerfile (Dockerfile.ci in this example) from the SCM (Git) since the agent block is executed before all the stages?

pipeline {
    agent {
        dockerfile {
            filename 'Dockerfile.ci'
        }
    }
    stage ('Checkout') {
        steps {
            git(
                url: 'https://www.github.com/...',
                credentialsId: 'CREDENTIALS',
                branch: "develop"
            )
        }
    }
    [...]
}

In all the examples i've seen, the Dockerfile seems to be already present in the workspace.

like image 797
Joshua Avatar asked May 08 '18 00:05

Joshua


2 Answers

You could try to declare agent for each stage separately, for checkout stage you could use some default agent and docker agent for others.

pipeline {
    agent none
    stage ('Checkout') {
        agent any
        steps {
            git(
                url: 'https://www.github.com/...',
                credentialsId: 'CREDENTIALS',
                branch: "develop"
            )
        }
    }
    stage ('Build') {
        agent {
            dockerfile {
            filename 'Dockerfile.ci'
        }
        steps {
            [...]
        }
}
    }
    [...]
}
like image 144
kamillitw Avatar answered Oct 02 '22 10:10

kamillitw


If you're using a multi-branch pipeline it automatically checks out your SCM before evaluating the agent. So in that case you can specify the agent from a file in the SCM.

like image 44
Steve Avatar answered Oct 02 '22 11:10

Steve