-115
cicd/index.js
-115
cicd/index.js
···
1
-
import z from "zod";
2
-
import path from "node:path";
3
-
import { promisify } from "node:util";
4
-
import { exec as callback_exec } from "node:child_process";
5
-
const exec = promisify(callback_exec);
6
-
7
-
/**
8
-
* manually translated from
9
-
* https://tangled.sh/@tangled.sh/core/blob/master/lexicons/git/refUpdate.json
10
-
* at commit ec0f9449c962ad6da54aeeb6e452b2bce0af7b35
11
-
* (https://tangled.sh/@tangled.sh/core/commit/ec0f9449c962ad6da54aeeb6e452b2bce0af7b35)
12
-
*/
13
-
const sh_tangled_git_refUpdate = z.object({
14
-
ref: z.string().max(2560),
15
-
commiterDid: z.string().regex(/^did:.+:.+$/),
16
-
repoDid: z.string().regex(/^did:.+:.+$/),
17
-
repoName: z.string(),
18
-
oldSha: z.string().length(40),
19
-
newSha: z.string().length(40),
20
-
meta: z.object({
21
-
isDefaultRef: z.boolean(),
22
-
langBreakdown: z
23
-
.object({
24
-
inputs: z
25
-
.array(
26
-
z.object({
27
-
lang: z.string(),
28
-
size: z.int(),
29
-
})
30
-
)
31
-
.optional(),
32
-
})
33
-
.optional(),
34
-
commitCount: z.object({
35
-
byEmail: z
36
-
.array(z.object({ email: z.email(), count: z.int() }))
37
-
.optional(),
38
-
}),
39
-
}),
40
-
});
41
-
42
-
const connect = () => {
43
-
const ws = new WebSocket("wss://knot1.tangled.sh/events");
44
-
ws.addEventListener("message", async (ev) => {
45
-
let data;
46
-
try {
47
-
data = sh_tangled_git_refUpdate.parse(JSON.parse(ev.data)?.event);
48
-
} catch {
49
-
console.log("Could not parse", ev.data);
50
-
return undefined;
51
-
}
52
-
53
-
console.log(data?.repoDid, data?.repoName, data);
54
-
55
-
// fail gracefully if not json
56
-
if (!data) return;
57
-
58
-
// now check if its to this repo
59
-
// assume it doesnt get renamed
60
-
// and assume my did is constant
61
-
// if this changes owner or name this needs updated
62
-
// if its not the right repo just quit out
63
-
if (
64
-
!(
65
-
data.repoDid === "did:plc:4zht3z4caxwrw3dlsybodywc" &&
66
-
data.repoName === "server-config"
67
-
)
68
-
)
69
-
return;
70
-
71
-
// this is the correct repo
72
-
// either code was changed (needing a rebuild)
73
-
// or a site was updated here
74
-
// either way we can stash, pull, docker, pop
75
-
const options = { cwd: path.join(process.cwd(), "..") };
76
-
await exec("git stash", options);
77
-
console.log("completed `git stash`");
78
-
await exec("git pull", options);
79
-
console.log("completed `git pull`");
80
-
await exec("docker compose up -d --build --force-recreate", options);
81
-
console.log("completed `docker compose up -d --build --force-recreate`");
82
-
await exec("git stash pop", options);
83
-
console.log("completed `git stash pop`");
84
-
// the repo should be updated now
85
-
});
86
-
87
-
ws.addEventListener("open", () => {
88
-
console.log("Connected to endpoint");
89
-
});
90
-
91
-
// when connection is closed or errors
92
-
// just connect again
93
-
ws.addEventListener("close", (ev) => {
94
-
console.log(
95
-
"Disconnected from endpoint because: ",
96
-
ev.reason,
97
-
"\nReconnecting..."
98
-
);
99
-
connect();
100
-
});
101
-
ws.addEventListener("error", (ev) => {
102
-
console.log(
103
-
"Disconnected from endpoint because: ",
104
-
ev,
105
-
"\nReconnecting..."
106
-
);
107
-
connect();
108
-
});
109
-
110
-
console.log("Set up ws:", ws, ws.readyState);
111
-
};
112
-
113
-
// launch connection and keep alive
114
-
connect();
115
-
while (true);
-17
cicd/package.json
-17
cicd/package.json
···
1
-
{
2
-
"name": "cicd",
3
-
"version": "1.0.0",
4
-
"description": "",
5
-
"type": "module",
6
-
"main": "index.js",
7
-
"scripts": {
8
-
"start": "node index.js"
9
-
},
10
-
"keywords": [],
11
-
"author": "",
12
-
"license": "ISC",
13
-
"packageManager": "pnpm@10.14.0",
14
-
"dependencies": {
15
-
"zod": "^4.1.5"
16
-
}
17
-
}
-22
cicd/pnpm-lock.yaml
-22
cicd/pnpm-lock.yaml
···
1
-
lockfileVersion: '9.0'
2
-
3
-
settings:
4
-
autoInstallPeers: true
5
-
excludeLinksFromLockfile: false
6
-
7
-
importers:
8
-
9
-
.:
10
-
dependencies:
11
-
zod:
12
-
specifier: ^4.1.5
13
-
version: 4.1.5
14
-
15
-
packages:
16
-
17
-
zod@4.1.5:
18
-
resolution: {integrity: sha512-rcUUZqlLJgBC33IT3PNMgsCq6TzLQEG/Ei/KTCU0PedSWRMAXoOUN+4t/0H+Q8bdnLPdqUYnvboJT0bn/229qg==}
19
-
20
-
snapshots:
21
-
22
-
zod@4.1.5: {}