-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
74 lines (66 loc) · 2.01 KB
/
index.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
const core = require('@actions/core');
const github = require('@actions/github');
const env = process.env;
const fs = require('fs');
async function getRelease(octokit, owner, repo, tag) {
try {
return await octokit.repos.getReleaseByTag({
owner,
repo,
tag,
});
} catch (error) {
if (error.status == 404) {
return null;
}
console.log(error);
}
}
async function createRelease(octokit, owner, repo, tag_name) {
return await octokit.repos.createRelease({
owner,
repo,
tag_name,
});
}
async function getUploadUrl(octokit, owner, repo, tag) {
var release = await getRelease(octokit, owner, repo, tag);
if (release === null) {
release = await createRelease(octokit, owner, repo, tag);
}
return release.data.upload_url;
}
async function uploadAsset(octokit, uploadUrl, artifactPath) {
const split = artifactPath.split("==>");
const data = fs.readFileSync(split[0].trim());
const name = split[1].trim();
try {
const url = uploadUrl + "?name=" + name;
await octokit.request({
method: "POST",
url: url,
headers: {
"content-type": "application/octet-stream"
},
data: data,
name: name
});
} catch (error) {
core.error(error);
core.setFailed(`Unable to upload release asset: ${artifactPath}`);
}
}
async function run() {
const token = core.getInput("GH_TOKEN");
const octokit = new github.GitHub(token);
const owner = env.GITHUB_REPOSITORY.split("/")[0];
const repo = env.GITHUB_REPOSITORY.split("/")[1];
const tag = core.getInput("TAG_NAME");
const artifacts = core.getInput("ARTIFACTS").split("\n");
const uploadUrl = await getUploadUrl(octokit, owner, repo, tag);
for (let i = 0; i < artifacts.length; i++) {
const artifact = artifacts[i];
await uploadAsset(octokit, uploadUrl, artifact);
}
}
run();