jenkins 自动构建docker 推送镜像发布镜像

1、通过pom.xml 进行clean package docker:build


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>


    <groupId>com.fintech.platform</groupId>
    <artifactId>bootmonitor</artifactId>
    <version>1.0.0</version>


    <repositories>
        <repository>
            <id>pt</id>
            <name>pt</name>
            <url>http://172.18.100.230:8080/nexus-2.5.1/content/groups/public</url>
        </repository>
    </repositories>


    
    
    <dependencies>
        


    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                    <fork>true</fork>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>springloaded</artifactId>
                        <version>1.2.5.RELEASE</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>


            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.4.14</version>
                <configuration>
                    <imageName>bootcsp/${project.artifactId}</imageName>
                    <dockerDirectory>src/main/docker</dockerDirectory>
                    <imageTags>
                        <imageTag>${version}</imageTag>
                    </imageTags>
                    
                </configuration>
            </plugin>
        </plugins>


        
    </build>
</project>



2、推送docker 镜像至仓库
echo '================开始向docker仓库推送镜像================'
project_id=bootcsp/bootmonitor
#version=latest
docker_registry=172.16.101.43:5000
image_tag=$docker_registry/$project_id:${POM_VERSION}
docker tag $project_id:${POM_VERSION} $image_tag
docker push $image_tag
docker rmi $image_tag
docker rmi $project_id:${POM_VERSION}
imageid=$(docker images |awk '{if($2=="<none>")  print $3}')
if [ $imageid != "" ]; then
   docker rmi $imageid
fi
curl http://$docker_registry/v2/_catalog
echo '================结束向docker仓库推送镜像================'


3、jenkins远程执行shell命令,拉去镜像,启动docker

echo '================remote obatin image start================'
CONTAINER_NAME=bootmonitor
docker pull 172.16.101.43:5000/bootcsp/$CONTAINER_NAME:${POM_VERSION}


echo '================remote启动容器start================'


#删除同名docker容器
cid=$(docker ps -a | grep "$CONTAINER_NAME" | awk '{print $1}')
if [ "$cid" != "" ]; then
   docker rm -f $cid
   sleep 3s
fi
docker run -d -p 20000:20000 -m 1024m --name $CONTAINER_NAME 172.16.101.43:5000/bootcsp/$CONTAINER_NAME:${POM_VERSION} --cpu 1
docker exec $CONTAINER_NAME /bin/sh -c "echo 172.18.100.196  eureka1 >> /etc/hosts"
docker exec $CONTAINER_NAME /bin/sh -c "echo 172.18.100.196  eureka2 >> /etc/hosts"
docker exec $CONTAINER_NAME /bin/sh -c "echo 172.18.100.196  bootmonitor >> /etc/hosts"


echo '================remote启动容器end================'
#docker logs -f $CONTAINER_NAME


猜你喜欢

转载自blog.csdn.net/chen978616649/article/details/78714418