簡體   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