Skip to content

Commit 2b9c42d

Browse files
authored
Merge pull request #345 from cakephp/improve/docs-build-reliability
Improve documentation build reliability, release caching, and fallback routing
2 parents c97f796 + 6b76b03 commit 2b9c42d

22 files changed

Lines changed: 566 additions & 253 deletions

‎.github/workflows/ci.yml‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ jobs:
1818
- name: Build Dockerfile
1919
run: docker build .
2020

21+
- name: Verify nginx redirects
22+
run: docker run --rm -v "$PWD:/app:ro" -w /app nginx:alpine sh tests/nginx.sh
23+
2124
cs-stan:
2225
name: Coding Standard
2326
runs-on: ubuntu-latest
@@ -50,8 +53,12 @@ jobs:
5053
- name: Composer install
5154
run: composer install
5255

53-
- name: Run loader regression checks
54-
run: php tests/loader.php
56+
- name: Run regression checks
57+
run: |
58+
php tests/loader.php
59+
php tests/generator.php
60+
php tests/releases.php
61+
sh tests/build-release.sh
5562
5663
- name: Run PHP CodeSniffer
5764
run: vendor/bin/phpcs -q --report=checkstyle src/ | cs2pr

‎Dockerfile‎

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# Build api docs with php 8.1 requirements
1+
# syntax=docker/dockerfile:1
2+
# Build API docs with PHP 8.2
23
FROM alpine:3.19 AS builder
34

45
RUN apk add --no-cache \
@@ -7,7 +8,7 @@ RUN apk add --no-cache \
78
git \
89
make \
910
openssh-client \
10-
php81 \
11+
php82 \
1112
php82-bz2 \
1213
php82-curl \
1314
php82-dom \
@@ -24,29 +25,23 @@ RUN apk add --no-cache \
2425

2526
RUN ln -sf /usr/bin/php82 /usr/bin/php
2627

27-
RUN mkdir /root/.ssh \
28-
&& ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts
29-
3028
WORKDIR /data
31-
COPY . /data
3229

33-
RUN git clone https://github.com/cakephp/cakephp.git /cakephp \
34-
&& git clone https://github.com/cakephp/authentication.git /authentication \
35-
&& git clone https://github.com/cakephp/authorization.git /authorization \
36-
&& git clone https://github.com/cakephp/chronos.git /chronos \
37-
&& git clone https://github.com/cakephp/elastic-search.git /elastic \
38-
&& git clone https://github.com/cakephp/queue.git /queue
30+
# Install generator dependencies independently of source, template and asset edits.
31+
COPY composer.json composer.lock ./
32+
RUN --mount=type=cache,target=/root/.composer/cache \
33+
curl -sS https://getcomposer.org/installer | php \
34+
&& php composer.phar install --no-interaction --prefer-dist
35+
36+
COPY . /data
3937

40-
RUN ls -lah \
41-
&& make build-cakephp3-all CAKEPHP_SOURCE_DIR=/cakephp \
42-
&& make build-cakephp4-all CAKEPHP_SOURCE_DIR=/cakephp \
43-
&& make build-cakephp5-all CAKEPHP_SOURCE_DIR=/cakephp \
44-
&& make build-cakephp6-all CAKEPHP_SOURCE_DIR=/cakephp \
45-
&& make build-authentication-all AUTHENTICATION_SOURCE_DIR=/authentication \
46-
&& make build-authorization-all AUTHORIZATION_SOURCE_DIR=/authorization \
47-
&& make build-chronos-all CHRONOS_SOURCE_DIR=/chronos \
48-
&& make build-elastic-all ELASTIC_SOURCE_DIR=/elastic \
49-
&& make build-queue-all QUEUE_SOURCE_DIR=/queue
38+
# Change DOCS_CACHE_REFRESH to re-resolve dependencies even for unchanged source refs.
39+
ARG DOCS_CACHE_REFRESH=
40+
ENV DOCS_CACHE_DIR=/cache/docs
41+
RUN --mount=type=cache,target=/sources,sharing=locked \
42+
--mount=type=cache,target=/cache/docs,sharing=locked \
43+
--mount=type=cache,target=/root/.composer/cache,sharing=locked \
44+
DOCS_CACHE_REFRESH="$DOCS_CACHE_REFRESH" sh bin/build-all.sh
5045

5146
# nginx server
5247
FROM alpine:3.19

‎Makefile‎

Lines changed: 32 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -9,65 +9,36 @@ BUILD_DIR ?= ./build/api
99
DEPLOY_DIR ?= ./website
1010

1111
PHP ?= php
12+
DOCS_CACHE_DIR ?=
13+
export DOCS_CACHE_REFRESH
1214
COMPOSER := $(CURDIR)/composer.phar
1315
COMPOSER_UPDATE_FLAGS := --no-plugins --no-audit --no-security-blocking --ignore-platform-reqs
1416

