-
Notifications
You must be signed in to change notification settings - Fork 1
148 lines (128 loc) · 4.95 KB
/
docker-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
name: docker image mirror
on:
issues:
types: [opened]
env:
RED: \033[1;31m
GREEN: \033[1;32m
YELLOW: \033[1;33m
BLUE: \033[1;34m
PURPLE: \033[1;35m
CYAN: \033[1;36m
BLANK: \033[0m
jobs:
build:
runs-on: ubuntu-latest
outputs:
DOCKER_IMAGE: ${{ steps.pullIssuesPorter.outputs.DOCKER_IMAGE }}
SUCCESS: ${{ steps.successCheck.outputs.SUCCESS }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Get porter issues
id: pullIssuesPorter
uses: actions/github-script@v7
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
// 使用 title 获取镜像名和tag
const title = context?.payload?.issue?.title;
// 使用 body 获取其它参数
const body = context?.payload?.issue?.body || '';
const reg = new RegExp("\\[PORTER\\]", "g");
let docker_image = title.replace(reg, "").trim();
const issues_author = context?.payload?.issue?.user?.login;
// 为了防止 image 不带tag,自动添加 latest
if(!docker_image.includes(":")) {
docker_image = `${docker_image}:latest`
}
let comment_body = '';
let is_error = false;
if( docker_image.includes("@")){
is_error = true;
comment_body = '@' + issues_author +' 拉取镜像不支持带摘要信息,请去除 @部分'
}else{
comment_body = `构建进展,详见 [构建任务](https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${{github.run_id}})`
}
const issuesComment = await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: comment_body
});
console.log("create issues comment resp:", issuesComment["status"]);
if(is_error){
core.setFailed("Error");
}else if (!docker_image){
core.setFailed("No Images");
}
core.setOutput('DOCKER_IMAGE', docker_image);
core.setOutput('BUILD_ARGS', body);
- name: Retrieve transfer image name
id: transferImage
run: |
echo "${{ steps.pullIssuesPorter.outputs.DOCKER_IMAGE }}" > docker_images.list
- name: Sync image
id: syncImage
shell: bash
run: |
bash jenkins_sync_docker_images.sh docker_images.list
- name: Success check
id: successCheck
uses: actions/github-script@v7
if: ${{ success() }}
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
core.setOutput('SUCCESS', true);
- name: Close Porter Issues
id: closePorterIssues
uses: actions/github-script@v7
if: ${{ always() }}
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const issuesResponse = await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
state: 'closed'
});
console.log("update issues resp:", issuesResponse["status"] == 200 ? "success" : "failed" );
let comment_body = `转换失败,详见 [构建任务](https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${{github.run_id}})`;
let success = String(${{ steps.successCheck.outputs.SUCCESS }}).toLowerCase() == "true";
console.log("is success?", success);
let labels = [];
if(success){
comment_body = "转换完成,请到你的 Harbor 仓库愉快使用吧 <br/>\n"
labels=['success']
}else{
const jobsResponse = await github.request(`GET /repos/${context.repo.owner}/${context.repo.repo}/actions/runs/${{github.run_id}}/jobs`, {
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{ github.run_id }}
});
console.log("jobs",jobsResponse['data']);
comment_body += "\n\n 日志:\n\n";
for(let job of jobsResponse['data']['jobs']){
comment_body += "- [" + job.name + "](" + job.html_url +")";
}
labels = ['failure'];
}
// 创建 issues comment
const issuesComment = await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: comment_body
});
console.log("create issues comment resp:", issuesComment["status"] == 201 ? "success" : "failed" );
// 更新 issues label
if(labels){
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: labels
});
}