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

7 篇文章 0 订阅
3 篇文章 0 订阅

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

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

然后在Jenkins服务器上

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

再按照如下步骤

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

pom.xml文件配置点:

复制jar包,复制运行的文件结构中配置信息和启动脚本
    <profiles>
        <profile>
        <!-- mvn package spring-boot:repackage -P docker-test -->
            <id>docker-test</id>
            <properties>
                <buildType>docker-test</buildType>
            </properties>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <build>
                <plugins>
                </plugins>
            </build>
        </profile>
    </profiles>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>cn.shary.boot.BootStrapSpringbootWebtest</mainClass>
                    <layout>JAR</layout>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <copy overwrite='true'
                                    tofile='${project.build.directory}/springboot-webtest/lib/${project.artifactId}.jar'
                                    file='${project.build.directory}/${project.build.finalName}.jar' />
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-bin</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <overwrite>true</overwrite>
                            <outputDirectory>${project.build.directory}/springboot-webtest/bin</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${basedir}/bin-script</directory>
                                    <includes>
                                        <include>**/*.sh</include>
                                        <include>**/*.bat</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-conf</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <overwrite>true</overwrite>
                            <outputDirectory>${project.build.directory}/springboot-webtest/conf</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.build.directory}/classes</directory>
                                    <includes>
                                        <include>**/*.xml</include>
                                        <include>**/*.yml</include>
                                        <include>**/*.properties</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Docker是一个开源的应用容器引擎,可以帮助开发者快速轻松地构建、部署和运行应用程序。它提供了一个隔离的环境,可以把应用程序和其依赖项打包成一个容器,使得应用程序在不同的环境中能够一致地运行。 Jenkins是一个开源的持续集成工具,它可以自动构建、测试和部署应用程序。通过与代码仓库的集成,Jenkins可以在代码发生变更时自动触发构建过程,帮助团队快速地检测和解决问题,提高开发效率。 GitLab是一个基于Git的开源代码托管平台,它提供了代码仓库管理、代码评审、问题跟踪、持续集成等功能。与其他代码托管平台相比,GitLab更加强调安全性和可扩展性,支持企业内部自建和云端部署,适用于团队协作和敏捷开发。 Maven是一个用于构建、管理和发布Java项目的工具。它通过一个配置文件描述项目的结构和依赖关系,可以自动下载并管理项目的依赖项,统一构建过程和产出,提高项目的可维护性和可复用性。 Spring Boot是一个基于Spring框架的开源Java开发框架。它的目标是简化Spring应用程序的开发和部署,并提供一套快速构建可独立运行的Java应用程序的解决方案。Spring Boot提供了自动配置、快速开发等特性,可以帮助开发者更加高效地开发和部署Spring应用程序。 综上所述,DockerJenkins、GitLab、Maven和Spring Boot是一些常用的开发和部署工具,它们相互配合使用可以帮助开发者更好地进行应用程序的构建、测试和部署工作,提高开发效率和应用程序的质量。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值