-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.ts
62 lines (59 loc) · 2 KB
/
upload.ts
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
import * as core from "@actions/core";
import { globby } from "globby";
import path from "path";
import md5File from "md5-file";
import { CreateReleaseResponse, OctokitClient } from "./typings";
import { Context } from "@actions/github/lib/context";
export const uploadReleaseArtifacts = async (
client: OctokitClient,
context: Context,
release: CreateReleaseResponse,
files: string[],
): Promise<void> => {
core.startGroup("Uploading release artifacts");
for (const fileGlob of files) {
const paths = await globby(fileGlob);
if (paths.length == 0) {
core.error(`${fileGlob} doesn't match any files`);
}
for (const filePath of paths) {
core.info(`Uploading: ${filePath}`);
const nameWithExt = path.basename(filePath);
try {
await client.repos.uploadReleaseAsset({
owner: context.repo.owner,
headers: {
"X-GitHub-Api-Version": "2022-11-28",
"Content-Type": "multipart/form-data",
},
baseUrl: release.data.upload_url,
release_id: release.data.id,
name: nameWithExt,
repo: context.repo.repo,
data: `@${filePath}`,
});
} catch (err: any) {
core.info(
`Problem uploading ${filePath} as a release asset (${err.message}). Will retry with the md5 hash appended to the filename.`,
);
const hash = await md5File(filePath);
const basename = path.basename(filePath, path.extname(filePath));
const ext = path.extname(filePath);
const newName = `${basename}-${hash}${ext}`;
await client.repos.uploadReleaseAsset({
owner: context.repo.owner,
headers: {
"X-GitHub-Api-Version": "2022-11-28",
"Content-Type": "multipart/form-data",
},
baseUrl: release.data.upload_url,
name: newName,
repo: context.repo.repo,
release_id: release.data.id,
data: `@${filePath}`,
});
}
}
}
core.endGroup();
};