Building docker image in Install stage and use it in several other build stages in parallel

I would like to during Install stage in Jenkins, run npm install on a docker image and push it to Artifactory.

And then in another stage use it to run my tests against it and some other things.

Currently it is failing to find dependencies. When I pull the image from Artifactory locally and check the contents, all the modules seem to be there, and running npm run test:unit works as expected. But for some reason it doesn’t work in Jenkins.

Here is my code:

Dockerfile

FROM node:12

COPY .npmrc .npmrc
COPY package.json ./

RUN npm install
RUN rm -f .npmrc

COPY . .

Jenkinsfile

String MY_TEST_DOCKER_IMAGE = 'my-artifactory.com/reponame/mymfetestcontainer:latest'
buildContainerVersion = "1.1"
imageName = "reponame/mymfetestcontainer"
...
stage('Install') {
   steps {
       script {
          docker.withRegistry('https://my-artifactory.com', 'svc_d_artifactory'){
              def customImage = docker.build("${imageName}:${buildContainerVersion}")
              customImage.push()
              customImage.push("latest")
              // delete image from local to cleanup your server space
              sh "docker rmi --force $(docker images -q ${customImage.id} | uniq)"
          }
       }
   }
}
stage('Unit') {
   agent {
       docker {
           image MY_TEST_DOCKER_IMAGE
           args '-u root'
       }
   }
   steps {
       sh 'npm run test:unit'
   }
}

Once I make this work, I want to run several stages in parallel and reuse same image for them:

Jenkinsfile

pipeline {
    agent {
        node {
            label 'centos'
        }
    }
    ...
    stage('Install') {...}
    stage('Tests') {
        when {
            not { triggeredBy 'TimerTrigger' }
            not { expression { isSkipCi } }
        }
    
        parallel {
           stage('Unit') {
               when {
                   beforeAgent true
                   not { expression { isReleaseDeployment } }
               }
               agent {
                   docker {
                       image MY_TEST_DOCKER_IMAGE
                       args '-u root'
                   }
               }
    
               steps {
                   sh 'npm run test:unit'
               }
    
               post {
                   success {
                       step([$class: 'CoberturaPublisher', coberturaReportFile: 'coverage/jest/cobertura-coverage.xml'])
                   }
               }
           }
    
           stage('Integration') {
               when {
                   beforeAgent true
                   not { expression { isReleaseDeployment } }
               }
               agent {
                   docker {
                       image MY_TEST_DOCKER_IMAGE
                       args '-u root'
                   }
               }
    
               steps {
                   sh 'npm run test:integration'
               }
           }
        }
    }

Source: Dockerfile Questions

LEAVE A COMMENT