简体   繁体   中英

Run command in Docker with declarative Jenkins Pipeline

My Jenkins is running in Docker. I have simple Dockerfile that has only npm install .

I use declarative Jenkins pipeline so it builds and runs container but when I run npm run test Jenkins throws error that Jest doesn't exist and

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

though during image build node_modules should have been installed

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 console output

+ 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?

As others mentioned here try to use npm init before npm install . npm init will create package.json on fly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM