website-staging-server/index.ts

68 lines
1.5 KiB
TypeScript

import { execSync } from "child_process";
import dotenv from "dotenv";
dotenv.config();
import Express from "express";
const app = Express();
const PORT = process.env.PORT;
const base64 = Buffer.from(
`${process.env.GITLAB_USER}:${process.env.PASS}`,
).toString("base64");
app.use(Express.static(__dirname + "/out"));
app.post("/", (req, res) => {
const header = req.header("authorization");
if (header == null) {
res.status(403).send("Authorization header is needed");
return;
}
if (header.split(" ").length !== 2) {
res.status(400).send("Authorization header is malformed");
return;
}
const [type, secret] = header.split(" ");
if (type !== "Basic") {
res.status(400).send("Only basic authorization is supported");
return;
}
if (secret !== base64) {
res.status(401).send("Incorrect username or password");
return;
}
updateWebsite();
res.send("Updating website ...");
});
app.listen(PORT, () => {
console.log(`👀 http://localhost:${PORT}`);
});
async function updateWebsite() {
const branch = "main";
const job = "staging";
const url = `https://git.uwaterloo.ca/csc/website/-/jobs/artifacts/${branch}/download?job=${job}`;
const zipPath = `zips/${branch}.zip`;
// Wait for pipeline to succeed and artifacts to be available
await sleep(120);
execSync(`wget -O ${zipPath} '${url}'`);
execSync(`yes All | unzip ${zipPath}`);
}
function sleep(seconds: number) {
return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
}