通過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

 

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