需要用到2个文件,项目的pom.xml文件和自动化shell脚本(这里我们命名为auto.sh)
一、修改pom.xml文件
1、在build - plugins节点下新增maven Docker插件
<!-- Docker maven plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}:latest</imageName>
<dockerDirectory>${project.basedir}/</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<!-- Docker maven plugin -->
2、properties节点下,新增变量
<docker.image.prefix>qa</docker.image.prefix>
二、新增auto.sh
#!/bin/bash
set -e
#这里的docker:build与pom.xml文件里刚刚新增的配置节对应
./mvnw clean package docker:build
docker_username=dockerAccountName
#密码为#EDC4rfv
docker_pwd=\#EDC4rfv
docker_project=projectGroup/projectName
docker_server_addr=my.dockerserver.com
docker_image=${docker_project}:latest
docker login -u${docker_username} -p${docker_pwd} ${docker_server_addr}
docker tag ${docker_image} ${docker_server_addr}/${docker_image}
docker push ${docker_server_addr}/${docker_image}
#需要安装sshpass命令,应用服务器登录名aaa,应用服务器ip 11.22.33.44
#远程执行的内容在“<< eeooff ” 至“ eeooff ”之间,在远程机器上的操作就位于其中
sshpass ssh aaa@11.22.33.44 -i /Users/bestfei/.ssh/my-rsa << eeooff
echo "========= docker stop ========="
docker ps -a | grep "${docker_server_addr}/${docker_project}" | awk '{print \$1}'|xargs docker stop
echo "========= docker rm ========="
docker ps -a | grep "${docker_server_addr}/${docker_project}" | awk '{print \$1}'|xargs docker rm
echo "========= docker rmi ========="
docker images | grep "${docker_server_addr}/${docker_project}"|awk '{print \$3}'|xargs docker rmi
docker login -u${docker_username} -p${docker_pwd} ${docker_server_addr}
docker pull ${docker_server_addr}/${docker_image}
docker run -p 8818:8080 -d --name dockerContainerName ${docker_server_addr}/${docker_image}
exit
eeooff
网友评论