1517
.DEFAULT_GOAL := help
1618

17-
CAKEPHP3_RELEASES := \
18-
3.0|3.0.19|no-vendor \
19-
3.1|3.1.14|no-vendor \
20-
3.2|3.2.14|update \
21-
3.3|3.3.16|update \
22-
3.4|3.4.14|update \
23-
3.5|3.5.18|update \
24-
3.6|3.6.15|update \
25-
3.7|3.7.9|update \
26-
3.8|3.8.13|update \
27-
3.9|3.9.10|update \
28-
3.10|origin/3.x|update
29-
30-
CAKEPHP4_RELEASES := \
31-
4.0|4.0.9|update \
32-
4.1|4.1.7|update \
33-
4.2|4.2.10|update \
34-
4.3|4.3.10|update \
35-
4.4|4.4.18|update \
36-
4.5|4.5.10|update \
37-
4.6|origin/4.x|update
38-
39-
CAKEPHP5_RELEASES := \
40-
5.0|5.0.11|update \
41-
5.1|5.1.9|update \
42-
5.2|5.2.15|update \
43-
5.3|5.3.7|update \
44-
5.4|origin/5.x|update
45-
46-
CAKEPHP6_RELEASES := \
47-
6.0|origin/6.x|update
48-
49-
CHRONOS_RELEASES := \
50-
1.x|origin/1.x|update \
51-
2.x|origin/2.x|update \
52-
3.x|origin/3.x|update
53-
54-
ELASTIC_RELEASES := \
55-
2.x|origin/2.x|update \
56-
3.x|origin/3.x|update \
57-
4.x|origin/4.x|update
58-
59-
QUEUE_RELEASES := \
60-
1.x|origin/1.x|update \
61-
2.x|origin/2.x|update
62-
63-
AUTHENTICATION_RELEASES := \
64-
2.x|origin/2.x|update \
65-
3.x|origin/3.x|update \
66-
4.x|origin/4.x|update
67-
68-
AUTHORIZATION_RELEASES := \
69-
2.x|origin/2.x|update \
70-
3.x|origin/3.x|update
19+
CAKEPHP3_RELEASES := $(shell $(PHP) bin/releases.php cakephp3)
20+
21+
CAKEPHP4_RELEASES := $(shell $(PHP) bin/releases.php cakephp4)
22+
23+
CAKEPHP5_RELEASES := $(shell $(PHP) bin/releases.php cakephp5)
24+
25+
CAKEPHP6_RELEASES := $(shell $(PHP) bin/releases.php cakephp6)
26+
27+
CHRONOS_RELEASES := $(shell $(PHP) bin/releases.php chronos)
28+
29+
ELASTIC_RELEASES := $(shell $(PHP) bin/releases.php elastic)
30+
31+
QUEUE_RELEASES := $(shell $(PHP) bin/releases.php queue)
32+
33+
AUTHENTICATION_RELEASES := $(shell $(PHP) bin/releases.php authentication)
34+
35+
AUTHORIZATION_RELEASES := $(shell $(PHP) bin/releases.php authorization)
36+
37+
RELEASE_GROUPS := CAKEPHP3 CAKEPHP4 CAKEPHP5 CAKEPHP6 CHRONOS ELASTIC QUEUE AUTHENTICATION AUTHORIZATION
38+
$(foreach group,$(RELEASE_GROUPS),$(if $($(group)_RELEASES),,$(error Unable to load $(group) releases; check PHP and config/releases.php)))
39+
40+
# Releases share source checkouts; concurrent builds would race on git checkout.
41+
.NOTPARALLEL:
7142

7243
release_version = $(word 1,$(subst |, ,$1))
7344
release_tag = $(word 2,$(subst |, ,$1))
@@ -134,6 +105,8 @@ help:
134105
@echo " AUTHENTICATION_SOURCE_DIR Location of the authentication clone."
135106
@echo " AUTHORIZATION_SOURCE_DIR Location of the authorization clone."
136107
@echo " BUILD_DIR Output directory. Default: $(BUILD_DIR)"
108+
@echo " DOCS_CACHE_DIR Optional cache directory for completed release builds."
109+
@echo " DOCS_CACHE_REFRESH Change this value to refresh cached dependency resolution."
137110
@echo " DEPLOY_DIR Deployment directory. Default: $(DEPLOY_DIR)"
138111
@echo ""
139112
@echo "NOTE: Source directories will have their checkout branch changed."
@@ -158,18 +131,15 @@ deploy: $(DEPLOY_DIR)
158131
composer.phar:
159132
curl -sS https://getcomposer.org/installer | php
160133

