Skip to content

Commit 0f577e6

Browse files
committed
Add docs workflow to CI
1 parent 85bb62c commit 0f577e6

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

.github/workflows/docs.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Build Docs
2+
3+
on:
4+
push:
5+
branches: [master]
6+
7+
permissions:
8+
contents: write
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
- name: Set up environment
16+
uses: ./.github/actions/setup
17+
- run: bash scripts/git-user-config.sh
18+
- run: node scripts/update-docs-branch.js
19+
- run: git push --all origin .

docs/antora.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: community-contracts
22
title: Community Contracts
3-
version: 0.0.0-alpha.0
3+
version: 0.0.1
44
prerelease: true
55
nav:
66
- modules/ROOT/nav.adoc

scripts/git-user-config.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail -x
4+
5+
git config user.name 'github-actions'
6+
git config user.email '41898282+github-actions[bot]@users.noreply.github.com'

scripts/update-docs-branch.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const proc = require('child_process');
2+
const read = cmd => proc.execSync(cmd, { encoding: 'utf8' }).trim();
3+
const run = cmd => {
4+
proc.execSync(cmd, { stdio: 'inherit' });
5+
};
6+
const tryRead = cmd => {
7+
try {
8+
return read(cmd);
9+
} catch {
10+
return undefined;
11+
}
12+
};
13+
14+
// The community contracts currently don't have a defined release process.
15+
// The master branch is used for development, and the docs are updated from there.
16+
// Use /^release-v(?<version>(?<major>\d+)\.(?<minor>\d+)(?:\.(?<patch>\d+))?)$/ for a release branch
17+
const masterBranch = /^master$/;
18+
19+
const currentBranch = read('git rev-parse --abbrev-ref HEAD');
20+
const match = currentBranch.match(masterBranch);
21+
22+
if (!match) {
23+
console.error('Not currently on master branch');
24+
process.exit(1);
25+
}
26+
27+
const pkgVersion = require('../package.json').version;
28+
29+
if (pkgVersion.includes('-') && !pkgVersion.includes('.0.0-')) {
30+
console.error('Refusing to update docs: non-major prerelease detected');
31+
process.exit(0);
32+
}
33+
34+
const current = match.groups;
35+
const docsBranch = `docs-v${current?.major ?? pkgVersion.split('.')[0]}.x`;
36+
37+
// Fetch remotes and find the docs branch if it exists
38+
run('git fetch --all --no-tags');
39+
const matchingDocsBranches = tryRead(`git rev-parse --glob='*/${docsBranch}'`);
40+
41+
if (!matchingDocsBranches) {
42+
// Create the branch
43+
run(`git checkout --orphan ${docsBranch}`);
44+
} else {
45+
const [publishedRef, ...others] = new Set(matchingDocsBranches.split('\n'));
46+
if (others.length > 0) {
47+
console.error(
48+
`Found conflicting ${docsBranch} branches.\n` +
49+
'Either local branch is outdated or there are multiple matching remote branches.',
50+
);
51+
process.exit(1);
52+
}
53+
const publishedVersion = JSON.parse(read(`git show ${publishedRef}:package.json`)).version;
54+
const publishedMinor = publishedVersion.match(/\d+\.(?<minor>\d+)\.\d+/).groups.minor;
55+
if (current.minor < publishedMinor) {
56+
console.error('Refusing to update docs: newer version is published');
57+
process.exit(0);
58+
}
59+
60+
run('git checkout --quiet --detach');
61+
run(`git reset --soft ${publishedRef}`);
62+
run(`git checkout ${docsBranch}`);
63+
}
64+
65+
run('npm run prepare-docs');
66+
run('git add -f docs'); // --force needed because generated docs files are gitignored
67+
run('git commit -m "Update docs"');
68+
run(`git checkout ${currentBranch}`);

0 commit comments

Comments
 (0)