Build the website on server side

This commit is contained in:
Aditya Thakral 2021-07-20 05:54:41 -04:00
parent 2d884595df
commit f3b0472a4a
1 changed files with 39 additions and 20 deletions

View File

@ -1,19 +1,18 @@
import { execSync } from "child_process"; import { execSync } from "child_process";
import path from "path";
import dotenv from "dotenv"; import dotenv from "dotenv";
import Express from "express";
dotenv.config(); dotenv.config();
import Express from "express";
const app = Express(); const app = Express();
const PORT = process.env.PORT; const PORT = process.env.PORT;
const base64 = Buffer.from( const base64 = Buffer.from(
`${process.env.GITLAB_USER}:${process.env.PASS}`, `${process.env.GITEA_USER}:${process.env.PASS}`,
).toString("base64"); ).toString("base64");
app.use(Express.static(__dirname + "/out"));
app.post("/", (req, res) => { app.post("/", (req, res) => {
const header = req.header("authorization"); const header = req.header("authorization");
@ -39,29 +38,49 @@ app.post("/", (req, res) => {
return; return;
} }
updateWebsite(); updateWebsite("main");
res.send("Updating website ..."); res.send("Updated!");
}); });
app.listen(PORT, () => { app.listen(PORT, () => {
console.log(`👀 http://localhost:${PORT}`); console.log(`👀 http://localhost:${PORT}`);
}); });
async function updateWebsite() { const USER = process.env.USER!;
const branch = "main"; const REPO_NAME = "www-new";
const job = "staging"; const URL = `https://git.csclub.uwaterloo.ca/www/${REPO_NAME}.git`;
const url = `https://git.uwaterloo.ca/csc/website/-/jobs/artifacts/${branch}/download?job=${job}`; const WWW_DIR = process.env.WWW_DIR ?? "~/www/csc";
const $ = (cmd: string, cwd?: string) => {
console.log(cwd ? `${cwd} >` : ">", cmd);
const zipPath = `zips/${branch}.zip`; const out = execSync(cmd, { cwd, encoding: "utf8" });
console.log(out);
// Wait for pipeline to succeed and artifacts to be available return out;
await sleep(120); };
execSync(`wget -O ${zipPath} '${url}'`); // Makes sure that the directory is present
execSync(`yes All | unzip ${zipPath}`); $(`mkdir -p ${WWW_DIR}`);
}
function updateWebsite(branch: string) {
function sleep(seconds: number) { const tmpDir = $("mktemp --directory").trim();
return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
$(`git clone ${URL}`, tmpDir);
const repo_$ = (cmd: string) => $(cmd, path.join(tmpDir, REPO_NAME));
repo_$(`git checkout ${branch}`);
repo_$(`npm install`);
repo_$(`NEXT_PUBLIC_BASE_PATH="/~${USER}/csc/${branch}" npm run build`);
repo_$(`npm run export`);
const finalDir = path.join(WWW_DIR, branch);
$(`rm -rf ${finalDir}`);
$(`mkdir -p ${finalDir}`);
repo_$(`mv ./out/* ${finalDir}`);
$(`rm -rf ${tmpDir}`);
} }