This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
forked from namespacelabs/nscloud-setup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
101 lines (82 loc) · 2.31 KB
/
main.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
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
import * as core from "@actions/core";
import * as tc from "@actions/tool-cache";
import * as exec from "@actions/exec";
import * as fs from "fs";
import * as path from "path";
async function run(): Promise<void> {
try {
await core.group(`Prepare access to Namespace`, async () => {
await installNsc();
await exec.exec("nsc version");
});
const registry = await core.group(`Log into Namespace workspace`, async () => {
await ensureNscloudToken();
return await dockerLogin();
});
await core.group(`Registry address`, async () => {
core.info(registry);
core.setOutput("registry-address", registry);
});
} catch (e) {
core.setFailed(e.message);
}
}
async function installNsc() {
// Download the specific version of the tool, e.g. as a tarball
const pathToTarball = await tc.downloadTool(getDownloadURL(), null, null, {
CI: process.env.CI,
"User-Agent": "nscloud-action",
});
// Extract the tarball onto the runner
const pathToCLI = await tc.extractTar(pathToTarball);
// Expose the tool by adding it to the PATH
core.addPath(pathToCLI);
core.exportVariable("NS_DO_NOT_UPDATE", "true");
}
function getDownloadURL(): string {
const { RUNNER_ARCH, RUNNER_OS } = process.env;
let arch = "";
switch (RUNNER_ARCH) {
case "X64":
arch = "amd64";
break;
case "ARM64":
arch = "arm64";
break;
default:
throw new Error(`Unsupported architecture: ${RUNNER_ARCH}`);
}
let os = "";
switch (RUNNER_OS) {
case "macOS":
os = "darwin";
break;
case "Linux":
os = "linux";
break;
default:
throw new Error(`Unsupported operating system: ${RUNNER_OS}`);
}
return `https://get.namespace.so/packages/nsc/latest?arch=${arch}&os=${os}`;
}
async function ensureNscloudToken() {
const tokenFile = "/var/run/nsc/token.json";
if (fs.existsSync(tokenFile)) {
core.exportVariable("NSC_TOKEN_FILE", tokenFile);
return;
}
await exec.exec("nsc auth exchange-github-token");
}
async function dockerLogin() {
const out = tmpFile("registry.txt");
await exec.exec(`nsc docker login --output_registry_to=${out} --log_actions=false`);
return fs.readFileSync(out, "utf8");
}
export function tmpFile(file: string): string {
const tmpDir = path.join(process.env.RUNNER_TEMP, "ns");
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir);
}
return path.join(tmpDir, file);
}
run();