【发布时间】:2020-09-28 14:52:20
【问题描述】:
意图
我正在尝试基于最新的node docker 映像构建一个非常简单的声明式Jenkinsfile。我想通过在Jenkinsfile 中调用sh 'npm install ...' 来安装Node.js 应用程序的依赖项。在没有 Jenkins 的情况下从 Docker 容器中使用 npm 安装就像一个魅力,但在使用 Jenkins Pipeline 时则不然。
Jenkins 文件
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'在 Jenkinsfile 的 Docker 代码块中。这可行,但我怀疑这应该如何解决。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。这可行,但出于好奇,我想知道为什么我不能直接在 Jenkinsfile 中调用它。FROM node:latest RUN npm i axios
【问题讨论】:
-
这篇文章解决了我的问题:stackoverflow.com/questions/42743201/…
标签: node.js docker jenkins npm npm-install