通过JENKINS+SSH制作SpringBoot Maven工程Docker镜像并远程部署

首先按照将jenkins配置到同一个目录安装

jenkins配置ssh凭据和git凭据和ssh服务、git仓库配置

然后在Jenkins服务器上

制作稳定运行又小巧的Docker基础镜像

再按照如下步骤

构建maven工程,然后通过shell执行
maven打springboot包->删镜像->Docker打包->Docker镜像打标签->Docker镜像上传Nexus
部署机器远程执行Shell
停容器->删容器->删镜像->拉镜像->起容器

pom.xml文件配置点:

复制jar包,复制运行的文件结构中配置信息和启动脚本
   
       
       
            docker-test
           
                docker-test
           

           
                false
           

           
               
               

           

       

   

           
                org.springframework.boot
                spring-boot-maven-plugin
               
                    cn.shary.boot.BootStrapSpringbootWebtest
                    JAR
               

               
                   
                       
                            repackage
                       

                   

               

           

           
                org.apache.maven.plugins
                maven-antrun-plugin
               
                   
                        package
                       
                            run
                       

                       
                           
                                                                    tofile='${project.build.directory}/springboot-webtest/lib/${project.artifactId}.jar'
                                    file='${project.build.directory}/${project.build.finalName}.jar' />
                           

                       

                   

               

           

           
                org.apache.maven.plugins
                maven-resources-plugin
               
                   
                        copy-bin
                        process-sources
                       
                            copy-resources
                       

                       
                            true
                            ${project.build.directory}/springboot-webtest/bin
                           
                               
                                    ${basedir}/bin-script
                                   
                                        **/*.sh
                                        **/*.bat
                                   

                               

                           

                       

                   

                   
                        copy-conf
                        process-sources
                       
                            copy-resources
                       

                       
                            true
                            ${project.build.directory}/springboot-webtest/conf
                           
                               
                                    ${project.build.directory}/classes
                                   
                                        **/*.xml
                                        **/*.yml
                                        **/*.properties
                                   

                               

                           

                       

                   

               

           

Dockfile直接拷贝生成好的目录生成镜像

FROM 192.168.1.34:808/baseimg/ubuntu-jre1.8 as build

ADD ./target/springboot-webtest
RUN mkdir -p /springboot-webtest/logs && chmod a+x /lspringboot-webtest/bin/*.sh

EXPOSE 7701
# top to block container exit
CMD /bin/bash /springboot-webtest/start.sh && top

 

新建自由风格的项目

源码管理:git

pre step   mvn clean

Build    mvn package

post Step   run only if build sucess

执行Shell

# 进入jenkins工作目录执行doker build工作
cd /opt/jenkins/home/workspace/springboot-webtest
mvn clean package -P docker-test
docker images | grep -E "(springboot-webtest)" | awk '{print $3}' | uniq | xargs -I {} docker rmi --force {}
docker build -t test/springboot-webtest:1.0-SNAPSHOT .
docker tag test/springboot-webtest:1.0-SNAPSHOT 192.168.1.34:8088/test/springboot-webtest:1.0-SNAPSHOT
docker push 192.168.1.34:8088/test/springboot-webtest:1.0-SNAPSHOT

Execute shell script on remote host using ssh

# 进入目标机器执行部署操作
docker ps -a | grep -E "(springboot-webtest)" | awk '{print $1}' | uniq | xargs -I {} docker stop  {}
docker ps -a | grep -E "(springboot-webtest)" | awk '{print $1}' | uniq | xargs -I {} docker rm  {}
docker images | grep -E "(springboot-webtest)" | awk '{print $3}' | uniq | xargs -I {} docker rmi --force {}
docker pull 192.168.1.35:8088/test/springboot-webtest:1.0-SNAPSHOT
docker run -itd -p 7701:7701 --name=springboot-webtest1.0  172.18.57.142:8088/test/springboot-webtest:1.0-SNAPSHOT

 

你可能感兴趣的:(jenkins,docker,Maven)