forked from tech-shrimp/docker_image_pusher
-
Notifications
You must be signed in to change notification settings - Fork 0
137 lines (106 loc) · 4.47 KB
/
docker.yaml
File metadata and controls
137 lines (106 loc) · 4.47 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
name: Docker
on:
workflow_dispatch:
push:
branches: [ main ]
env:
ALIYUN_REGISTRY: "${{ secrets.ALIYUN_REGISTRY }}"
ALIYUN_NAME_SPACE: "${{ secrets.ALIYUN_NAME_SPACE }}"
ALIYUN_REGISTRY_USER: "${{ secrets.ALIYUN_REGISTRY_USER }}"
ALIYUN_REGISTRY_PASSWORD: "${{ secrets.ALIYUN_REGISTRY_PASSWORD }}"
jobs:
build:
name: Pull
runs-on: ubuntu-latest
steps:
- name: Before freeing up disk space
run: |
echo "Before freeing up disk space"
echo "=============================================================================="
df -hT
echo "=============================================================================="
# 增加可用磁盘空间
- name: Maximize build space
uses: easimon/maximize-build-space@master
with:
root-reserve-mb: 2048
swap-size-mb: 128
remove-dotnet: 'true'
remove-haskell: 'true'
# 如果空间还是不够用,可以把以下开启,清理出更多空间
# remove-android: 'true'
# remove-codeql: 'true'
build-mount-path: '/var/lib/docker/'
- name: Restart docker
run: sudo service docker restart
- name: Free up disk space complete
run: |
echo "Free up disk space complete"
echo "=============================================================================="
df -hT
echo "=============================================================================="
- name: Checkout Code
uses: actions/checkout@v4
- name: Docker Setup Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push image Aliyun
run: |
docker login -u $ALIYUN_REGISTRY_USER -p $ALIYUN_REGISTRY_PASSWORD $ALIYUN_REGISTRY
max_count=72
# 函数:裁剪字符串,直到长度小于或等于最大长度
trim_string() {
local str="$1"
# 计算字符串长度
local len=${#str}
# 只要字符串长度大于最大长度,就继续裁剪
while [ "$len" -gt $max_count ]; do
# 去除第一个'/'及其后的所有字符
str=${str#*/}
# 更新字符串长度
len=${#str}
done
# 返回裁剪后的字符串
echo "$str"
}
while IFS= read -r line || [ -n "$line" ]; do
# 忽略空行与注释
[[ -z "$line" ]] && continue
if echo "$line" | grep -q '^\s*#'; then
continue
fi
echo "docker pull $line"
docker pull "$line"
platform=$(echo "$line" | awk -F'--platform[ =]' '{if (NF>1) print $2}' | awk '{print $1}')
echo "platform is $platform"
# 如果存在架构信息 将架构信息拼到镜像名称前面
if [ -z "$platform" ]; then
platform_prefix=""
else
platform_prefix="${platform//\//_}_"
fi
echo "platform_prefix is $platform_prefix"
# 获取镜像的完整名称,例如kasmweb/nginx:1.25.3(命名空间/镜像名:版本号)
image=$(trim_string "$line")
# 获取 镜像名:版本号 例如1.25.3
image_tag=$(echo "$image" | awk -F":" '{print $2}')
# 将@sha256:等字符删除
image_name_tag="${image_tag%%@*}"
repo=$(echo "$image" | cut -d':' -f1)
# 替换/为_,这里使用sed命令
replaced_repo=$(echo "$repo" | sed 's/\//_/g')
new_image="$ALIYUN_REGISTRY/$ALIYUN_NAME_SPACE/$platform_prefix$replaced_repo:$image_name_tag"
echo "docker tag $line $new_image"
docker tag "$line" "$new_image"
echo "docker push $new_image"
docker push "$new_image"
echo "开始清理磁盘空间"
echo "=============================================================================="
df -hT
echo "=============================================================================="
docker rmi "$line"
docker rmi "$new_image"
echo "磁盘空间清理完毕"
echo "=============================================================================="
df -hT
echo "=============================================================================="
done < images.txt