詹金斯管道建立在容器内

时间:2018-10-31 19:08:23

标签: jenkins jenkins-pipeline jenkins-build-flow

我正在尝试在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时,一切都会按预期进行。

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

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

但是jenkins工作空间清理失败,显示为:

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

我在这里想念什么?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

使用cloudbees中的docker step代替将其用作代理。这样做,您将在master上签出并将工作区作为卷挂载到docker(由docker插件隐式完成)。

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

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

def image = docker.image(‘node:latest’)
image.pull()
image.inside(dockerOptions) {
   sh “npm”
   // my other logic
  }