意图
我尝试构建一个非常简单的基于最新的node
Docker镜像的声明式Jenkinsfile
。我想通过在Jenkinsfile
中调用sh 'npm install ...'
来安装Node.js
应用程序的依赖项。从Docker容器中使用npm
进行安装在Jenkins之外表现良好,但在使用Jenkins Pipeline时出现问题。
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
}
}
}
}
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容器启动的用户有关:
我尝试过的方法
args '-u root'
in the Docker code block in the Jenkinsfile. 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. 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 theDockerfile
. This works, but out of curiosity I want to know why I cannot invoke this directly in Jenkinsfile instead.FROM node:latest RUN npm i axios