Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm install fails in jenkins pipeline in docker

I'm following a tutorial about Jenkins pipeline and I can get a "hello world" working under at node 6.10 docker container.

But, when I added a default EmberJS app (using ember init) to the repo and attempt to build that in the pipeline, it fails when running npm install (because of directory access issues). The Jenkinsfile can be seen here: https://github.com/CloudTrap/pipeline-tutorial/blob/fix-build/Jenkinsfile

The error message printed by the build is (which is installed locally and run using java -jar jenkins.war on a Macbook, not relevant but included just in case) is:

npm ERR! Linux 4.9.12-moby npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" npm ERR! node v6.10.0 npm ERR! npm  v3.10.10 npm ERR! path /.npm npm ERR! code EACCES npm ERR! errno -13 npm ERR! syscall mkdir  npm ERR! Error: EACCES: permission denied, mkdir '/.npm' npm ERR!     at Error (native) npm ERR!  { Error: EACCES: permission denied, mkdir '/.npm' npm ERR!     at Error (native) npm ERR!   errno: -13, npm ERR!   code: 'EACCES', npm ERR!   syscall: 'mkdir', npm ERR!   path: '/.npm', npm ERR!   parent: 'pipeline-tutorial' } npm ERR!  npm ERR! Please try running this command again as root/Administrator. 

Note: I would like to not run npm install as root / sudo.

UPDATE: I have been able to make some progress as follows:

I found the command that Jenkins uses to build using the container from the logs:

[Pipeline] withDockerContainer $ docker run -t -d -u 501:20 -w /long-workspace-directory -v /long-workspace-directory:/long-workspace-directory:rw -v /long-workspace-directory@tmp:/long-workspace-directory@tmp:rw -e 

So when the docker image runs, it's work directory is a /long-workspace-directory (it's really a cryptic looking jenkins workspace path) and the user id is 501 (group id 20), etc. The user doesn't have a name (which is apparently breaking other things not related to this question).

  1. Changed agent to use a Dockefile:

    agent {   dockerfile {     filename 'Dockerfile'     args '-v /.cache/ -v /.bower/  -v /.config/configstore/'   } } 
  2. Specify args '-v ...' for creating volumes for the directories npm install / bower needs.

like image 894
les2 Avatar asked Mar 12 '17 02:03

les2


People also ask

Why npm install failed?

This cause of this error is that one of the dependencies you define in your package. json file fails to be installed properly on your computer. This means that npm fails to install the node-sass module that's added as a dependency to the n-app project.

How do I build and push Docker image in Jenkins pipeline?

Here is a quick and full example to build a docker image from private GitHub repo and push to docker hub with declarative pipeline in Jenkins. After that, allow Jenkins users to access the docker socket. Click Add Credentials on the left menu. Create another user and password credentials for Docker Hub login.


1 Answers

Adding the environments and setting the Home to '.' solves this as below.

pipeline {     agent { docker { image 'node:8.12.0' } }     environment {         HOME = '.'     }     stages {         stage('Clone') {             steps {                 git branch: 'master',                     credentialsId: '121231k3jkj2kjkjk',                     url: 'https://myserver.com/my-repo.git'             }         }         stage('Build') {             steps {                 sh "npm install"             }         }     } } 
like image 159
Dinuka De Silva Avatar answered Sep 22 '22 09:09

Dinuka De Silva