如何使用 Jenkins 管道在 docker 容器中挂载 Jenkins 工作区 [英] How to mount Jenkins workspace in docker container using Jenkins pipeline

查看:49
本文介绍了如何使用 Jenkins 管道在 docker 容器中挂载 Jenkins 工作区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 docker 中使用 Jenkins./var/jenkins_home 安装在我主机上的 /var/jenkins-data 上.我的 Jenkins 可以执行 docker 命令(安装套接字)并且我已经安装了 git 插件和管道插件.

I'm using Jenkins in docker. The /var/jenkins_home is mounted on /var/jenkins-data on my host. My Jenkins can execute docker commands (mount of sockets) and I've installed the git plugin and pipeline plugin.

现在我有一个名为 test 的管道作业和以下管道:

Now I have a pipeline job named test and the following pipeline:

pipeline {
    agent any
    stages {
        stage('Clone') {
            steps {
                git branch: 'master', url: 'https://github.com/lvthillo/maven-hello-world.git'
            }
        }

        stage('Build in Docker') {
            agent {
                docker {
                    image 'maven:3.5.2'
                    args '-v /var/jenkins_home/workspace/test:/opt/maven -w /opt/maven'
                }
            }

            steps {
                sh 'pwd'
                sh 'mvn -v'
                sh 'mvn clean install'
            }
        }
    }
}

我想要实现的是从 github 克隆我的公共存储库.这有效.在下一步中,我想启动一个 docker 容器 (maven) 并打印当前目录、maven 版本并执行全新安装.

What I want to achieve is cloning my public repo from github. This works. In the next step I want to start a docker container (maven) and print the current directory, the maven version and perform a clean install.

三个命令的输出是:

[test@2] Running shell script
+ pwd
/var/jenkins_home/workspace/test@2
[Pipeline] sh
[test@2] Running shell script
+ mvn -v
Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T07:58:13Z)
Maven home: /usr/share/maven
Java version: 1.8.0_151, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "4.4.86-boot2docker", arch: "amd64", family: "unix"
[Pipeline] sh
[test@2] Running shell script
+ mvn clean install

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.179 s
[INFO] Finished at: 2018-01-12T12:12:00Z
[INFO] Final Memory: 5M/31M
[INFO] ------------------------------------------------------------------------
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (/var/jenkins_home/workspace/test@2). Please verify you invoked Maven from the correct directory. -> [Help 1]

它似乎可以工作,因为我的主机上没有安装 maven,所以它是从容器内部执行的,但它突然创建了一个新的工作区 (@2) 而不是从哪里使用现有的工作区我克隆了回购.我不想立即克隆容器中的 repo,因为我想要多个阶段,所有阶段都使用不同的容器,但都在我的工作区中的 git repo 上执行.

It seems to work because maven is not installed on my host, so it's executed from inside the container, but it's suddenly creating a new workspace (@2) instead of using the existing one from where I cloned the repo. I don't want to clone the repo in my container immediately because I want multiple stages, all with different containers, but all executed on my git repo in my workspace.

我做错了什么或者我该如何解决?我在想这可能是因为代理步骤.我的第一步可以在任何代理(任何从站)上运行,docker 步骤将在 docker 容器中运行,但当然必须在执行 git clone 的同一个从站上运行.

What am I doing wrong or how can I fix this? I was thinking it was maybe because of the agent step. my first step can run on any agent (any slave), the docker step will run in the docker container, but must of course run on that same slave als where the git clone was executed.

推荐答案

pipeline {
agent any
stages {
    stage('Clone') {
        steps {
            git branch: 'master', url: 'https://github.com/lvthillo/maven-hello-world.git'
            stash name:'scm', includes:'*'
        }
    }

    stage('Build in Docker') {
        steps {
            unstash 'scm'
            script{
                docker.image('maven:3.5.2').inside{ 
                    sh 'pwd'
                    sh 'mvn -v'
                    sh 'mvn clean install'
                }
            }
        }
    }
}
}

即使是多节点设置,您也可以使用此管道.Docker 插件也将您的工作区挂载为 docker 工作区.因此,除非它们在工作区之外,否则没有必要挂载任何卷.

You can use this pipeline even with a multi-node setup. Docker plugin mounts your workspace as a docker workspace too.Hence, it is not necessary to mount any volume unless they are outside the workspace.

这篇关于如何使用 Jenkins 管道在 docker 容器中挂载 Jenkins 工作区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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