[英]Jenkins: "sh npm i ..." not working in docker agent
我正在尝试构建一个非常简单的声明性Jenkinsfile
,它基于最新的node
docker 映像。 我想通过在 Jenkinsfile 中调用sh 'npm install...'
来安装Node.js
应用程序的依赖Jenkinsfile
。 在没有 Jenkins 的情况下,从 Docker 容器中使用npm
安装就像一个魅力,但在使用 Z2E54334C0A3ABADA2E3E5A54 时不会。
詹金斯文件
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
}
}
}
}
控制台登录 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"
我认为它必须对来自 Jenkins 和/或 Docker 容器从以下位置开始的用户的已安装卷中的权限做一些事情:
Jenkinsfile 中 Docker 代码块中的args '-u root'
。 这可行,但我怀疑这应该如何解决。
docker { image 'node:latest' args '-u root' }
sudo chown -R 1962192188:58041779 "/.npm"
如错误消息中所建议。 但这会导致:
+ 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
在 Dockerfile 中定义一个层RUN npm install axios
Dockerfile
。 这可行,但出于好奇,我想知道为什么我不能直接在 Jenkinsfile 中调用它。
FROM node:latest RUN npm i axios
解决此问题的最佳方法是使用以下方法之一(受npm install failed in jenkins pipeline in docker 启发)。 这三个最终都会将.npm
(即 npm 的缓存)的默认目录更改为当前工作目录(即 Jenkins 作业的工作空间映射到 Docker 容器)。
声明式管道
pipeline {
agent { docker { image 'node:latest'' } }
environment {
HOME = '.'
}
...
脚本流水线
docker.image('node:latest').inside {
withEnv([
'HOME=.',
])
...
npm install
时,使用附加参数--cache
更改.npm
文件夹的位置npm install --cache npm_cache <optional:packagename>
npm_config_cache
使用的环境变量npm install
声明式管道
pipeline {
agent { docker { image 'node:latest'' } }
environment {
npm_config_cache = 'npm-cache'
}
...
脚本流水线
docker.image('node:latest').inside {
withEnv([
'npm_config_cache=npm-cache',
])
...
在尝试了很多事情之后,这对我有用。 似乎 Jenkins 将 map 工作区目录到 docker 容器。
pipeline {
agent { dockerfile true }
environment {
HOME = "${env.WORKSPACE}"
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.