forked from imdingtalk/image-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
165 lines (151 loc) · 6.32 KB
/
image-mirror.yml
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
# workflow 名称
name: image-mirror
# 当新建 issues 时,触发
on:
workflow_dispatch:
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 }}
cancel-in-progress: true
# 镜像转换任务的步骤列表
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: 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)
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