161-
install: composer.phar
134+
install: vendor/autoload.php
135+
136+
vendor/autoload.php: composer.json composer.lock composer.phar
162137
$(PHP) $(COMPOSER) install
163138

164139
define build_docs
165140
build-$(1)-$(5): install
166-
cd $($(2)) && git checkout -f $(6)
167-
$(if $(filter no-vendor,$(7)),cd $($(2)) && rm -rf ./vendor,cd $($(2)) && $(PHP) $(COMPOSER) update $(COMPOSER_UPDATE_FLAGS))
168-
mkdir -p $(BUILD_DIR)/$(3)/$(5)
169-
cp -r static/assets/* $(BUILD_DIR)/$(3)/$(5)
170-
171-
$(PHP) bin/apitool.php generate --config $(4) --version $(5) --tag $(6) \
172-
--output-dir $(BUILD_DIR)/$(3)/$(5) $($(2))
141+
sh bin/build-release.sh "$(PHP)" "$(COMPOSER)" "$($(2))" "$(BUILD_DIR)/$(3)/$(5)" \
142+
"$(4)" "$(5)" "$(6)" "$(7)" "$(COMPOSER_UPDATE_FLAGS)" "$(DOCS_CACHE_DIR)"
173143
endef
174144

175145
build-cakephp3-all: $(foreach version,$(CAKEPHP3_VERSIONS),build-cakephp-$(version))

‎README.md‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,57 @@ mkdir -p build/api/cakephp/6.0
4242
cp -r static/assets/* build/api/cakephp/6.0/
4343
php bin/apitool.php generate --config cakephp6 --version 6.0 --tag origin/6.x --output-dir build/api/cakephp/6.0/ ../cakephp
4444
```
45+
46+
### Release definitions and cached builds
47+
48+
`config/releases.php` defines the versions, Git refs and dependency modes used by
49+
Make targets. All CakePHP configurations derive their version selector from this
50+
manifest; legacy 1.x and 2.x links are retained in `config/cakephp-versions.php`.
51+
PHP is required when reading the Makefile, including for `make help`.
52+
53+
Docker uses BuildKit caches for source clones, Composer downloads and completed
54+
release documentation. Generator dependencies are installed before application
55+
sources are copied, so template edits reuse the Composer layer. Rendered releases
56+
are keyed by the source commit, generator code, templates, assets, configuration,
57+
Composer lock file, PHP major/minor version and dependency update flags. Only
58+
successful builds are cached, and a complete output directory replaces the old one.
59+
60+
Local Make builds can opt into the same release cache:
61+
62+
```sh
63+
make build-cakephp-6.0 DOCS_CACHE_DIR="$HOME/.cache/cakephp-api-docs"
64+
```
65+
66+
Cached documentation retains its resolved source dependencies. To re-resolve them
67+
without changing the source ref, set a new refresh value:
68+
69+
```sh
70+
make build-cakephp-6.0 DOCS_CACHE_DIR="$HOME/.cache/cakephp-api-docs" DOCS_CACHE_REFRESH=2026-11-29
71+
# Also invalidates Docker's build step so remote refs are fetched again:
72+
docker build --build-arg DOCS_CACHE_REFRESH=2026-11-29 .
73+
```
74+
75+
BuildKit caches are local to the builder. A fresh CI runner performs a full build
76+
unless its builder cache is persisted. Changing templates or shared version links
77+
correctly regenerates all affected release pages. README and test edits do not
78+
change the rendered-release cache keys.
79+
80+
### Public URL routing
81+
82+
The `site-api` proxy forwards public `/6.0/...` requests to this image's internal
83+
`/cakephp/6.0/...` path. Missing HTML pages redirect to the public `/6.0/` overview,
84+
with a relative Location header so the internal prefix and upstream host do not
85+
leak into public URLs. Missing assets retain a 404 response.
86+
87+
### Regression checks
88+
89+
After installing Composer dependencies:
90+
91+
```sh
92+
php tests/loader.php
93+
php tests/generator.php
94+
php tests/releases.php
95+
sh tests/build-release.sh
96+
# Runs nginx and HTTP requests inside a disposable container:
97+
docker run --rm -v "$PWD:/app:ro" -w /app nginx:alpine sh tests/nginx.sh
98+
```

‎bin/build-all.sh‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
# Source and rendered-documentation directories are Docker BuildKit cache mounts.
5+
for project in cakephp authentication authorization chronos elastic-search queue; do
6+
source_dir="/sources/$project"
7+
if [ ! -d "$source_dir/.git" ]; then
8+
git clone "https://github.com/cakephp/$project.git" "$source_dir"
9+
else
10+
git -C "$source_dir" fetch --prune --tags origin
11+
fi
12+
case "$project" in
13+
cakephp)
14+
for major in 3 4 5 6; do
15+
make "build-cakephp$major-all" CAKEPHP_SOURCE_DIR="$source_dir"
16+
done
17+
;;
18+
elastic-search) make build-elastic-all ELASTIC_SOURCE_DIR="$source_dir" ;;
19+
*)
20+
variable=$(printf '%s_SOURCE_DIR' "$project" | tr '[:lower:]' '[:upper:]')
21+
make "build-$project-all" "$variable=$source_dir"
22+
;;
23+
esac
24+
done

‎bin/build-release.sh‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
php=$1
5+
composer=$2
6+
source_dir=$(cd "$3" && pwd -P)
7+
output=$4
8+
config=$5
9+
version=$6
10+
tag=$7
11+
mode=$8
12+
update_flags=$9
13+
cache_dir=${10}
14+
15+
commit=$(git -C "$source_dir" rev-parse "$tag^{commit}")
16+
cache_entry=
17+
if [ -n "$cache_dir" ]; then
18+
mkdir -p "$cache_dir"
19+
cache_dir=$(cd "$cache_dir" && pwd -P)
20+
fingerprint=$("$php" bin/cache-key.php "$commit" "$config" "$version" "$mode" "$update_flags" "${DOCS_CACHE_REFRESH:-}")
21+
cache_entry="$cache_dir/$config-$version-$fingerprint"
22+
fi
23+
24+
mkdir -p "$(dirname "$output")"
25+
stage=$(mktemp -d "$(dirname "$output")/.docs-XXXXXX")
26+
trap 'rm -rf "$stage"' EXIT HUP INT TERM
27+
28+
if [ -n "$cache_entry" ] && [ -f "$cache_entry/.complete" ]; then
29+
echo "Using cached documentation for $config $version ($commit)"
30+
cp -a "$cache_entry/." "$stage/"
31+
rm "$stage/.complete"
32+
else
33+
git -C "$source_dir" checkout -f "$tag"
34+
if [ "$mode" = no-vendor ]; then
35+
rm -rf "$source_dir/vendor"
36+
else
37+
# Flags are deliberately split into individual Composer arguments.
38+
# shellcheck disable=SC2086
39+
(cd "$source_dir" && "$php" "$composer" update $update_flags)
40+
fi
41+
cp -R static/assets/. "$stage/"
42+
"$php" bin/apitool.php generate --config "$config" --version "$version" --tag "$tag" \
43+
--output-dir "$stage" "$source_dir"
44+
if [ -n "$cache_entry" ]; then
45+
cache_stage=$(mktemp -d "$cache_dir/.docs-XXXXXX")
46+
cp -a "$stage/." "$cache_stage/"
47+
touch "$cache_stage/.complete"
48+
rm -rf "$cache_entry"
49+
mv "$cache_stage" "$cache_entry"
50+
fi
51+
fi
52+
53+
# Publish only after every page has been generated successfully.
54+
# mktemp creates a private directory; nginx workers must be able to traverse it.
55+
chmod 755 "$stage"
56+
rm -rf "$output"
57+
mv "$stage" "$output"

‎bin/cache-key.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
// Standalone: cache lookup must work before loading the generator's dependencies.
5+
$files = ['composer.json', 'composer.lock'];
6+
foreach (['src', 'templates', 'static', 'config', 'bin'] as $directory) {
7+
$iterator = new RecursiveIteratorIterator(
8+
new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS)
9+
);
10+
foreach ($iterator as $file) {
11+
if ($file->isFile()) {
12+
$files[] = $file->getPathname();
13+
}
14+
}
15+
}
16+
sort($files, SORT_STRING);
17+
$hash = hash_init('sha256');
18+
foreach ($files as $file) {
19+
$digest = hash_file('sha256', $file);
20+
if ($digest === false) {
21+
throw new RuntimeException(sprintf('Unable to fingerprint `%s`.', $file));
22+
}
23+
hash_update($hash, $file . "\0" . $digest . "\0");
24+
}
25+
hash_update($hash, json_encode(array_slice($argv, 1), JSON_THROW_ON_ERROR));
26+
hash_update($hash, PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION);
27+
echo hash_final($hash), "\n";

‎bin/releases.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
$releases = require dirname(__DIR__) . '/config/releases.php';
5+
$group = $argv[1] ?? '';
6+
if (!isset($releases[$group])) {
7+
fwrite(STDERR, sprintf("Unknown release group `%s`.\n", $group));
8+
exit(1);
9+
}
10+
foreach ($releases[$group] as $release) {
11+
echo implode('|', [$release['version'], $release['tag'], $release['mode']]), ' ';
12+
}
13+
echo "\n";

0 commit comments

Comments
 (0)