npm install 在 docker 中的 jenkins 管道中失败 [英] npm install fails in jenkins pipeline in docker

查看:36
本文介绍了npm install 在 docker 中的 jenkins 管道中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习有关 Jenkins 管道的教程,我可以在节点 6.10 docker 容器下获得一个hello world".

I'm following a tutorial about Jenkins pipeline and I can get a "hello world" working under at node 6.10 docker container.

但是,当我将默认 EmberJS 应用程序(使用 ember init)添加到存储库并尝试在管道中构建它时,它在运行 npm install 时失败(由于目录访问问题).Jenkinsfile 可以在这里看到:https://github.com/CloudTrap/管道教程/blob/fix-build/Jenkinsfile

But, when I added a default EmberJS app (using ember init) to the repo and attempt to build that in the pipeline, it fails when running npm install (because of directory access issues). The Jenkinsfile can be seen here: https://github.com/CloudTrap/pipeline-tutorial/blob/fix-build/Jenkinsfile

构建打印的错误消息是(本地安装并在 Macbook 上使用 java -jar jenkins.war 运行,不相关但包含以防万一)是:

The error message printed by the build is (which is installed locally and run using java -jar jenkins.war on a Macbook, not relevant but included just in case) is:

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.

Note: I would like to not run npm install as root / sudo.

更新:我已经取得了如下进展:

UPDATE: I have been able to make some progress as follows:

我从日志中找到了 Jenkins 用来使用容器构建的命令:

I found the command that Jenkins uses to build using the container from the logs:

[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),等等.用户没有名字(这显然破坏了与此问题无关的其他事情).

So when the docker image runs, it's work directory is a /long-workspace-directory (it's really a cryptic looking jenkins workspace path) and the user id is 501 (group id 20), etc. The user doesn't have a name (which is apparently breaking other things not related to this question).

  1. 将代理更改为使用 Dockefile:

  1. Changed agent to use a Dockefile:

agent {
  dockerfile {
    filename 'Dockerfile'
    args '-v /.cache/ -v /.bower/  -v /.config/configstore/'
  }
}

  • 指定 args '-v ...' 用于为 npm install/bower 需要的目录创建卷.

  • Specify args '-v ...' for creating volumes for the directories npm install / bower needs.

    推荐答案

    添加环境并将 Home 设置为."解决这个问题如下.

    Adding the environments and setting the Home to '.' solves this as below.

    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"
                }
            }
        }
    }
    

    这篇关于npm install 在 docker 中的 jenkins 管道中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