使用声明性Jenkins Pipeline在Docker中运行命令

时间:2019-05-23 06:21:12

标签: docker jenkins npm jenkins-pipeline

我的Jenkins在Docker中运行。我有一个只有npm install的简单Dockerfile。

我使用声明性Jenkins管道,因此它可以构建并运行容器,但是当我运行npm run test时,Jenkins会抛出错误,表明Jest不存在,并且

npm WARN Local package.json exists, but node_modules missing, did you mean to install?

尽管应该在映像构建node_modules期间安装

Step 4/5 : RUN npm install --unsafe-perm ---> Using cache

Dockerfile

WORKDIR /app
COPY package*.json /app/
RUN npm install --unsafe-perm
COPY ./ /app/

Jenkinsfile

pipeline {
    agent {
        dockerfile {
            filename 'docker/Dockerfile.test'
            dir '.'
        }
    }
    stages {
        stage ('Test') {
            steps {
                sh 'npm run test'
            }
        }
    }
}

Jenkins控制台输出

+ docker build -t b4508423f0c768b0e72e947a272772d60a2bb495 -f ./docker/Dockerfile.test .
Sending build context to Docker daemon  5.959MB

Step 1/5 : FROM node:8-alpine
 ---> 75a2ffce2f88
Step 2/5 : WORKDIR /app
 ---> Using cache
 ---> 8fe07f9e374f
Step 3/5 : COPY package*.json /app/
 ---> Using cache
 ---> 118fc7d484b4
Step 4/5 : RUN npm install --unsafe-perm
 ---> Using cache
 ---> 0bd115e6b70a
Step 5/5 : COPY ./ /app/
 ---> Using cache
 ---> 58a55b1c72ff
Successfully built 58a55b1c72ff
Successfully tagged b4508423f0c768b0e72e947a272772d60a2bb495:latest
[Pipeline] dockerFingerprintFrom
[Pipeline] }
[Pipeline] // stage
[Pipeline] sh
+ docker inspect -f . b4508423f0c768b0e72e947a272772d60a2bb495
.
[Pipeline] withDockerContainer
Jenkins seems to be running inside container 95d484a9ea8a1a1377e19ac13a5303935bf3530c466b6dd88dba48798c98b00a
$ docker run -t -d -u 0:0 -w /var/jenkins_home/workspace/customers_survey_front --volumes-from 95d484a9ea8a1a1377e19ac13a5303935bf3530c466b6dd88dba48798c98b00a -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** b4508423f0c768b0e72e947a272772d60a2bb495 cat
$ docker top 7b36043b2385a5e5a65de07284e14002bf5b59ec56724f575202fa9193d0ecf9 -eo pid,comm
[Pipeline] {
[Pipeline] timeout
Timeout set to expire in 15 min
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] sh
+ npm run test

> customers_survey@1.0.0 test /var/jenkins_home/workspace/customers_survey_front
> jest

sh: jest: not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! customers_survey@1.0.0 test: `jest`
npm ERR! spawn ENOENT
npm ERR! 
npm ERR! Failed at the customers_survey@1.0.0 test 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?

1 个答案:

答案 0 :(得分:0)

正如其他人所述,here尝试在npm init之前使用npm install。 npm init会动态创建package.json。