Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

dockerfile - 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'
               }
           }
        }
    }

Failure:

...
01ddae7f607e: Pull complete
Digest: sha256:dfb4b264f627da5cbd0fe25f180be0fd9b7a46e2678cff471f24cfc4f1f1a2e2
Status: Downloaded newer image for my-artifactory.com/reponame/mymfetestcontainer:latest
my-artifactory.com/reponame/mymfetestcontainer:latest
[Pipeline] withDockerContainer
JenkinsBuildAgent-CentOS-004 does not seem to be running inside a container
$ docker run -t -d -u 1001:1001 -w /mnt/data/jenkins/workspace/ivery_cartmfe_test-107 -v /mnt/data/jenkins/workspace/ivery_cartmfe_test-107:/mnt/data/jenkins/workspace/ivery_cartmfe_test-107:rw,z -v /mnt/data/jenkins/workspace/ivery_cartmfe_test-107@tmp:/mnt/data/jenkins/workspace/ivery_cartmfe_test-107@tmp:rw,z -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e  my-artifactory.com/reponame/mymfetestcontainer:latest cat
$ docker top 1b2a93e54589165a6cdc2cdac1e18dbb4f388764504996a7c6732588e84b2238 -eo pid,comm
[Pipeline] {
[Pipeline] sh
+ npm run test:unit
[Pipeline] sh

> [email protected] test:unit /mnt/data/jenkins/workspace/ivery_cartmfe_test-107
> TEST_MODE=unit jest --runInBand --coverage --coverageDirectory=coverage/jest --config=jest.unit.config.js

sh: 1: jest: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! [email protected] test:unit: `TEST_MODE=unit jest --runInBand --coverage --coverageDirectory=coverage/jest --config=jest.unit.config.js`
npm ERR! spawn ENOENT
npm ERR! 
npm ERR! Failed at the [email protected] test:unit script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

I also tried adding:

Dockerfile

WORKDIR /app

Jenkinsfile

args -v ${PWD}:/app -w /app

but that didn't help either.

question from:https://stackoverflow.com/questions/65837507/building-docker-image-in-install-stage-and-use-it-in-several-other-build-stages

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...