Files
sponge/scripts/image-push.sh
2022-10-01 20:28:41 +08:00

56 lines
1.3 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# todo 填写自己的私有镜像仓库hosthttps://index.docker.io/v1是docker官方镜像仓库
IMAGE_REPO_HOST="https://index.docker.io/v1"
# 镜像名称不能有大写字母
SERVER_NAME="project-name-example.server-name-example"
# 镜像仓库地址,通过第一个参数传进来
#REPO_HOST="ip或域名"
REPO_HOST=$1
if [ "X${REPO_HOST}" = "X" ];then
echo "param 'repo host' cannot be empty, example: ./image-push.sh hub.docker.com v1.0.0"
exit 1
fi
# 版本tag通过第二参数传进来如果为空默认为latest
TAG=$2
if [ "X${TAG}" = "X" ];then
TAG="latest"
fi
# 镜像名称和tag
IMAGE_NAME_TAG="${REPO_HOST}/${SERVER_NAME}:${TAG}"
function checkResult() {
result=$1
if [ ${result} -ne 0 ]; then
exit ${result}
fi
}
# 检查是否授权登录docker
function checkLogin() {
loginStatus=$(cat /root/.docker/config.json | grep "${IMAGE_REPO_HOST}")
if [ "X${loginStatus}" = "X" ];then
echo "docker未登录镜像仓库"
checkResult 1
fi
}
checkLogin
# 上传镜像
echo "docker push ${IMAGE_NAME_TAG}"
docker push ${IMAGE_NAME_TAG}
checkResult $?
echo "docker push image success."
sleep 1
# 删除镜像
echo "docker rmi -f ${IMAGE_NAME_TAG}"
docker rmi -f ${IMAGE_NAME_TAG}
checkResult $?
echo "docker remove image success."