Allow build-only website CI from the development repository

This commit is contained in:
unimatrixgit 2026-11-28 10:35:34 +01:00
commit 220dd1b1dc
2 changed files with 51 additions and 7 deletions

View file

@ -1,7 +1,46 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
test('feature branch CI never invokes production deployment',()=>{
const workflow=readFileSync(new URL('../workflows/web-deploy.yml',import.meta.url),'utf8');
assert.match(workflow,/- name: Trigger deployment after successful CI\r?\n\s+if: github\.ref == 'refs\/heads\/main'/);
const workflow = readFileSync(new URL('../workflows/web-deploy.yml', import.meta.url), 'utf8');
const steps = new Map(
workflow.split(/^ - name: /m).slice(1).map(chunk => {
const lineEnd = chunk.indexOf('\n');
return [chunk.slice(0, lineEnd).trim(), chunk.slice(lineEnd + 1)];
})
);
const production = "github.repository == 'unimatrix/MC' && github.ref == 'refs/heads/main'";
function step(name) {
assert.ok(steps.has(name), 'Expected workflow step: ' + name);
return steps.get(name);
}
test('development can dispatch a build and production still builds on main pushes', () => {
assert.match(workflow, /^ workflow_dispatch:\s*$/m);
assert.match(workflow, /^ push:\r?\n branches:\r?\n - main\r?$/m);
assert.equal(workflow.match(/^ if: (.+)$/m)?.[1].trim(),
"github.repository == 'unimatrix/MC' || (github.repository == 'dev/MC' && github.event_name == 'workflow_dispatch')");
for (const name of ['Verify deployment policy', 'Build and test website']) {
assert.doesNotMatch(step(name), /^ if:/m);
}
});
test('checkout fetches the exact event commit from the repository running the workflow', () => {
const checkout = step('Check out exact revision');
assert.match(checkout, /fetch --depth=1 "https:\/\/www\.dunerealms\.com\/git\/\$\{GITHUB_REPOSITORY\}\.git" "\$GITHUB_SHA"/);
assert.match(checkout, /git checkout --detach FETCH_HEAD\r?\n\s+test "\$\(git rev-parse HEAD\)" = "\$GITHUB_SHA"/);
assert.doesNotMatch(checkout, /unimatrix\/MC/);
});
test('publication comparison, artifact upload and deployment require canonical main', () => {
for (const name of ['Prepare publication comparison', 'Upload website build', 'Trigger deployment after successful CI']) {
assert.equal(step(name).match(/^ if: (.+)$/m)?.[1].trim(), production);
}
assert.match(step('Prepare publication comparison'), /publication-changes\.mjs "\$BEFORE" "\$GITHUB_SHA"/);
assert.match(step('Upload website build'), /uses: actions\/upload-artifact@v3/);
assert.match(step('Trigger deployment after successful CI'), /StrictHostKeyChecking=yes[^\n]+"\$GITHUB_SHA"/);
for (const [name, body] of steps) {
if (name !== 'Trigger deployment after successful CI') assert.doesNotMatch(body, /DUNEREALMS_DEPLOY_KEY|ssh .*dunedeploy@/);
}
});

View file

@ -7,20 +7,24 @@ on:
paths:
- 'website/**'
- '.forgejo/workflows/web-deploy.yml'
- '.forgejo/tests/web-deploy.test.mjs'
workflow_dispatch:
jobs:
web:
if: github.repository == 'unimatrix/MC' || (github.repository == 'dev/MC' && github.event_name == 'workflow_dispatch')
runs-on: dune-ci
steps:
- name: Checkout
- name: Check out exact revision
shell: bash
env:
REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -euo pipefail
git init .
git -c http.extraheader="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$REPO_TOKEN" | base64 -w0)" fetch --depth=1 https://www.dunerealms.com/git/unimatrix/MC.git "$GITHUB_SHA"
git -c http.extraheader="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$REPO_TOKEN" | base64 -w0)" fetch --depth=1 "https://www.dunerealms.com/git/${GITHUB_REPOSITORY}.git" "$GITHUB_SHA"
git checkout --detach FETCH_HEAD
test "$(git rev-parse HEAD)" = "$GITHUB_SHA"
- name: Install Node 22
uses: actions/setup-node@v4
@ -28,6 +32,7 @@ jobs:
node-version: '22'
- name: Prepare publication comparison
if: github.repository == 'unimatrix/MC' && github.ref == 'refs/heads/main'
shell: bash
env:
REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@ -56,7 +61,7 @@ jobs:
node --test --test-concurrency=1 ../tools/tests/*.test.mjs
- name: Upload website build
if: github.ref == 'refs/heads/main'
if: github.repository == 'unimatrix/MC' && github.ref == 'refs/heads/main'
uses: actions/upload-artifact@v3
with:
name: DuneRealms-Website
@ -65,7 +70,7 @@ jobs:
retention-days: 1
- name: Trigger deployment after successful CI
if: github.ref == 'refs/heads/main'
if: github.repository == 'unimatrix/MC' && github.ref == 'refs/heads/main'
shell: bash
env:
DEPLOY_KEY: ${{ secrets.DUNEREALMS_DEPLOY_KEY }}