通过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包,复制运行的文件结构中配置信息和启动脚本
    <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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章