-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.ts
110 lines (93 loc) · 3.17 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
102
103
104
105
106
107
108
109
110
import * as core from "@actions/core";
import * as exec from "@actions/exec";
import * as fs from "node:fs";
import { nscRemoteBuilderName, nscDebugFolder, nscVmIdKey } from "./common";
async function run(): Promise<void> {
const commandExists = require("command-exists");
commandExists("nsc")
.then(prepareBuildx)
.catch(() => {
core.setFailed(`Namespace Cloud CLI not found.
Please add a step this step to your workflow's job definition:
- uses: namespacelabs/nscloud-setup@v0`);
});
}
async function prepareBuildx(): Promise<void> {
try {
const exists = await core.group(
"Check if Namespace Builder proxy is already configured",
async (): Promise<boolean> => {
const builderExists = await remoteNscBuilderExists();
if (builderExists) {
core.info(
"GitHub runner is already configured to use Namespace Cloud build cluster."
);
return true;
}
core.info("Namespace Builder is not yet configured.");
return false;
}
);
if (!exists) {
await core.group("Proxy Buildkit from Namespace Cloud", async () => {
await ensureNscloudToken();
const loadToDockerFlag = parseInputLoadToDocker() ? "--default_load" : "";
const nscRunner = await isNscRunner();
if (nscRunner) {
core.debug("Environment is Namespace Runner");
await exec.exec(
`nsc docker buildx setup --name=${nscRemoteBuilderName} --background --use ${loadToDockerFlag} --background_debug_dir=${nscDebugFolder}`
);
} else {
core.debug("Environment is not Namespace Runner");
await exec.exec(
`nsc docker buildx setup --name=${nscRemoteBuilderName} --background --use ${loadToDockerFlag}`
);
}
});
}
await core.group("Builder", async () => {
core.info(nscRemoteBuilderName);
});
// New line to separate from groups.
core.info(`
Configured buildx to use remote Namespace Cloud build cluster.`);
} catch (error) {
core.setFailed(error.message);
}
}
function parseInputLoadToDocker(): boolean {
const loadToDockerString = (
core.getInput('load-to-docker') || ''
).toUpperCase()
core.debug(`load-to-docker = ${loadToDockerString}`)
if (loadToDockerString === 'TRUE') {
return true
}
return false
}
async function ensureNscloudToken() {
const tokenFile = "/var/run/nsc/token.json";
if (fs.existsSync(tokenFile)) {
core.exportVariable("NSC_TOKEN_FILE", tokenFile);
return;
}
// We only need a valid token when opening the proxy
await exec.exec("nsc auth exchange-github-token --ensure=5m");
}
async function remoteNscBuilderExists(): Promise<boolean> {
const { stdout, stderr } = await exec.getExecOutput(
`docker buildx inspect ${nscRemoteBuilderName}`,
null,
{ ignoreReturnCode: true, silent: true }
);
const builderNotFoundStr = `no builder "${nscRemoteBuilderName}" found`;
return !(
stdout.includes(builderNotFoundStr) || stderr.includes(builderNotFoundStr)
);
}
async function isNscRunner(): Promise<boolean> {
const vmID: string = process.env[`${nscVmIdKey}`] || "";
return vmID !== "";
}
run();