10
votes

According to jenkins documentation referenced here, to ensure that docker agent defined on a particular stage run on the same node defined in the pipeline, the flag reuseNode must be set to true.

reuseNode A boolean, false by default. If true, run the container on the node specified at the top-level of the Pipeline, in the same workspace, rather than on a new node entirely.This option is valid for docker and dockerfile, and only has an effect when used on an agent for an individual stage.

For declarative this can be achieved using

   agent {
                 docker {
                     image 'gradle-java:0.0.1'
                     reuseNode true
                 }
             }

However I am unable to find any example of how to set this in scripted pipelines. Can somebody help with how to achieve this in scripted pipelines?

2
Did you ever find the answer to this?JoAMoS
anyone have a solution for this?cmlonder

2 Answers

0
votes

In new version of declarative pipeline, it has been enhanced and suggesting to use label

agent {
    docker {
        image 'maven:3-alpine'
        label 'my-defined-label'
        args  '-v /tmp:/tmp'
    }
}

If you want to do the same with the scripted pipeline , mention the agent label name in node(agentName), it would be like

node("my-defined-label") {
  docker.image('maven:3-alpine').inside('-v $HOME/.m2:/root/.m2') {
        stage('Build') {
            sh 'mvn -B'
        }
   }
}
0
votes

I found that the way to do this in the scripted pipeline, via the use of docker.image(dockerImage).inside(dockerArgs), is not to include it at all. From what I can tell, opposite of the declarative pipeline, this runs on the same node by default.

Instead, if you wanted to run on a different node, you would insert use of node:

node {
    docker.image(dockerImage).inside(dockerArgs) {
        sh '''echo container'''
    }
}