-
Notifications
You must be signed in to change notification settings - Fork 250
207 lines (186 loc) · 8.03 KB
/
image-mirror.yml
File metadata and controls
207 lines (186 loc) · 8.03 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# workflow 名称
name: image-mirror
# 当新建 issues 时,触发
on:
issues:
types:
- opened
- reopened
- edited
# 需要执行的任务列表
jobs:
# 镜像转换任务
sync_to_target_registry:
# 运行环境
runs-on: ubuntu-22.04
# 运行条件 => issues 的 label 包含 image-mirror
# if: contains(github.event.issue.labels.*.name, 'image-mirror')
concurrency:
group: ${{ github.workflow }}-${{ github.actor }}-${{ github.event.issue.number }}
cancel-in-progress: false
# 镜像转换任务的步骤列表
steps:
- name: Check out code
uses: actions/checkout@v2
# - uses: hmarr/debug-action@v2
# name: debug
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Check issue body for image format
id: check_body
run: |
python .github/scripts/check_image_format.py "${{ github.event.issue.body }}"
- name: Add comment if body is not image format
if: steps.check_body.outputs.is_image_format == 'false'
uses: peter-evans/create-or-update-comment@v2
with:
issue-number: ${{ github.event.issue.number }}
body: |
@${{ github.event.issue.user.login }}
!!!请使用标准镜像格式
1. 镜像要带tag,如nginx,请写为nginx:latest
2. 标题不重要,issue描述部分,每行一个镜像不要有其他内容,你可以直接更新你的issue
reactions: hooray
edit-mode: replace
- name: Update issue title with image summary
if: steps.check_body.outputs.is_image_format == 'true'
uses: actions/github-script@v5
with:
script: |
const issue_number = context.issue.number;
const owner = context.repo.owner;
const repo = context.repo.repo;
const body = context.payload.issue.body;
// 🧾 拆分 body,并跳过第一行
const lines = body.split(/\r?\n/).map(line => line.trim()).filter(Boolean);
const images = lines.slice(1); // 从第二行开始提取镜像名
const top3 = images.slice(0, 3).join(' | ');
const total = images.length;
const newTitle = `${top3} (共 ${total} 个镜像)`;
await github.rest.issues.update({
owner,
repo,
issue_number,
title: newTitle
});
- name: Add image-mirror label if body is image format
if: steps.check_body.outputs.is_image_format == 'true'
uses: actions-ecosystem/action-add-labels@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
labels: 'image-mirror'
- name: Create comment
if: ${{ steps.check_body.outputs.is_image_format == 'true' && steps.fc.outputs.comment-id == '' }}
uses: peter-evans/create-or-update-comment@v2
with:
issue-number: ${{ github.event.issue.number }}
body: |
Syncing image to TargetRegistry, please wait a moment
[Check the progress](https://github.com/${{ github.repository_owner }}/image-mirror/actions/runs/${{ github.run_id }})
reactions: rocket
- name: prepare
if: steps.check_body.outputs.is_image_format == 'true'
run: |
wget -q https://github.com/AliyunContainerService/image-syncer/releases/download/v1.5.4/image-syncer-v1.5.4-linux-amd64.tar.gz
tar zxf image-syncer-v1.5.4-linux-amd64.tar.gz
sudo apt install dos2unix
- name: start sync
if: steps.check_body.outputs.is_image_format == 'true'
id: syncImage
env:
TARGET_REGISTRY_PASSWORD: ${{ secrets.TARGET_REGISTRY_PASSWORD }}
TARGET_REGISTRY_USER: ${{ vars.TARGET_REGISTRY_USER }}
# TARGET_REGISTRY: index.docker.io
# TARGET_REGISTRY: registry.cn-hangzhou.aliyuncs.com
TARGET_NAMESPACE: ${{ vars.TARGET_NAMESPACE }}
TARGET_REGISTRY: ${{ vars.TARGET_REGISTRY }}
#处理body中的空格和tab和换行,并在结尾添加: , 写入images.yml 文件
run: |
echo "${{ github.event.issue.body }}" > images-init.yml
dos2unix images-init.yml
sed -i 's/^[ \t]*//g' images-init.yml
sed -i 's/[ \t]*$//g' images-init.yml
sed -i '/^[ ]*$/d' images-init.yml
# img=$(cat images-init.yml)
img=$(tail -n +2 images-init.yml)
for i in ${img[@]}
do
tagName=$(echo $i | awk -F "/" '{print $NF}');
echo $i: ${TARGET_REGISTRY}/${TARGET_NAMESPACE}/${tagName} >> images.yml;
echo ${TARGET_REGISTRY}/${TARGET_NAMESPACE}/${tagName} >> dockerhub-image.yml;
done
cat images.yml
./image-syncer --auth=./auth.yml --images=./images.yml
- name: Add Success Issues
if: ${{ success() && steps.check_body.outputs.is_image_format == 'true' }}
uses: actions/github-script@v5
with:
script: |
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['succeed']
})
- id: get-comment-body
if: steps.check_body.outputs.is_image_format == 'true'
run: |
body="$(cat script.md)"
body="${body//'%'/'%25'}"
body="${body//$'\n'/'%0A'}"
body="${body//$'\r'/'%0D'}"
echo "::set-output name=body::$body"
dockerhubImage="$(cat dockerhub-image.yml)"
dockerhubImage="${dockerhubImage//'%'/'%25'}"
dockerhubImage="${dockerhubImage//$'\n'/'%0A'}"
dockerhubImage="${dockerhubImage//$'\r'/'%0D'}"
echo "::set-output name=dockerhubImage::$dockerhubImage"
sourceImage="$(cat images-init.yml)"
sourceImage="${sourceImage//'%'/'%25'}"
sourceImage="${sourceImage//$'\n'/'%0A'}"
sourceImage="${sourceImage//$'\r'/'%0D'}"
echo "::set-output name=sourceImage::$sourceImage"
- name: Find Comment
if: steps.check_body.outputs.is_image_format == 'true'
uses: peter-evans/find-comment@v2
id: fc
with:
issue-number: ${{ github.event.issue.number }}
comment-author: 'github-actions[bot]'
body-includes: Syncing image to TargetRegistry
- name: Add Tips
if: ${{ success() && steps.check_body.outputs.is_image_format == 'true' }}
uses: peter-evans/create-or-update-comment@v2
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ github.event.issue.number }}
body: |
[Check the progress](https://github.com/${{ github.repository_owner }}/image-mirror/actions/runs/${{ github.run_id }})
## 镜像信息如下
- dockerhub-image.yml
```shell
${{ steps.get-comment-body.outputs.dockerhubImage }}
```
- images-init.yml
```shell
${{ steps.get-comment-body.outputs.sourceImage }}
```
${{ steps.get-comment-body.outputs.body }}
reactions: hooray
edit-mode: replace
- name: Add failure hint comment
if: failure() && steps.check_body.outputs.is_image_format == 'true'
uses: peter-evans/create-or-update-comment@v2
with:
issue-number: ${{ github.event.issue.number }}
body: |
❌ 镜像同步失败,请检查以下几点:
- 源镜像是否拼写错误?
- tag 是否存在(如 `latest`、`1.21`)?
- 该镜像是否为公开可拉取的镜像?
- 若为私有镜像,请确认目标 registry 有访问权限
可参考 logs:
[查看工作详情](https://github.com/${{ github.repository_owner }}/image-mirror/actions/runs/${{ github.run_id }})
reactions: confused