Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 32 additions & 39 deletions .github/docs/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Deploy the Django-GC documentation

Pushes to any branch and pull requests run **Security checks**.
Pushes to protected branches also run the test workflow. To deploy,
The branch protection check dispatches testing for the current head of a protected branch.
Pushes to unprotected branches create no testing jobs. To deploy,
run **Documentation** on `main` or dispatch it from another workflow.
The release workflow dispatches it after PyPI accepts the package. It
passes the release commit, which must belong to `main`. A manual run
Expand All @@ -16,10 +17,10 @@ The server builds the Dockerfile from the selected revision. Both documentation
deployment and PyPI publishing require security and test checks for the exact
revision. The separate **Security checks** workflow runs Gitleaks, Python and
Django SAST and dependency checks, JavaScript and TypeScript SAST and npm
audit, then checks GitHub Actions. The shared CI then runs the Python checks
and matrix one job at a time, builds the wheel and sdist, and type-checks
and builds both documentation locales. A failing check stops publishing. Check
the **Documentation** run for the deployment result.
audit, then checks GitHub Actions. The verification job checks the result of
the existing `testing.yml` run on `main` for that SHA after scanning.
Publish the current head of `main` after its tests succeed. Check the
**Documentation** run for the deployment result.

### PyPI Trusted Publishing

Expand Down Expand Up @@ -49,7 +50,7 @@ Add `DOCS_PORT` as an **Environment variable**.
| `DOCS_PORT` | Free loopback port for this documentation site, for example `18080` |

