推送到其它平台
设置环境变量 REGISTRIES
的值为 ~/.docker/config.json
内容并且 base64
后的值。
base64 -w 0 ~/.docker/config.json
default:
image: docker:latest
services:
- name: docker:dind
variables:
HEALTHCHECK_TCP_PORT: "2375"
command: ["--registry-mirror", "https://mirror.ccs.tencentyun.com"]
before_script:
- docker info
- |
sed -i.bak 's#dl-cdn.alpinelinux.org#mirrors.tencentyun.com#g' /etc/apk/repositories
apk update && apk add --no-cache bash jq
- /bin/bash --login
- bash --version
- |
if [ -n "${REGISTRIES:-}" ]; then
REGISTRY_CONF=$(echo "$REGISTRIES" | base64 -d)
fi
- |
if [ -z "${REGISTRY_CONF:-}" ]; then
echo "not found REGISTRIES"
exit 1
fi
- mkdir -p ~/.docker/
- echo "$REGISTRY_CONF" > ~/.docker/config.json
# - while read -r reg; do echo "$reg"; done <<< $(jq -r '.auths | to_entries[] | .key' ~/.docker/config.json)
variables:
# When using dind service, you must instruct Docker to talk with
# the daemon started inside of the service. The daemon is available
# with a network connection instead of the default
# /var/run/docker.sock socket.
# DOCKER_HOST: tcp://docker:2375
#
# The 'docker' hostname is the alias of the service container as described at
# https://docs.gitlab.com/ee/ci/services/#accessing-the-services.
# If you're using GitLab Runner 12.7 or earlier with the Kubernetes executor and Kubernetes 1.6 or earlier,
# the variable must be set to tcp://localhost:2376 because of how the
# Kubernetes executor connects services to the job container
# DOCKER_HOST: tcp://localhost:2376
#
# This instructs Docker not to start over TLS.
DOCKER_TLS_CERTDIR: ""
build:
stage: build
script:
- docker build -t jetsung/test .
# - docker push jetsung/test
- docker images
- docker images --format '{{.Repository}}:{{.Repository}}'
- |
registries=$(jq -r '.auths | keys[]' ~/.docker/config.json)
# 列出所有本地镜像
docker_images=$(docker images --format "{{.Repository}}:{{.Tag}}")
# 遍历每个镜像
for image in $docker_images; do
# 检查是否是 Docker Hub 的镜像
if [[ $image == "library/"* || $image == *"/"* ]]; then
# 获取镜像名称和标签
repo=${image%:*}
tag=${image##*:}
# 如果没有标签,默认为 latest
if [ "$tag" = "$image" ]; then
tag="latest"
fi
# 遍历每个已登录的 Registry
for registry in $registries; do
# 打上新标签
new_image="$registry/${repo#:library/}:${tag}"
docker tag "$image" "$new_image"
# 推送到新 Registry
if ! docker push "$new_image" > /dev/null 2>&1; then
echo "Push failed for $new_image"
else
echo "Push successful for $new_image"
fi
done
fi
done