-
Notifications
You must be signed in to change notification settings - Fork 0
263 lines (240 loc) · 10.2 KB
/
sync-docker-image.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
name: Sync Docker Image To Cloud Repository
on:
issues:
types: [ opened,edited ]
workflow_dispatch:
jobs:
# 同步镜像文件
sync-docker-image:
runs-on: ubuntu-latest
# 设置超时时间为 360 分钟(6 小时)
timeout-minutes: 360
steps:
# 拉取源代码
- name: Checkout repository
uses: actions/checkout@v4
# 判断是否为新建 issue
- name: Check if issue is new
id: check_issue
uses: actions/github-script@v7
with:
script: |
if (context.payload.action === 'opened') {
core.setOutput('is_new', 'true');
return { is_new: true };
} else {
core.setOutput('is_new', 'false');
return { is_new: false };
}
# 获取 issue 的内容并比较
- name: Compare issue content
if: steps.check_issue.outputs.is_new == 'false'
id: compare_issue
uses: actions/github-script@v7
with:
script: |
const issue_number = context.issue.number;
const repo_owner = context.repo.owner;
const repo_name = context.repo.repo;
// 获取当前的 issue 内容
const current_issue = await github.rest.issues.get({
owner: repo_owner,
repo: repo_name,
issue_number: issue_number
});
// 获取 issue 编辑事件的历史记录
const issue_events = await github.rest.issues.listEvents({
owner: repo_owner,
repo: repo_name,
issue_number: issue_number
});
// 找到最近一次编辑事件
const edit_event = issue_events.data.reverse().find(event => event.event === 'edited');
if (!edit_event) {
console.log('No edit event found. Assuming issue content has not changed.');
core.setOutput('content_changed', 'false');
return;
}
// 获取编辑前的 issue 内容
const previous_issue = await github.rest.issues.get({
owner: repo_owner,
repo: repo_name,
issue_number: issue_number,
headers: { 'If-None-Match': edit_event.node_id }
});
const current_body = current_issue.data.body || '';
const previous_body = previous_issue.data.body || '';
console.log(`Current body: ${current_body}`);
console.log(`Previous body: ${previous_body}`);
if (current_body === previous_body) {
console.log('Issue body has not changed.');
core.setOutput('content_changed', 'false');
} else {
console.log('Issue body has changed.');
core.setOutput('content_changed', 'true');
}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# 继续执行步骤,如果内容未改变则跳过后续步骤
- name: Check if content changed
id: determine_skip
run: |
if [[ "${{ steps.check_issue.outputs.is_new }}" == "false" && "${{ steps.compare_issue.outputs.content_changed }}" == "false" ]]; then
echo "skip_next_steps=true" >> $GITHUB_ENV
else
echo "skip_next_steps=false" >> $GITHUB_ENV
fi
# 设置 JDK
- name: Set up JDK 17
if: ${{ env.skip_next_steps == 'false'}}
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
# 查看目录结构
- name: tree
if: ${{ env.skip_next_steps == 'false'}}
run: |
apt -y upgrade && apt -y update && apt -y install tree
tree -L 3 .
pwd && ls -lah
# 构建 Java 镜像
- name: Build Docker image
if: ${{ env.skip_next_steps == 'false'}}
run: docker build -t springboot-docker-java-action .
# 检查用户组
- name: Check user groups
if: ${{ env.skip_next_steps == 'false'}}
run: groups
# 启动 Java 容器
- name: Run SpringBoot container
if: ${{ env.skip_next_steps == 'false'}}
run: |
docker run -d -p 8080:8080 --privileged --name springboot-container \
-e "ALIYUN_USERNAME=${{ secrets.ALIYUN_USERNAME }}" \
-e "ALIYUN_PASSWORD=${{ secrets.ALIYUN_PASSWORD }}" \
-e "ALIYUN_REGISTRY=${{ secrets.ALIYUN_REGISTRY }}" \
-e "ALIYUN_NAMESPACE=${{ secrets.ALIYUN_NAMESPACE }}" \
-e "TENCENT_USERNAME=${{ secrets.TENCENT_USERNAME }}" \
-e "TENCENT_PASSWORD=${{ secrets.TENCENT_PASSWORD }}" \
-e "TENCENT_REGISTRY=${{ secrets.TENCENT_REGISTRY }}" \
-e "TENCENT_NAMESPACE=${{ secrets.TENCENT_NAMESPACE }}" \
-e "HUAWEI_USERNAME=${{ secrets.HUAWEI_USERNAME }}" \
-e "HUAWEI_PASSWORD=${{ secrets.HUAWEI_PASSWORD }}" \
-e "HUAWEI_REGISTRY=${{ secrets.HUAWEI_REGISTRY }}" \
-e "HUAWEI_NAMESPACE=${{ secrets.HUAWEI_NAMESPACE }}" \
-e "CUSTOM_USERNAME=${{ secrets.CUSTOM_USERNAME }}" \
-e "CUSTOM_PASSWORD=${{ secrets.CUSTOM_PASSWORD }}" \
-e "CUSTOM_REGISTRY=${{ secrets.CUSTOM_REGISTRY }}" \
-e "CUSTOM_NAMESPACE=${{ secrets.CUSTOM_NAMESPACE }}" \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $PWD:/path/in/container \
springboot-docker-java-action --server.port=8080
# 等待 SpringBoot 启动成功
- name: Wait for SpringBoot to start
if: ${{ env.skip_next_steps == 'false'}}
run: |
echo "Waiting for SpringBoot to start..."
for i in {1..10}; do
curl -s http://localhost:8080/actuator/health | grep '"status":"UP"' && break || sleep 5
echo "SpringBoot is not up yet, waiting..."
docker logs springboot-container
done
curl -s http://localhost:8080/actuator/health | grep '"status":"UP"' || (echo "SpringBoot failed to start in time" && exit 1)
echo "SpringBoot is up and running!"
# 检查 SpringBoot 的日志(首次)
- name: Stream SpringBoot logs
if: ${{ env.skip_next_steps == 'false'}}
run: |
echo "Starting to stream SpringBoot logs..."
nohup docker logs -f springboot-container > springboot-container.log &
# 提取 issue 内容并设置变量
- name: Extract issue content
id: extract_issue
if: ${{ env.skip_next_steps == 'false'}}
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
// 获取 issue 的内容
const issueContentString = context?.payload?.issue?.body || '{}';
console.log(`issueContentString --> ${issueContentString}`);
// 写入 JSON 文件
fs.writeFileSync('issue_content.json', issueContentString );
// 解析 JSON 字符串为对象
const issueContent = JSON.parse(issueContentString);
const images = issueContent['hub-mirrors'] || [];
console.log(`images ==> ${images}`)
const jsonPayload = JSON.stringify({
images: images
});
console.log(jsonPayload)
fs.writeFileSync('json_payload.json', jsonPayload);
# 将数据发送给 SpringBoot
- name: Send Data to SpringBoot Service
if: ${{ env.skip_next_steps == 'false'}}
run: |
echo "Sending data to SpringBoot Service..."
cat json_payload.json
# --trace -v
curl --http1.1 --trace-ascii /dev/stdout --max-time 3600 --retry 5 --retry-delay 30 -X POST http://localhost:8080/process -H "Content-Type: application/json" -d @json_payload.json
echo "Data sent to SpringBoot Service."
# 检查 SpringBoot 的日志
- name: Check SpringBoot logs
if: ${{ env.skip_next_steps == 'false'}}
run: |
echo "Checking SpringBoot logs..."
docker logs springboot-container
# 查看完整的日志
- name: Check complete SpringBoot logs
if: ${{ env.skip_next_steps == 'false'}}
run: |
echo "Displaying complete SpringBoot logs..."
tree -L 3 .
pwd && ls -lah
ls -lah $PWD
cat springboot-container.log
# 增加评论
- name: Add comment
if: ${{ env.skip_next_steps == 'false' && hashFiles('output.md') }}
uses: actions/github-script@v7
with:
script: |
const fs = require('fs')
const data = fs.readFileSync('output.md', 'utf8')
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: data
})
# 当以上步骤成功时,为 issues 添加 success 标签
- name: Success issues
if: ${{ env.skip_next_steps == 'false' && success() }}
uses: actions/github-script@v7
with:
script: |
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['success']
})
# 当以上步骤失败时,为 issues 添加 failure 标签,并为其添加失败原因评论
- name: Failure issues
if: ${{ env.skip_next_steps == 'false' && failure() }}
uses: actions/github-script@v7
with:
script: |
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['failure']
})
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: "[构建失败,点击查看](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})"
})