diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 2bc8972..10a9a64 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -6,17 +6,21 @@ name: Release to GitHub Pages # tagged v. on: release: - types: [published] + # "released" also fires when a pre-release is promoted to a full release. + types: [released] workflow_dispatch: inputs: tag: description: Existing release tag to redeploy (e.g. v1.0.0) required: true + allow_older: + description: Allow deploying a version older than what is live (rollback) + type: boolean + default: false +# Least privilege: only the deploy job can write to Pages. permissions: contents: read - pages: write - id-token: write concurrency: group: pages @@ -29,6 +33,8 @@ jobs: runs-on: ubuntu-latest env: TAG: ${{ github.event.release.tag_name || inputs.tag }} + ALLOW_OLDER: ${{ inputs.allow_older || false }} + GH_TOKEN: ${{ github.token }} steps: - uses: actions/checkout@v7 with: @@ -45,10 +51,21 @@ jobs: echo "::error::Release tag $TAG does not match package.json version ($want)" exit 1 fi + - name: Never deploy pre-releases or roll the live site back by accident + run: | + set -euo pipefail + if [ "$(gh release view "$TAG" --repo "$GITHUB_REPOSITORY" --json isPrerelease -q .isPrerelease)" = "true" ]; then + echo "::error::$TAG is a pre-release; pre-releases never deploy"; exit 1 + fi + live=$(curl -sSf https://seriesoftub.es/version.json 2>/dev/null | jq -r .version || echo "0.0.0") + new="${TAG#v}" + if [ "$ALLOW_OLDER" != "true" ] && [ "$(printf '%s\n%s\n' "$live" "$new" | sort -V | tail -n1)" != "$new" ]; then + echo "::error::$new is older than the live $live. Re-run with allow_older to roll back on purpose."; exit 1 + fi - run: npm ci - run: npm test - name: Build site - run: VERSION="${TAG#v}" npm run build + run: VERSION="${TAG#v}" REQUIRE_NOTES=1 npm run build - uses: actions/upload-pages-artifact@v5 with: path: ./_site @@ -56,6 +73,9 @@ jobs: deploy: needs: build runs-on: ubuntu-latest + permissions: + pages: write + id-token: write environment: name: github-pages url: ${{ steps.deployment.outputs.page_url }} diff --git a/README.md b/README.md index 8dcada4..46104b1 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ site/ everything that is deployed robots.txt, sitemap.xml, llms.txt, 404.html, CNAME scripts/build.sh copies site/ to _site/ and stamps __VERSION__ / __BUILD_DATE__ tests/ node:test + jsdom -.github/workflows/ ci.yml (every push) and release.yml (Pages deploy) +.github/workflows/ ci.yml (pushes to main and PRs) and release.yml (Pages deploy) ``` ## Releasing @@ -65,13 +65,18 @@ Only published releases reach [seriesoftub.es](https://seriesoftub.es). Pushes t 3. Publish a GitHub release tagged `v`, e.g. `gh release create v1.1.0 --generate-notes`. The release workflow checks that the tag matches `package.json`, runs the tests, -builds, deploys to GitHub Pages and pings IndexNow. Pre-releases never deploy. +builds, deploys to GitHub Pages and pings IndexNow. It refuses to build when the +CHANGELOG has no `## []` section (rename `[Unreleased]` first). +Pre-releases never deploy, also not when run by hand, and a version older than +the live one is refused unless you tick `allow_older` (a deliberate rollback). To redeploy an existing release, run the workflow manually with its tag. ## Hosting setup GitHub Pages serves the site with **Source: GitHub Actions** and the custom domain -`seriesoftub.es`. DNS for the apex domain: +`seriesoftub.es`. Releases deploy from tags, so in **Settings → Environments → +github-pages → Deployment branches and tags**, add a tag rule for `v*` (by default +only the default branch may deploy). DNS for the apex domain: ``` seriesoftub.es. A 185.199.108.153 diff --git a/scripts/build.sh b/scripts/build.sh index 15acf88..7f9f7d4 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -1,15 +1,22 @@ #!/usr/bin/env bash -# Builds the deployable site into _site/: copies site/ and stamps the version -# (from package.json) and the build date into the pages that carry them. +# Builds the deployable site into _site/ (or $OUT): copies site/ and stamps the +# version (from package.json) and the build date into the pages that carry them. +# Set REQUIRE_NOTES=1 to fail when CHANGELOG.md has no bullets for this version. set -euo pipefail cd "$(dirname "$0")/.." VERSION="${VERSION:-$(node -p "require('./package.json').version")}" BUILD_DATE="${BUILD_DATE:-$(date -u +%Y-%m-%d)}" +OUT="${OUT:-_site}" -rm -rf _site -cp -R site _site -for f in _site/index.html _site/sitemap.xml _site/llms.txt; do +if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+([-.+][0-9A-Za-z.-]+)?$ ]]; then + echo "error: VERSION '$VERSION' is not a semantic version" >&2 + exit 1 +fi + +rm -rf "$OUT" +cp -R site "$OUT" +for f in "$OUT/index.html" "$OUT/sitemap.xml" "$OUT/llms.txt"; do sed -i.bak -e "s/__VERSION__/${VERSION}/g" -e "s/__BUILD_DATE__/${BUILD_DATE}/g" "$f" rm -f "$f.bak" done @@ -17,15 +24,21 @@ done # Release notes for this version, baked into the page ("What's new") and # published as version.json so open games can spot a newer release. NOTES="$(node scripts/release-notes.js "$VERSION")" -node -e ' - const fs=require("fs"),[,v,d,notes]=process.argv; - const f="_site/index.html"; - fs.writeFileSync(f,fs.readFileSync(f,"utf8").replace("/*__RELEASE_NOTES__*/[]",notes)); - fs.writeFileSync("_site/version.json",JSON.stringify({version:v,date:d,notes:JSON.parse(notes)},null,2)+"\n"); -' "$VERSION" "$BUILD_DATE" "$NOTES" - -if grep -rl "__VERSION__\|__BUILD_DATE__" _site >/dev/null; then - echo "error: unreplaced placeholders left in _site" >&2 +if [ "${REQUIRE_NOTES:-}" = "1" ] && [ "$NOTES" = "[]" ]; then + echo "error: CHANGELOG.md has no '## [$VERSION]' section with bullets (still under [Unreleased]?)" >&2 exit 1 fi -echo "Built Series of Tubes ${VERSION} (${BUILD_DATE}) into _site/" +node -e ' + const fs=require("fs"),[,out,v,d,notes]=process.argv; + const f=out+"/index.html",ph="/*__RELEASE_NOTES__*/[]",html=fs.readFileSync(f,"utf8"); + if(!html.includes(ph)){console.error("error: release-notes placeholder missing from index.html");process.exit(1);} + // Function replacer: no $& / $$ expansion. Escape "<" so a note can never close the