1

我正在尝试在 docker 容器中运行构建步骤。这是我的Jenkinsfile

    pipeline {
        agent { label 'slave1' }
        stages {
            stage ('Build') {
                agent {
                    docker {image 'node:8'}
                }
                steps {
                    sh "npm install"
                }
            }
        }
        post {
            failure {
                script {
                    echo "TestRail failed"
                }
            }
        }
    }

但是步骤失败并出现以下错误

    [Frontend@2] Running shell script
    + npm install
    npm WARN mycloud@1.0.0 No repository field.
    npm WARN mycloud@1.0.0 No license field.

    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!  { Error: EACCES: permission denied, mkdir '/.npm'
    npm ERR!   stack: 'Error: EACCES: permission denied, mkdir \'/.npm\'',
    npm ERR!   errno: -13,
    npm ERR!   code: 'EACCES',
    npm ERR!   syscall: 'mkdir',
    npm ERR!   path: '/.npm' }
    npm ERR! 
    npm ERR! The operation was rejected by your operating system.
    npm ERR! It is likely you do not have the permissions to access this file as the current user
    npm ERR! 
    npm ERR! If you believe this might be a permissions issue, please double-check the
    npm ERR! permissions of the file and its containing directories, or try running
    npm ERR! the command again as root/Administrator (though this is not recommended).
    [Pipeline] }
    $ docker stop --time=1 56e0023a9538d890a72a07bc3e57aa99b6c92d0adfc99f8e70117dd143e3d22b
    $ docker rm -f 56e0023a9538d890a72a07bc3e57aa99b6c92d0adfc99f8e70117dd143e3d22b

当我手动运行 docker 容器然后执行时,npm install一切都按预期工作。

如果以 root 身份运行容器,-u 0:0npm install通过

            agent {
                docker {
                    image 'node:8'
                    args '-u 0:0'
                }
            }

但詹金斯工作区清理失败:

        ERROR: Error fetching remote repo 'origin'
    hudson.plugins.git.GitException: Failed to fetch from https://github.com/mycompany/Frontend.git
        at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:888)
        at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1155)
        at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1186)
        at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:120)
        at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:90)
        at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:77)
        at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution$1$1.call(SynchronousNonBlockingStepExecution.java:50)
        at hudson.security.ACL.impersonate(ACL.java:290)
        at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution$1.run(SynchronousNonBlockingStepExecution.java:47)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
    Caused by: hudson.plugins.git.GitException: Command "git clean -fdx" returned status code 1:
    stdout: 
    stderr: warning: failed to remove node_modules/grunt-contrib-copy/README.md: Permission denied
    warning: failed to remove node_modules/grunt-contrib-copy/package.json: Permission denied
    warning: failed to remove node_modules/grunt-contrib-copy/tasks/copy.js: Permission denied
    warning: failed to remove node_modules/grunt-contrib-copy/LICENSE-MIT: Permission denied
    warning: failed to remove node_modules/meow/readme.md: Permission denied

在这里,我尝试在 docker 容器中运行构建过程,而不是在构建机器上运行,这样我就不必在构建机器上安装包,它将来自容器映像。

我在这里想念什么?

4

2 回答 2

1

目前我无权访问我的服务器,因此无法对其进行测试,但是您是否尝试过skipDefaultCheckout然后在 docker 阶段从存储库中签出?

pipeline {
    agent { label 'slave1' }

    options {
        skipDefaultCheckout true
    }

    stages {
        stage ('Build') {
            agent {
                docker {image 'node:8'}
            }
            steps {
                checkout scm
                sh "npm install"
            }
        }
    }
    post {
        failure {
            script {
                echo "TestRail failed"
            }
        }
    }
}
于 2018-11-01T00:55:53.513 回答
0

使用 cloudbees 的 docker step 而不是将其用作代理。这样做,您将在 master 上签出并将工作区作为卷装入 docker(由 docker 插件隐式完成)。

您的 Jenkins 用户的用户 ID 是什么?据我记得,如果您的 jenkins 用户的 ID 为 1000,那么您无需覆盖该用户,因为默认节点 docker 映像的用户 ID 为 1000,并且不应该存在任何文件权限问题(用于删除文件)。

请原谅我的机器上没有语法错误

def image = docker.image(‘node:latest’)
image.pull()
image.inside(dockerOptions) {
   sh “npm”
   // my other logic
  }
于 2018-11-02T15:35:53.417 回答