blog.git

blog

~ Personal research notes. blog.sqzer.com
git clone https://github.com/debuman/blog.git

scripts/build-info.mjs (1353B)

      1 /**
      2  * build-info.mjs — lets the site state its own provenance.
      3  *
      4  * "Is my commit live yet?" is answered by the site itself rather than by the pipeline that
      5  * deployed it: every build publishes /build-info.json with the commit it was built from and
      6  * when, so the answer is whatever the live site serves.
      7  *
      8  * Content and code share one repository, so contentSha and appSha name the same commit.
      9  * CONTENT_DIR and CONTENT_SHA can still override the content side; nothing in this
     10  * repository or its workflow sets them.
     11  */
     12 import { writeFile, mkdir } from 'node:fs/promises';
     13 import { execFileSync } from 'node:child_process';
     14 
     15 const SRC = process.env.CONTENT_DIR ?? '.';
     16 const git = (args, cwd) => {
     17   try { return execFileSync('git', args, { cwd, encoding: 'utf8', stdio: ['ignore','pipe','ignore'] }).trim(); }
     18   catch { return null; }
     19 };
     20 const info = {
     21   contentSha: process.env.CONTENT_SHA ?? git(['rev-parse', 'HEAD'], SRC),
     22   contentRef: git(['rev-parse', '--abbrev-ref', 'HEAD'], SRC),
     23   appSha: process.env.GITHUB_SHA ?? git(['rev-parse', 'HEAD'], '.'),
     24   builtAt: new Date().toISOString(),
     25 };
     26 await mkdir('public', { recursive: true });
     27 await writeFile('public/build-info.json', JSON.stringify(info, null, 1) + '\n');
     28 console.log(`build-info: content ${String(info.contentSha).slice(0, 8)} / app ${String(info.appSha).slice(0, 8)}`);