简体   繁体   中英

npm install fails in Jenkins pipeline

I've created a docker images to be able to run node >= 7.9.0 and monogodb for testing in Jenkins. Some might argue that testing with mongodb is not correct approach but the app uses it extensively and I have some complex updates and deletes so I need it there.

Docker file is under dockerfiles/test/Dockerfile in my github repo. When using the pipeline syntax the docker images is built successfully but I can't do sh 'npm install' or sh 'npm -v' in the steps of the pipeline. The docker images is tested and if I build it locally and run it I can do the npm install there. sh 'node -v' runs successfully in the pipeline and also sh 'ls'.

Here is the pipeline syntax.

pipeline {
  agent { dockerfile { dir 'dockerfiles/test' } }
  stages {
    stage('Build') {
        steps {
           sh 'npm install'
        }
    }
  }
  post {
      always {
        echo 'I will always say Hello again!'
      }
  }
}

I get this error: ERROR: script returned exit code -1. I can't see anything wrong here. I've also tested with other node images with the same result. If I run it with a node slave I can do the installation but I do not want to have many different slaves with a lot of setups for integration tests.

And here is the dockerfile

FROM ubuntu:16.04

ENV DEBIAN_FRONTEND noninteractive

RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
RUN echo "deb http://repo.mongodb.org/apt/ubuntu $(cat /etc/lsb-release | grep DISTRIB_CODENAME | cut -d= -f2)/mongodb-org/3.2 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-3.2.list

RUN apt-get update && apt-get install -y \
curl && \
curl -sL https://deb.nodesource.com/setup_7.x | bash - && \
apt-get install -y nodejs && \
apt-get install -y mongodb-org


RUN mkdir -p /data/db
RUN export LC_ALL=C
RUN groupadd -g 1000 jenkins && useradd -u 1000 jenkins -g jenkins

EXPOSE 27017

CMD ["/usr/bin/mongod"]

Found a workaround to a similar problem.

Problem

  • Jenkins running a pipeline job
  • This job is running commands inside a debian slim container
  • All commands are failing instantly with no error output, only a ERROR: script returned exit code -1
  • Running the container outside docker and executing the same commands with the same user is working as it should be

Extract from Jenkinfile :

androidImage = docker.build("android")
androidImage.inside('-u root') {
    stage('Install'){
        sh 'npm install' // is failing with generic error and no output
}

Solution

Found the answer on Jenkins bugtracker : https://issues.jenkins-ci.org/browse/JENKINS-35370 and on Jenkins Docker Pipeline Exit Code -1

My problem was solved by installing the procps package in my debian Dockerfile :

apt-get install -y procps

I replicated your setup as faithfully as I could. I used your Dockerfile and Jenkinsfile, and here's my package.json:

{
  "name": "minimal",
  "description": "Minimal package.json",
  "version": "0.0.1",
  "devDependencies": {
    "mocha": "*"
  }
}

It failed like this for me during npm install:

npm ERR! Error: EACCES: permission denied, mkdir '/home/jenkins'

I updated one line in your Dockerfile to add --create-home :

RUN groupadd -g 1000 jenkins && useradd -u 1000 jenkins -g jenkins --create-home

And the build passed. Kudos to @mkobit for keying in on the issue and linking to the jenkins issue that will make this cleaner in the future.

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