-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (63 loc) · 2.1 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
const core = require("@actions/core");
const tc = require("@actions/tool-cache");
const https = require("https");
const { join } = require("path");
async function run() {
try {
// Get the version input or the latest release
let version = core.getInput("version") || "latest";
// Get the download URL for the release
const releaseUrl = `https://api.github.com/repos/apppackio/apppack/releases/${version}`;
const data = await downloadJson(releaseUrl);
// strip the leading "v" from the tag name
version = data.tag_name.slice(1);
// Determine the platform-specific asset name
const arch = process.arch === "x64" ? "x86_64" : process.arch;
const platform =
process.platform.charAt(0).toUpperCase() +
process.platform.slice(1) +
"_" +
arch;
const assetName = `apppack_${version}_${platform}.tar.gz`;
const asset = data.assets.find((a) => a.name === assetName);
if (!asset) {
throw new Error(
`Could not find AppPack CLI asset in release ${data.tag_name}`
);
}
// Download and cache the AppPack CLI tool
const downloadUrl = asset.browser_download_url;
const pathToTarball = await tc.downloadTool(downloadUrl);
const tmpDir = process.env.RUNNER_TEMP || os.tmpdir();
const pathToCLI = await tc.extractTar(pathToTarball, tmpDir);
// Cache the downloaded tool
const toolPath = await tc.cacheFile(
join(pathToCLI, "apppack"),
"apppack",
"apppack",
data.tag_name
);
// Add the AppPack CLI directory to the PATH
core.addPath(toolPath);
// Set the version output
core.setOutput("version", version);
} catch (error) {
core.setFailed(error.message);
}
}
// Download a JSON file from a URL
async function downloadJson(url) {
return new Promise((resolve, reject) => {
const req = https.get(
url,
{ headers: { "User-Agent": "apppackio/setup-apppack" } },
(res) => {
let data = "";
res.on("data", (chunk) => (data += chunk));
res.on("end", () => resolve(JSON.parse(data)));
}
);
req.on("error", reject);
});
}
run();