[英]Jenkins: "sh npm i ..." not working in docker agent
I am trying to build a very simple declarative Jenkinsfile
that is based on the latest node
docker image.我正在尝试构建一个非常简单的声明性
Jenkinsfile
,它基于最新的node
docker 映像。 I want to install the dependencies for the Node.js
app by calling sh 'npm install...'
in the Jenkinsfile
.我想通过在 Jenkinsfile 中调用
sh 'npm install...'
来安装Node.js
应用程序的依赖Jenkinsfile
。 Installing with npm
from the Docker container without Jenkins works like a charm, but not when using Jenkins Pipeline.在没有 Jenkins 的情况下,从 Docker 容器中使用
npm
安装就像一个魅力,但在使用 Z2E54334C0A3ABADA2E3E5A54 时不会。
Jenkinsfile詹金斯文件
pipeline {
agent {
docker {
image 'node:latest'
}
}
stages {
stage('Install Dependencies') {
steps {
sh 'npm -v' // sanity check
sh 'ls -lart' // debugging filesystem
sh 'npm i axios' // this leads to the error
}
}
}
}
Console Log in Jenkins控制台登录 Jenkins
+ npm install axios
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /.npm
npm ERR! errno -13
npm ERR!
npm ERR! Your cache folder contains root-owned files, due to a bug in
npm ERR! previous versions of npm which has since been addressed.
npm ERR!
npm ERR! To permanently fix this problem, please run:
npm ERR! sudo chown -R 1962192188:58041779 "/.npm"
I assume it has to do something with the privileges in the mounted volume from Jenkins and/or the user with which the Docker container ist started from:我认为它必须对来自 Jenkins 和/或 Docker 容器从以下位置开始的用户的已安装卷中的权限做一些事情:
args '-u root'
in the Docker code block in the Jenkinsfile. Jenkinsfile 中 Docker 代码块中的
args '-u root'
。 This works but I doubt this is how this should be solved.这可行,但我怀疑这应该如何解决。
docker { image 'node:latest' args '-u root' }
sudo chown -R 1962192188:58041779 "/.npm"
as proposed in the error message. sudo chown -R 1962192188:58041779 "/.npm"
如错误消息中所建议。 But this leads to:但这会导致:
+ sudo chown -R 1962192188:58041779 /.npm /Users/<user>/.jenkins/workspace/pipe@tmp/durable-664f481d/script.sh: 1: /Users/<user>/.jenkins/workspace/pipe@tmp/durable-664f481d/script.sh: sudo: not found
Define a layer RUN npm install axios
in the Dockerfile
.在 Dockerfile 中定义一个层
RUN npm install axios
Dockerfile
。 This works, but out of curiosity I want to know why I cannot invoke this directly in Jenkinsfile instead.这可行,但出于好奇,我想知道为什么我不能直接在 Jenkinsfile 中调用它。
FROM node:latest RUN npm i axios
The best way to solve this issue is by using one of the following methods (inspired by npm install fails in jenkins pipeline in docker ).解决此问题的最佳方法是使用以下方法之一(受npm install failed in jenkins pipeline in docker 启发)。 All three will end up changing the default directory of
.npm
(ie, npm's cache) to the current working directory (which is the Jenkins Job's workspace mapped to the Docker container).这三个最终都会将
.npm
(即 npm 的缓存)的默认目录更改为当前工作目录(即 Jenkins 作业的工作空间映射到 Docker 容器)。
Declarative Pipeline声明式管道
pipeline {
agent { docker { image 'node:latest'' } }
environment {
HOME = '.'
}
...
Scripted Pipeline脚本流水线
docker.image('node:latest').inside {
withEnv([
'HOME=.',
])
...
--cache
to change the location of the .npm
folder when calling npm install
npm install
时,使用附加参数--cache
更改.npm
文件夹的位置npm install --cache npm_cache <optional:packagename>
npm_config_cache
used by npm before calling npm install
npm_config_cache
使用的环境变量npm install
Declarative Pipeline声明式管道
pipeline {
agent { docker { image 'node:latest'' } }
environment {
npm_config_cache = 'npm-cache'
}
...
Scripted Pipeline脚本流水线
docker.image('node:latest').inside {
withEnv([
'npm_config_cache=npm-cache',
])
...
This worked for me after trying so many things.在尝试了很多事情之后,这对我有用。 It seems Jenkins will map the workspace directory to the docker container.
似乎 Jenkins 将 map 工作区目录到 docker 容器。
pipeline {
agent { dockerfile true }
environment {
HOME = "${env.WORKSPACE}"
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.