在安装阶段构建 docker 镜像并在其他几个构建阶段并行使用它

时间:2021-01-21 23:59:08

标签: dockerfile jenkins-pipeline jenkins-declarative-pipeline

我想在 InstallJenkins 阶段,在 docker 映像上运行 npm install 并将其推送到 Artifactory

然后在另一个阶段使用它来运行我的测试和其他一些事情。

目前无法找到依赖项。当我从本地 Artifactory 拉取图像并检查内容时,所有模块似乎都在那里,并且运行 npm run test:unit 可以按预期工作。但由于某种原因,它在 Jenkins 中不起作用。

这是我的代码:

Dockerfile

FROM node:12

COPY .npmrc .npmrc
COPY package.json ./

RUN npm install
RUN rm -f .npmrc

COPY . .

詹金斯档案

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

完成这项工作后,我想并行运行多个阶段并为它们重复使用相同的图像:

詹金斯档案

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

失败:

...
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

> autodeskwdcartmfe@2.145.0 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! cartmfe@2.145.0 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 cartmfe@2.145.0 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?

我也尝试添加:

Dockerfile

WORKDIR /app

詹金斯档案

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

但这也无济于事。

0 个答案:

没有答案
相关问题