forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (42 loc) · 1.39 KB
/
index.js
File metadata and controls
55 lines (42 loc) · 1.39 KB
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
const fs = require('fs');
const path = require('path');
const github = require('@actions/github');
const core = require('@actions/core');
const GITHUB_TOKEN = process.env['GITHUB_TOKEN'];
const MSI_PATH = 'gh.msi';
process.on('unhandledRejection', function(reason, _) {
handleError(reason);
});
main().catch(handleError)
async function main() {
if (!fs.existsSync(MSI_PATH)) {
throw new Error(`Could not find MSI file at ${MSI_PATH}`);
}
const tag = github.context.ref.replace('refs/tags/', '');
const owner = github.context.repo.owner;
const repo = github.context.repo.repo;
const release = await getRelease(GITHUB_TOKEN, owner, repo, tag);
const assetFile = fs.readFileSync(MSI_PATH);
await uploadAsset(GITHUB_TOKEN, release, assetFile);
}
async function getRelease(token, owner, repo, tag) {
const octokit = new github.GitHub(token);
const response = await octokit.repos.getReleaseByTag({ owner, repo, tag });
return response.data;
}
async function uploadAsset(token, release, assetFile) {
const octokit = new github.GitHub(token);
await octokit.repos.uploadReleaseAsset({
url: release.upload_url,
file: assetFile,
name: path.basename(MSI_PATH),
headers: {
'content-type': 'application/octet-stream',
'content-length': fs.statSync(MSI_PATH).size,
}
});
}
async function handleError(err) {
console.error(err);
core.setFailed(err.message);
}