docker中的jenkinspipe道中npm安装失败

我正在关注一个关于Jenkinspipe道的教程,我可以在节点6.10的docker容器中获得一个“hello world”。

但是,当我将一个默认的EmberJS应用程序(使用ember init )添加到repo并尝试在pipe道中构build它时,运行npm install(由于目录访问问题)时失败。 Jenkinsfile可以在这里看到: https : //github.com/CloudTrap/pipeline-tutorial/blob/fix-build/Jenkinsfile

由构build打印的错误信息是(这是安装本地和运行使用java -jar jenkins.war在Macbook上,不相关,但包括以防万一)是:

 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. 

注意:我不想以root / sudo身份运行npm install

更新:我已经能够取得一些进展如下:

我发现了Jenkins用来从日志中使用容器来构build的命令:

 [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 

所以当Docker镜像运行时,工作目录是/long-workspace-directory (这真是一个神秘的jenkins工作区path),用户ID是501(组ID 20)等等。用户没有名字(这显然是打破了与这个问题无关的其他事情)。

  1. 更改代理使用Dockefile:

    agent {dockerfile {filename'Dockerfile'args'-v /.cache/ -v /.bower/ -v /.config/configstore/'}}

  2. 指定args '-v ...'来创buildnpm install / bower需要的目录的卷。

https://github.com/jenkins-infra/jenkins.io/blob/master/Jenkinsfile

 docker.image('openjdk:8').inside { /* One Weird Trick(tm) to allow git(1) to clone inside of a * container */ withEnv([ /* Override the npm cache directory to avoid: EACCES: permission denied, mkdir '/.npm' */ 'npm_config_cache=npm-cache', /* set home to our current directory because other bower * nonsense breaks with HOME=/, eg: * EACCES: permission denied, mkdir '/.config' */ 'HOME=.', ]) { // your code } } 

在这个问题上浪费了一整天的时间,我发现只需在代理阶段使用Pipeline Editor添加以下内容作为环境variables就可以解决问题。

 'npm_config_cache=npm-cache' 

我添加相同的问题。 我解决了它使用root用户运行Docker镜像:

 node { stage("Prepare environment") { checkout scm // Build the Docker image from the Dockerfile located at the root of the project docker.build("${JOB_NAME}") } stage("Install dependencies") { // Run the container as `root` user // Note: you can run any official Docker image here withDockerContainer(args: "-u root", image: "${JOB_NAME}") { sh "npm install" } } } 

您可以在构build之前,在具有NVM_DIR的本地目录中安装NVM_DIR而不将其设置为全局依赖关系:

 mkdir -p node_dir export NVM_DIR=$(pwd)/node_dir curl https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash source $(pwd)/node_dir/nvm.sh nvm install 7 nvm use 7 

新的地点是:

 $ which node ~/someDir/node_dir/versions/node/v7.7.2/bin/node $ which npm ~/someDir/node_dir/versions/node/v7.7.2/bin/npm