代码之家  ›  专栏  ›  技术社区  ›  Angelina

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

  •  0
  • Angelina  · 技术社区  · 5 年前

    我想在 Install 进入阶段 Jenkins 跑 npm install 在docker映像上,并将其推送到 Artifactory .

    然后在另一个阶段,使用它来运行我对它和其他一些东西的测试。

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

    这是我的代码:

    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 回复  |  直到 5 年前