forked from you8023/docker_images_sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.sh
More file actions
61 lines (51 loc) · 1.5 KB
/
Copy pathsync.sh
File metadata and controls
61 lines (51 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
set -eux
# 检查参数数量是否正确
if [ "$#" -ne 3 ]; then
echo "错误:脚本需要3个参数 images_file、docker_registry和docker_namespace"
echo "用法: $0 <images_file> <docker_registry> <docker_namespace>"
exit 1
fi
IMAGES_FILE=$1
TARGET_REGISTRY=$2
TARGET_NAMESPACE=$3
# 检查文件是否存在
if [ ! -f "$IMAGES_FILE" ]; then
echo "错误:文件 $IMAGES_FILE 不存在"
exit 1
fi
failed_count=0
failed_images=""
while IFS= read -r image; do
# 拉取镜像
set +e
docker pull "$image"
pull_status=$?
if [ $pull_status -ne 0 ]; then
echo "Error: Failed to pull image $image, continuing..."
failed_count=$((failed_count + 1))
failed_images="${failed_images} ${image}"
continue
fi
name=$(echo "${image}" | cut -d '/' -f2)
tag=$(echo "${name}" | cut -d ':' -f2)
targetFullName=${TARGET_REGISTRY}/${TARGET_NAMESPACE}/${name}
# 打阿里云的tag
docker tag "${image}" "${targetFullName}"
tag_status=$?
# 推送到阿里云
set +e
docker push "${targetFullName}"
push_status=$?
if [ $push_status -ne 0 ]; then
echo "Error: Failed to push image $targetFullName, continuing..."
failed_count=$((failed_count + 1))
failed_images="${failed_images} ${image}"
continue
fi
done < "$IMAGES_FILE"
if [ $failed_count -gt 0 ]; then
echo "Error: Failed to sync $failed_count images: $failed_images"
exit 1
fi
echo "Successfully synced all images."