Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reuse agent (docker container) in Jenkins between multiple stages

I have a pipeline with multiple stages, and I want to reuse a docker container between only "n" number of stages, rather than all of them:

pipeline {
   agent none

   stages {
       stage('Install deps') {
            agent {
                docker { image 'node:10-alpine' }
            }

            steps {
                sh 'npm install'
            }
        }

       stage('Build, test, lint, etc') {
            agent {
                docker { image 'node:10-alpine' }
            }

            parallel {
                stage('Build') {
                    agent {
                        docker { image 'node:10-alpine' }
                    }

                    // This fails because it runs in a new container, and the node_modules created during the first installation are gone at this point
                    // How do I reuse the same container created in the install dep step?
                    steps {
                        sh 'npm run build'
                    }
                }

                stage('Test') {
                    agent {
                        docker { image 'node:10-alpine' }
                    }

                    steps {
                        sh 'npm run test'
                    }
                }
            }
        }


        // Later on, there is a deployment stage which MUST deploy using a specific node,
        // which is why "agent: none" is used in the first place

   }
}
like image 619
Avindra Goolcharan Avatar asked May 21 '18 20:05

Avindra Goolcharan


3 Answers

See reuseNode option for Jenkins Pipeline docker agent:
https://jenkins.io/doc/book/pipeline/syntax/#agent

pipeline {
  agent any

  stages {
    stage('NPM install') {
      agent {
        docker {
          /*
           * Reuse the workspace on the agent defined at top-level of
           * Pipeline, but run inside a container.
           */
          reuseNode true
          image 'node:12.16.1'
        }
      }

      environment {
        /*
         * Change HOME, because default is usually root dir, and
         * Jenkins user may not have write permissions in that dir.
         */
        HOME = "${WORKSPACE}"
      }

      steps {
        sh 'env | sort'
        sh 'npm install'
      }
    }
  } 
}
like image 70
Oleksandr Shmyrko Avatar answered Oct 23 '22 09:10

Oleksandr Shmyrko


For Declarative pipeline, one solution can be to use Dockerfile in the root of the project. For e.g.

Dockerfile

FROM node:10-alpine
// Further Instructions

Jenkinsfile

pipeline{

    agent {
        dockerfile true
    }
    stage('Build') {
        steps{
            sh 'npm run build'
        }
    }
     stage('Test') {
        steps{
            sh 'npm run test'
        }
    }
}
like image 44
Ankit Pandoh Avatar answered Oct 23 '22 11:10

Ankit Pandoh


You can use scripted pipelines, where you can put multiple stage steps inside a docker step, e.g.

node {
  checkout scm
  docker.image('node:10-alpine').inside {
    stage('Build') {
       sh 'npm run build'
     }
     stage('Test') {
       sh 'npm run test'
     }
  }
}

(code untested)

like image 8
StephenKing Avatar answered Oct 23 '22 11:10

StephenKing