首页 > 解决方案 > 詹金斯管道在容器内构建

问题描述

我正在尝试在 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 容器中运行构建过程,而不是在构建机器上运行,这样我就不必在构建机器上安装包,它将来自容器映像。

我在这里想念什么?

标签: jenkinsjenkins-pipelinejenkins-build-flow

解决方案


目前我无权访问我的服务器,因此无法对其进行测试,但是您是否尝试过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"
            }
        }
    }
}

推荐阅读