我正在关注有关Jenkins管道的教程,并且可以在节点6.10 docker容器下运行一个“hello world”。
但是,当我向仓库中添加默认的EmberJS应用程序(使用ember init
)并尝试在管道中进行构建时,在运行npm install时会失败(由于目录访问问题)。 Jenkinsfile可以在这里看到:https://github.com/CloudTrap/pipeline-tutorial/blob/fix-build/Jenkinsfile
该版本显示的错误消息是(它是在本地安装并在Macbook上使用java -jar jenkins.war
运行的,不相关,但以防万一)是:
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使用容器构建的命令:
[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工作区路径),并且用户ID是501(组ID 20),等等。用户没有名称(显然是不合法的)其他与此问题无关的事物)。agent {
dockerfile {
filename 'Dockerfile'
args '-v /.cache/ -v /.bower/ -v /.config/configstore/'
}
}
args '-v ...'
。 最佳答案
添加环境并将“主页”设置为“。”。解决如下。
pipeline {
agent { docker { image 'node:8.12.0' } }
environment {
HOME = '.'
}
stages {
stage('Clone') {
steps {
git branch: 'master',
credentialsId: '121231k3jkj2kjkjk',
url: 'https://myserver.com/my-repo.git'
}
}
stage('Build') {
steps {
sh "npm install"
}
}
}
}