-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcn_upload.js
188 lines (182 loc) · 7.26 KB
/
cn_upload.js
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
const COS = require('cos-nodejs-sdk-v5');
const tencentcloud = require('tencentcloud-sdk-nodejs');
module.exports = async ({ github, context, core }) => {
const { COS_SECRET_ID, COS_SECRET_KEY, COS_BUCKET, COS_REGION } =
process.env;
// Note: 获取
var cos = new COS({
SecretId: COS_SECRET_ID,
SecretKey: COS_SECRET_KEY,
});
// Note: action/github 中的 github.event.commits 对象中并不包含 added modifiy removed 等资源(shit!) https://docs.github.com/cn/actions/learn-github-actions/events-that-trigger-workflows#push
// API 发送接口看这个: https://docs.github.com/cn/rest/reference/repos#get-a-commit
// 所以需要手动发送请求获取 change,因此我用 compare 接口:https://docs.github.com/cn/rest/reference/repos#compare-two-commits
// 参考了这个人的 aciton: https://github.com/jitterbit/get-changed-files/blob/master/src/main.ts
// 不过该 API 已经废弃了,要使用这个接口: compareCommitsWithBasehead
// Note: 如果 commit msg 中含有特定信息则不再继续
if (context.payload.head_commit.message.startsWith('NO')) {
console.log('本次任务终止');
return;
}
const base = context.payload.before;
const head = context.payload.after;
const response = await github.rest.repos.compareCommitsWithBasehead({
basehead: `${base}...${head}`,
owner: context.repo.owner,
repo: context.repo.repo,
});
if (response.status !== 200) {
core.setFailed(
`The GitHub API for comparing the base and head commits for this ${context.eventName} event returned ${response.status}, expected 200. ` +
"Please submit an issue on this action's GitHub repo."
);
}
if (response.data.status !== 'ahead') {
core.setFailed(`head commit 的 id 落后于 base commit,搞错了吧?`);
}
const files = response.data.files;
// Note: 从 push 事件中获取到相关文件变动信息,然后进行相应的上传和删除
const addAndModifyList = [];
const removedList = [];
const renamedList = [];
for (const file of files) {
const filename = file.filename;
switch (file.status) {
case 'added':
case 'modified':
addAndModifyList.push(filename);
break;
case 'removed':
removedList.push(filename);
break;
case 'renamed':
// Note: 重命名的,需要删了旧的,上传新的
addAndModifyList.push(filename);
removedList.push(file.previous_filename);
renamedList.push({
before: file.previous_filename,
after: filename
});
default:
console.log(`特殊文件状态:${file.status}:`, file);
break;
}
}
if (addAndModifyList.length) {
console.log('添加或修改的文件列表:', addAndModifyList);
}
if (removedList.length) {
console.log('移除的文件列表:', removedList);
}
if (renamedList.length) {
console.log('重命名的文件有:', renamedList);
}
console.log('--------------------------------------------');
const ignored = [];
const addFiles = [];
const removedFiles = [];
for (let fileName of addAndModifyList) {
addFiles.push({
Bucket: COS_BUCKET,
Region: COS_REGION,
Key: fileName,
FilePath: fileName
});
}
for (let fileName of removedList) {
console.log('即将删除文件:', fileName);
removedFiles.push({
Key: fileName,
});
}
// Note: 执行上传
if (addAndModifyList.length) {
cos.uploadFiles(
{
files: addFiles,
SliceSize: 1024 * 1024 * 10 /* 设置大于10MB采用分块上传 */,
onProgress: function (info) {
var percent = parseInt(info.percent * 10000) / 100;
var speed =
parseInt((info.speed / 1024 / 1024) * 100) / 100;
console.log(
'进度:' + percent + '%; 速度:' + speed + 'Mb/s;'
);
},
onFileFinish: function (err, data, options) {
console.log(
options.Key +
'上传' +
(err ? '失败:' + err.statusCode + err : '完成')
);
console.log('--------------------------------------------');
},
},
function (err, data) {
if (err) {
console.log(`批量上传错误码: ${err.statusCode}, ${err}`);
} else {
console.log('批量上传完成:', data.options, data.data);
console.log(
'----------------上传完成,开始刷新 cdn url------------'
);
const CdnClient = tencentcloud.cdn.v20180606.Client;
const clientConfig = {
credential: {
secretId: COS_SECRET_ID,
secretKey: COS_SECRET_KEY,
},
region: '',
profile: {
httpProfile: {
endpoint: 'cdn.tencentcloudapi.com',
},
},
};
const client = new CdnClient(clientConfig);
// Note: 需要刷新的 url 地址
const refreshList = addAndModifyList.map((fileName) => {
return `https://www.xheldon.cn/${fileName}`;
});
if (refreshList.length) {
console.log('CDN 刷新文件列表:', refreshList);
const params = {
Urls: refreshList,
};
client.PurgeUrlsCache(params).then(
(data) => {
console.log('刷新成功:', data);
},
(err) => {
console.error('刷新失败:', err);
}
);
} else {
console.log('本次无需 CDN 刷新');
}
}
}
);
} else {
console.log('本次没有需要上传的文件');
}
// Note: 执行删除
if (removedList.length) {
cos.deleteMultipleObject(
{
Bucket: COS_BUCKET,
Region: COS_REGION,
Objects: removedFiles,
},
function (err, data) {
if (err) {
console.log('删除过程出现错误:', err);
} else {
console.log('删除结果:', data);
}
}
);
} else {
console.log('本次没有需要删除的文件');
}
};