Assign each documentation site on the host its own `DOCS_PORT`.
Set `SSH_USER` to `docs-deploy`. For `SSH_HOST_KEY`, use the first two
Set `SSH_USER` to the existing deployment account, for example `deploy`. For `SSH_HOST_KEY`, use the first two
fields of the server's `/etc/ssh/ssh_host_ed25519_key.pub`, for example
`ssh-ed25519 AAAA…`. Obtain the key through the server console or its
administrator. From the server console, print the two fields with:
Expand All @@ -70,39 +71,31 @@ ssh-keygen -t ed25519 -f ./gc-docs-ci -C django-gc-docs-ci
gh secret set SSH_PRIVATE_KEY --env docs-production --repo RDDLab/Django-GlobalConfig < ./gc-docs-ci
```

### Server setup

1. Create the unprivileged `docs-deploy` account with a home directory,
a shell for OpenSSH's forced command, and its own rootless Docker
daemon with Docker Compose V2. Enable the rootless Docker user service
and lingering so the daemon remains available without a login session.
Install Git, Bash, tar, and flock. Configure outbound access to GitHub,
the npm registry, and the container registries. For a private Git
repository, configure read-only Git HTTPS credentials for this account.
2. From a reviewed revision, install the deployment script and Compose
file as root-owned files. The CI key can invoke only this installed
script.

```sh
sudo install -m 0755 .github/docs/deploy-docs.sh /usr/local/bin/django-gc-docs-deploy
sudo install -d -m 0755 /usr/local/share/django-gc-docs
sudo install -m 0644 .github/docs/compose.yml /usr/local/share/django-gc-docs/compose.yml
```

3. In the account's `.ssh/authorized_keys`, prepend
`restrict,command="/usr/local/bin/django-gc-docs-deploy"` to the
dedicated public key. Set `.ssh` to mode 0700 and `authorized_keys`
to 0600, both owned by `docs-deploy`. The GitHub Actions runner must
be able to reach the configured SSH port.
4. Point the existing HTTPS reverse proxy for
`django-gc.rdd-lab.com` at `127.0.0.1:<DOCS_PORT>`. When you
change this setting, update the proxy upstream to match.

Install both server files before the first deployment.
The workflow sends the commit SHA and host port to the forced SSH command.
The server validates both, fetches `main` from GitHub and checks that the
commit belongs to it. Rootless Docker builds and serves the site. Angie
and the container health check use its internal port `8080`.
### Server preparation and deployment

Use an existing unprivileged account that can execute commands over SSH and
access Docker with Compose V2. Bash, tar, and flock must be available on the host.
The existing GitHub Actions key can use the `restrict` option in
`~/.ssh/authorized_keys`. Use a key that can execute the workflow's SSH
commands. For a key previously configured with `command=`, use a dedicated
key without that option for both preparation and deployment.

After the checks succeed, `docs.yml` calls `prepare-docs.yml` using the
same `docs-production` secrets. It sends the deployment script and
Compose file over SSH and installs both together under
`$HOME/.local/share/django-gc-docs/<workflow-sha>/`. Repeating the
preparation for the same revision reuses the installed files. To prepare
the server by itself, run **Prepare documentation server** from `main`.

The deploy job sends a source archive for the selected commit from its GitHub
checkout. The SSH account builds and updates the container with its Docker
context or socket. When a rootless socket is available and no custom Docker
context is selected, the script uses that socket. Both jobs run with the
existing account's permissions.

Point the HTTPS reverse proxy for `django-gc.rdd-lab.com` at
`127.0.0.1:<DOCS_PORT>`. Angie and the container health check use the
container's internal port `8080`.

The deployment lock serializes updates. The container checks both `/`
and `/ru/`. If the new deployment fails, the script restores the previous
Expand Down
50 changes: 26 additions & 24 deletions .github/docs/deploy-docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,29 @@
set -euo pipefail
umask 077

if [[ ! "${SSH_ORIGINAL_COMMAND:-}" =~ ^([0-9a-f]{40})[[:blank:]]([1-9][0-9]{0,4})$ ]] ||
(( BASH_REMATCH[2] > 65535 )); then
echo 'Expected a full commit SHA and a host port from 1 to 65535 as the SSH command.' >&2
if (( $# != 2 )) || [[ ! "$1" =~ ^[0-9a-f]{40}$ || ! "$2" =~ ^[1-9][0-9]{0,4}$ ]] ||
(( $2 > 65535 )); then
echo 'Expected a full commit SHA and a host port from 1 to 65535.' >&2
exit 1
fi
sha=${BASH_REMATCH[1]}
export DOCS_PORT="${BASH_REMATCH[2]}"
sha=$1
export DOCS_PORT=$2

state_dir="$HOME/.local/state/django-gc-docs"
repo="$state_dir/repository.git"
compose_file=/usr/local/share/django-gc-docs/compose.yml
deploy_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
compose_file="$deploy_dir/compose.yml"

# Use the dedicated rootless daemon even when SSH has no login session.
DOCKER_HOST="unix:///run/user/$(id -u)/docker.sock"
export DOCKER_HOST
unset DOCKER_CONTEXT
# Use a rootless socket when SSH did not start a session and Docker has no custom context.
docker_socket="/run/user/$(id -u)/docker.sock"
if [[ -z "${DOCKER_HOST:-}" && -z "${DOCKER_CONTEXT:-}" && -S "$docker_socket" ]] &&
[[ "$(docker context show)" == default ]]; then
export DOCKER_HOST="unix://$docker_socket"
fi

mkdir -p "$state_dir"
exec 9>"$state_dir/deploy.lock"
flock -w 300 9

if [[ ! -d "$repo/objects" ]]; then
git init --bare "$repo"
fi
git --git-dir="$repo" fetch --no-tags https://github.com/RDDLab/Django-GlobalConfig.git \
'+refs/heads/main:refs/heads/main'
if ! git --git-dir="$repo" merge-base --is-ancestor "$sha" refs/heads/main; then
echo 'The documentation revision must be a commit in main.' >&2
exit 1
fi

compose() {
docker compose --project-name django-gc-docs --env-file /dev/null --file "$compose_file" "$@"
}
Expand Down Expand Up @@ -67,8 +59,18 @@ trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM

git --git-dir="$repo" archive "$sha" docs .github/docs | tar -x -C "$release_dir"
dockerfile="$release_dir/.github/docs/Dockerfile"
tar -xf - -C "$release_dir"
if [[ -f "$release_dir/.github/docs/Dockerfile" ]]; then
build_context="$release_dir"
dockerfile="$build_context/.github/docs/Dockerfile"
else
build_context="$release_dir/docs"
dockerfile="$build_context/Dockerfile"
fi
if [[ ! -f "$dockerfile" ]]; then
echo 'The documentation archive does not contain a Dockerfile.' >&2
exit 1
fi
export DOCS_IMAGE=django-gc-docs:"$sha"
previous_container=$(compose ps --all --quiet docs)
if [[ -n "$previous_container" ]]; then
Expand All @@ -80,7 +82,7 @@ if [[ -n "$previous_container" ]]; then
fi
fi

docker build --file "$dockerfile" --tag "$DOCS_IMAGE" "$release_dir"
docker build --file "$dockerfile" --tag "$DOCS_IMAGE" "$build_context"
new_image=$(docker image inspect --format '{{.Id}}' "$DOCS_IMAGE")
restore_service=true
compose up --detach --wait --wait-timeout 60
Expand Down
34 changes: 34 additions & 0 deletions .github/docs/prepare-server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash
set -euo pipefail
umask 077

if (( $# != 1 )) || [[ ! "$1" =~ ^[0-9a-f]{40}$ ]]; then
echo 'Expected a full workflow commit SHA.' >&2
exit 1
fi

source_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
base_dir="$HOME/.local/share/django-gc-docs"
install_dir="$base_dir/$1"

mkdir -p -- "$base_dir"
staging_dir=$(mktemp -d "$base_dir/.prepare.XXXXXXXX")
trap 'rm -rf -- "$staging_dir"' EXIT

bash -n "$source_dir/deploy-docs.sh"
install -m 0700 "$source_dir/deploy-docs.sh" "$staging_dir/deploy-docs.sh"
install -m 0600 "$source_dir/compose.yml" "$staging_dir/compose.yml"

exec 9>"$base_dir/prepare.lock"
flock -w 60 9
if [[ -e "$install_dir" ]]; then
if ! cmp -s "$staging_dir/deploy-docs.sh" "$install_dir/deploy-docs.sh" ||
! cmp -s "$staging_dir/compose.yml" "$install_dir/compose.yml"; then
echo 'Prepared deployment files do not match this workflow revision.' >&2
exit 1
fi
else
mv -- "$staging_dir" "$install_dir"
fi

printf 'Prepared documentation deployment files for %s.\n' "$1"
28 changes: 28 additions & 0 deletions .github/docs/ssh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash
set -euo pipefail

if [[ ! "${SSH_HOST:-}" =~ ^[[:alnum:].:-]+$ ||
! "${SSH_PORT:-}" =~ ^[1-9][0-9]{0,4}$ ]] ||
(( SSH_PORT > 65535 )) ||
[[ ! "${SSH_USER:-}" =~ ^[a-z_][a-z0-9_-]*$ ||
-z "${SSH_PRIVATE_KEY:-}" || ! "${SSH_HOST_KEY:-}" =~ ^ssh-ed25519[[:blank:]]+[A-Za-z0-9+/]+=*$ ]]; then
echo 'Invalid or missing docs-production SSH settings.' >&2
exit 1
fi

umask 077
ssh_dir=$(mktemp -d)
trap 'rm -rf -- "$ssh_dir"' EXIT
printf '%s\n' "$SSH_PRIVATE_KEY" > "$ssh_dir/key"
known_host=$SSH_HOST
if (( SSH_PORT != 22 )); then
known_host="[$SSH_HOST]:$SSH_PORT"
fi
printf '%s %s\n' "$known_host" "$SSH_HOST_KEY" > "$ssh_dir/known_hosts"
unset SSH_PRIVATE_KEY

ssh -F /dev/null -T -p "$SSH_PORT" -l "$SSH_USER" -i "$ssh_dir/key" \
-o BatchMode=yes -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes \
-o "UserKnownHostsFile=$ssh_dir/known_hosts" -o GlobalKnownHostsFile=/dev/null \
-o ConnectTimeout=15 -o ServerAliveInterval=15 -o ServerAliveCountMax=3 \
-- "$SSH_HOST" "$@"
27 changes: 21 additions & 6 deletions .github/docs/test-deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,29 @@ set -euo pipefail

docs_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
sha=0123456789abcdef0123456789abcdef01234567
expected='Expected a full commit SHA and a host port from 1 to 65535 as the SSH command.'
for invalid in "$sha" "$sha 0" "$sha 65536" "$sha 18080 extra"; do
if output=$(SSH_ORIGINAL_COMMAND="$invalid" bash "$docs_dir/deploy-docs.sh" 2>&1) ||
[[ "$output" != "$expected" ]]; then
echo 'The deployment script accepted an invalid SSH command.' >&2
expected='Expected a full commit SHA and a host port from 1 to 65535.'
reject_deploy_arguments() {
local output
if output=$(bash "$docs_dir/deploy-docs.sh" "$@" 2>&1) || [[ "$output" != "$expected" ]]; then
echo 'The deployment script accepted invalid arguments.' >&2
exit 1
fi
done
}
reject_deploy_arguments "$sha"
reject_deploy_arguments bad-sha 18080
reject_deploy_arguments "$sha" 0
reject_deploy_arguments "$sha" 65536
reject_deploy_arguments "$sha" 18080 extra

if command -v flock >/dev/null; then
test_home=$(mktemp -d)
trap 'rm -rf -- "$test_home"' EXIT
HOME="$test_home" bash "$docs_dir/prepare-server.sh" "$sha"
HOME="$test_home" bash "$docs_dir/prepare-server.sh" "$sha"
prepared="$test_home/.local/share/django-gc-docs/$sha"
cmp -s "$docs_dir/deploy-docs.sh" "$prepared/deploy-docs.sh"
cmp -s "$docs_dir/compose.yml" "$prepared/compose.yml"
fi

export DOCS_IMAGE=django-gc-docs:test
compose_config() {
Expand Down
40 changes: 40 additions & 0 deletions .github/workflows/dispatch-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Dispatch tests for protected branches

on:
workflow_run:
workflows: [Security checks]
types: [requested]

permissions: {}

jobs:
dispatch:
name: Dispatch testing for the protected branch head
if: github.event.workflow_run.event == 'push' && github.event.workflow_run.head_repository.full_name == github.repository
permissions:
actions: write
contents: read
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Check branch protection and dispatch testing
shell: bash
env:
GH_TOKEN: ${{ github.token }}
BRANCH: ${{ github.event.workflow_run.head_branch }}
SOURCE_SHA: ${{ github.event.workflow_run.head_sha }}
run: |
if [[ -z "$BRANCH" || ! "$SOURCE_SHA" =~ ^[0-9a-f]{40}$ ]]; then
echo "::error::Missing push branch or commit."
exit 1
fi

protected_heads=$(gh api --paginate "repos/$GITHUB_REPOSITORY/branches?protected=true&per_page=100")
is_protected_head=$(jq -rs --arg branch "$BRANCH" --arg sha "$SOURCE_SHA" \
'any(.[][]; .name == $branch and .commit.sha == $sha)' <<< "$protected_heads")
if [[ "$is_protected_head" != true ]]; then
echo "The pushed commit is no longer the head of a protected branch, or the branch is unprotected."
exit 0
fi

gh workflow run testing.yml --repo "$GITHUB_REPOSITORY" --ref "$BRANCH" -f "expected_sha=$SOURCE_SHA"
Loading