繁体   English   中英

如何使用Jenkins Docker插件在容器内构建maven项目

[英]How to use Jenkins Docker plugin to build a maven project inside a container

使用maven作为构建工具的项目。 现在,当使用Jenkins进行部署时,我们需要使用Docker插件在docker容器内构建项目。 我的理解是项目应该在容器内部构建,一旦完成它应该被删除。

我正在尝试使用类似的命令:docker.image(“imageName”)。inside {}现在我们如何确保删除容器并装入卷,以便在docker之后可以访问作为构建的一部分创建的jar容器删除?

有些人可以提供上述理解的输入和上述命令的示例或任何指向的链接吗?

我想,如果你将使用管道工作将会很好。 在这里,您可以通过评论查看我的示例

pipeline {
stages {
    stage('Build') {
        agent { //here we select only docker build agents
            docker {
                image 'maven:latest' //container will start from this image
                args '-v /root/.m2:/root/.m2' //here you can map local maven repo, this let you to reuse local artifacts
            }
        }
        steps {
            sh 'mvn -B -DskipTests clean package' //this command will be executed inside maven container
        }
    }
    stage('Test') { //on this stage New container will be created, but current pipeline workspace will be remounted to it automatically
        agent {
            docker {
                image 'maven:latest'
                args '-v /root/.m2:/root/.m2'
            }
        }
        steps {
            sh 'mvn test' 
        }
    }
    stage ('Build docker image') { //here you can check how you can build even docker images inside container
        agent {
            docker {
                image 'maven:latest'
                args '-v /root/.m2:/root/.m2 -v /var/run/docker.sock:/var/run/docker.sock' //here we expose docker socket to container. Now we can build docker images in the same way as on host machine where docker daemon is installed
            }
        }
        steps {
            sh 'mvn -Ddocker.skip=false -Ddocker.host=unix:///var/run/docker.sock docker:build' //example of how to build docker image with pom.xml and fabric8 plugin
        }
    }
}

}

即使Jenkins本身在一个带有来自主机的贴片机jenkins_home的容器中运行,这也会有效。

如果我能从我的经验中为您提供更多有用的详细信息,请告知我们

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM