需要用到2个文件,项目的build.gradle文件和自动化shell脚本(这里我们命名为auto.sh)
一、修改build.gradle文件
新增task配置节,注意build.gradle和Dockerfile在同一目录下
task buildDocker(type: Docker, dependsOn: build) {
applicationName = jar.baseName
tagVersion = "latest"
dockerfile = file('Dockerfile')
doFirst {
copy {
from jar
into stageDir
}
}
}
二、新增auto.sh
#!/bin/bash
set -e
#这里的buildDocker与build.gradle文件里刚刚新增的task配置节对应
gradle clean build buildDocker -x test --info
docker_username=dockerAccountName
#密码为#EDC4rfv
docker_pwd=\#EDC4rfv
docker_project=projectGroup/projectName
docker_server_addr=my.dockerserver.com
docker login -u${docker_username} -p${docker_pwd} ${docker_server_addr}
#用于上传到latest分支
docker_image=${docker_project}:latest
#docker tag ${docker_image} ${docker_server_addr}/${docker_image}
#docker push ${docker_server_addr}/${docker_image}
#用于自定义docker分支
docker_tag=v1
docker tag ${docker_image} ${docker_server_addr}/${docker_project}:${docker_tag}
docker push ${docker_server_addr}/${docker_project}:${docker_tag}
#需要安装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 8848:8080 -d --name dockerContainerName ${docker_server_addr}/${docker_image}
docker pull ${docker_server_addr}/${docker_project}:${docker_tag}
docker run -p 8848:8080 -d --name dockerContainerName ${docker_server_addr}${docker_project}:${docker_tag}
exit
eeooff
网友评论