From c0a10c4192d8ef5b3607eb2e62d8442dba6c312b Mon Sep 17 00:00:00 2001 From: xnosir <283333996+xnosir@users.noreply.github.com> Date: Sat, 14 Nov 2026 21:26:30 +0500 Subject: [PATCH 1/2] test: add a shell test suite and CI, fix same-second release collision - tests/run.sh: 33 checks for remote.sh, rollback.sh and deploy.sh end to end, with docker, ssh, curl and date stubbed on PATH, so no server is needed. - CI: ShellCheck (style severity) and the tests on every pull request; DCO check. - remote.sh: the same commit deployed twice within one second got the same release name and mv moved the new site inside the live release. It now stops before anything changes. - CONTRIBUTING.md, SECURITY.md, CHANGELOG.md. Signed-off-by: xnosir <283333996+xnosir@users.noreply.github.com> --- .github/workflows/ci.yml | 35 +++++++ .github/workflows/dco.yml | 45 +++++++++ CHANGELOG.md | 28 ++++++ CONTRIBUTING.md | 58 ++++++++++++ README.md | 11 +++ SECURITY.md | 39 ++++++++ deploy/remote.sh | 3 + tests/run.sh | 187 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 406 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/dco.yml create mode 100644 CHANGELOG.md create mode 100644 CONTRIBUTING.md create mode 100644 SECURITY.md create mode 100755 tests/run.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..661677d --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,35 @@ +name: CI + +# ShellCheck on every script, then the test suite in tests/run.sh (no server or Docker needed: docker, +# ssh and curl are stubbed). Runs on every pull request and on main. +on: + pull_request: + push: + branches: [main] + merge_group: + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: ci-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + build: + name: build + runs-on: ubuntu-24.04 + timeout-minutes: 10 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: ShellCheck + run: | + shellcheck --version + shellcheck --severity=style deploy/*.sh tests/*.sh + + - name: Tests + run: bash tests/run.sh diff --git a/.github/workflows/dco.yml b/.github/workflows/dco.yml new file mode 100644 index 0000000..1b72058 --- /dev/null +++ b/.github/workflows/dco.yml @@ -0,0 +1,45 @@ +name: dco + +# Every commit in a pull request must carry a "Signed-off-by:" line (CONTRIBUTING.md, DCO). +# Merge commits and Dependabot commits are skipped. The PR code is only read, never executed. +on: + pull_request: + +concurrency: + group: dco-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: read + +jobs: + dco: + name: dco + runs-on: ubuntu-24.04 + timeout-minutes: 5 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + persist-credentials: false + - name: Signed-off-by + env: + BASE: ${{ github.event.pull_request.base.sha }} + HEAD: ${{ github.event.pull_request.head.sha }} + run: | + set -euo pipefail + missing=0 + for c in $(git rev-list --no-merges "$BASE..$HEAD"); do + email=$(git log -1 --format='%ae' "$c") + if [ "$email" = "49699333+dependabot[bot]@users.noreply.github.com" ]; then continue; fi + if git log -1 --format='%B' "$c" | grep -qE '^Signed-off-by: .+ <[^>]+@[^>]+>$'; then + echo "ok $c" + else + echo "::error::commit $c has no 'Signed-off-by: Name ' line (use git commit -s)" + missing=1 + fi + done + if [ "$missing" -ne 0 ]; then + echo "Fix: git rebase --signoff origin/main && git push --force-with-lease" + exit 1 + fi diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..a4df3c9 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,28 @@ +# Changelog + +All notable changes to this project are documented here. The format follows +[Keep a Changelog](https://keepachangelog.com/en/1.1.0/). + +## [Unreleased] + +### Added + +- Test suite (`tests/run.sh`) for `deploy.sh`, `remote.sh` and `rollback.sh`, with `docker`, `ssh`, `curl` + and `date` stubbed, so it needs no server. +- CI: ShellCheck and the tests on every pull request; DCO sign-off check. +- `CONTRIBUTING.md`, `SECURITY.md` and this changelog. +- OpenSSF Scorecard workflow, CODEOWNERS and Dependabot for GitHub Actions. + +### Fixed + +- `remote.sh`: deploying the same commit twice within one second produced the same release name, and the new + site was moved *inside* the existing release directory. It now stops before anything changes. + +### Security + +- The example deploy workflow pins `actions/checkout` to a commit and does not persist credentials. + +## Initial version + +- Zero-downtime static deploy template: release directories, atomic symlink swap, validated Caddy config, + graceful reload, health check against `/version.txt`, rollback. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..0206695 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,58 @@ +# Contributing to deploy-kit + +Thank you for helping. deploy-kit is a small template for zero-downtime static deploys with Caddy: three shell +scripts in [deploy/](deploy/), a Caddyfile and a compose file. + +## Ways to contribute + +| You want to | Do this | +|---|---| +| Report a bug | Open an issue with the steps, what you expected and what happened | +| Suggest a change | Open an issue first if it changes how a deploy works, so we can agree on it | +| Fix a typo or a small bug | Open a pull request | +| Report a vulnerability | Not a public issue: see [SECURITY.md](SECURITY.md) | + +## Development + +Requirements: `bash`, `git` and [ShellCheck](https://www.shellcheck.net/). The tests need no server, Docker +or network: `docker`, `ssh`, `curl` and `date` are replaced by stubs. + +```sh +shellcheck --severity=style deploy/*.sh tests/*.sh # static analysis, no findings allowed +bash tests/run.sh # the test suite; prints one line per check +``` + +The server side uses `mv -T` (GNU coreutils), so run the tests on Linux or in WSL. + +## Requirements for a change + +- **Tests.** New behaviour and every bug fix come with a check in [tests/run.sh](tests/run.sh). CI runs + ShellCheck and the tests on every pull request. +- **Nothing live changes before validation.** `remote.sh` must stop before the swap when the upload is + incomplete or the Caddyfile is invalid; keep it that way and keep the swaps atomic (`mv -T`). +- **Placeholders only.** No real hosts, paths, keys or names: use `example.com`, `deploy@your-server`, + `/srv/sites/example`. +- **Coding standard.** Bash with `set -euo pipefail`, quoted expansions, and a comment for each step that + says why it is there. +- **One topic per pull request**, Conventional Commits style (`feat:`, `fix:`, `docs:`, `test:`). + +## Developer Certificate of Origin (DCO) + +Every commit must be signed off, which certifies that you wrote it or have the right to submit it under the +project licence ([developercertificate.org](https://developercertificate.org/)): + +```sh +git commit -s -m "fix: ..." +``` + +The `dco` check fails a pull request that has an unsigned commit; fix it with +`git rebase --signoff origin/main` and push again. + +## Review and merge + +Every change goes through a pull request with green CI (`build`, `dco`) and an approving review from a +maintainer other than the author. We squash-merge and delete the branch. + +## Licence + +Contributions are released under the [MIT License](LICENSE). No CLA is required. diff --git a/README.md b/README.md index 1877797..14c50c7 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ deploy/ rollback.sh runs on the server: point current at an earlier release and reload Caddyfile serves /www/current docker-compose.yml one caddy:2-alpine container +tests/run.sh tests for the three scripts (docker, ssh, curl stubbed; no server needed) .github/workflows/deploy.yml example manual-dispatch workflow ``` @@ -99,6 +100,16 @@ These are the same limits the post lists for the original flow: - **A recreate still has a limit.** When `docker-compose.yml` changes, open downloads get `stop_grace_period` (15 s), not unlimited time. - **`mv -T` is GNU coreutils.** The server must be Linux (the client side does not need it). +## Testing and contributing + +```bash +shellcheck --severity=style deploy/*.sh tests/*.sh +bash tests/run.sh +``` + +CI runs both on every pull request. See [CONTRIBUTING.md](CONTRIBUTING.md) for the rules, and +[SECURITY.md](SECURITY.md) to report a vulnerability privately. Changes are listed in [CHANGELOG.md](CHANGELOG.md). + ## Licence [MIT](LICENSE). diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..2165d89 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,39 @@ +# Security policy + +## Supported versions + +deploy-kit is a template: you copy `deploy/` into your own repository. Fixes go to `main`; there are no +older versions to patch. If you copied an earlier version, compare it with `main` and the +[CHANGELOG](CHANGELOG.md). + +## Reporting a vulnerability + +Please **do not** open a public issue, pull request or discussion for a vulnerability. + +1. Preferred: GitHub private vulnerability reporting, in this repository's **Security** tab, + then **Report a vulnerability**: . +2. If you cannot use GitHub: a private message on Telegram to . A short first message such as + "Security report for deploy-kit" is enough; we will agree on a safe way to share the details. + +Include the affected file, the steps to reproduce, and what an attacker could gain. Never send live +credentials, keys or server addresses, even as proof. + +## What happens next + +| Step | What we do | Within | +|---|---|---| +| Acknowledge | Confirm we received your report | 7 days | +| Assess | Reproduce it and rate its severity (CVSS) | 14 days | +| Fix | Fix high and critical issues on `main`, sooner where possible | 60 days | +| Disclose | Publish a GitHub security advisory with the fix | With the fix | + +We credit reporters in the advisory if they wish. Good-faith research that follows the +[organization security policy](https://github.com/wonmortech/.github/blob/main/SECURITY.md) is welcome and +covered by its safe-harbour section. There is no paid bug bounty. + +## Security notes for users + +- `deploy.sh` sends the site over SSH; pin the server's host key (`DEPLOY_KNOWN_HOSTS` in the example + workflow) instead of accepting it on first use. +- The deploy user only needs to run `docker` and write to the site directory. Do not deploy as root. +- The example workflow runs on manual dispatch only and checks out without persisting credentials. diff --git a/deploy/remote.sh b/deploy/remote.sh index 36f8fbe..41bf995 100755 --- a/deploy/remote.sh +++ b/deploy/remote.sh @@ -23,7 +23,10 @@ mkdir -p conf www/releases # 2) Unpack into a directory no request has seen. The name includes the time, so redeploying the # same commit never overwrites the release that is being served. +# The same commit deployed twice within one second gets the same name; mv would then move the +# site inside the existing release, so stop before anything changes. REL="${SHA:0:12}-$(date -u +%Y%m%dT%H%M%SZ)" +[ ! -e "www/releases/$REL" ] || { echo "www/releases/$REL already exists; deploy again in a second" >&2; exit 1; } mv new/site "www/releases/$REL" # 3) Swap. `ln -sfn` alone is not atomic (unlink + create). Creating a second link and renaming it diff --git a/tests/run.sh b/tests/run.sh new file mode 100755 index 0000000..fb11c3b --- /dev/null +++ b/tests/run.sh @@ -0,0 +1,187 @@ +#!/usr/bin/env bash +# Tests for deploy/deploy.sh, deploy/remote.sh and deploy/rollback.sh. No server, Docker or network +# is needed: docker, ssh, curl and date are replaced by small stubs on PATH, and every test case +# runs in its own temporary directory. Run with: bash tests/run.sh +set -uo pipefail + +ROOT="$(cd "$(dirname "$0")/.." && pwd)" +REAL_DATE="$(command -v date)" +export REAL_DATE +PASS=0 +FAIL=0 +WORK="" + +# Call right after a test command: records its exit status under the given name. +check() { + local rc=$? + if [ "$rc" -eq 0 ]; then PASS=$((PASS + 1)); echo "ok - $1"; else FAIL=$((FAIL + 1)); echo "FAIL - $1"; fi +} +# Passes when the command fails. +fails() { ! "$@" >/dev/null 2>&1; } + +stub() { cat > "$WORK/bin/$1"; chmod +x "$WORK/bin/$1"; } + +# A fresh server directory with the scripts installed, and stubs on PATH. +setup() { + WORK="$(mktemp -d)" + mkdir -p "$WORK/bin" "$WORK/srv" + cp "$ROOT/deploy/remote.sh" "$ROOT/deploy/rollback.sh" "$WORK/srv/" + # docker records every call; `docker run` (the Caddyfile validation) fails when DOCKER_VALIDATE=fail. + stub docker <<'EOF' +#!/usr/bin/env bash +echo "docker $*" >> "$STUB_LOG" +if [ "$1" = run ] && [ "${DOCKER_VALIDATE:-ok}" = fail ]; then echo "invalid Caddyfile" >&2; exit 1; fi +exit 0 +EOF + # date returns FAKE_DATE when it is set, so two deploys can land in the same second on purpose. + stub date <<'EOF' +#!/usr/bin/env bash +if [ -n "${FAKE_DATE:-}" ]; then echo "$FAKE_DATE"; else exec "$REAL_DATE" "$@"; fi +EOF + export STUB_LOG="$WORK/calls.log" + : > "$STUB_LOG" + export PATH="$WORK/bin:$PATH" +} +teardown() { rm -rf "$WORK"; WORK=""; unset FAKE_DATE DOCKER_VALIDATE; } + +# What deploy.sh uploads: new/ with the site, the Caddyfile and the compose file. +stage() { + mkdir -p "$WORK/srv/new/site" + echo "

$1

" > "$WORK/srv/new/site/index.html" + echo "$1" > "$WORK/srv/new/site/version.txt" + cp "$ROOT/deploy/Caddyfile" "$ROOT/deploy/docker-compose.yml" "$WORK/srv/new/" +} +remote() { (cd "$WORK/srv" && ./remote.sh "$@"); } +rollback() { (cd "$WORK/srv" && ./rollback.sh "$@"); } +deploy_remote() { stage "$1"; remote "$1" >/dev/null; } +live() { readlink "$WORK/srv/www/current"; } +live_version() { cat "$WORK/srv/www/current/version.txt"; } +line_of() { grep -n -m1 "$1" "$STUB_LOG" | cut -d: -f1; } + +# ---------------------------------------------------------------- remote.sh +setup +deploy_remote aaaaaaaaaaaa1111 +[ "$(live_version)" = aaaaaaaaaaaa1111 ]; check "remote.sh: first deploy makes current point at the new release" +[[ "$(live)" == releases/aaaaaaaaaaaa-* ]]; check "remote.sh: current is a relative symlink into releases/" +cmp -s "$WORK/srv/conf/Caddyfile" "$ROOT/deploy/Caddyfile"; check "remote.sh: the Caddyfile is installed under conf/" +[ "$(cat "$WORK/srv/www/history")" = "$(basename "$(live)")" ]; check "remote.sh: history holds the live release" +[ "$(cat "$WORK/srv/REVISION")" = aaaaaaaaaaaa1111 ]; check "remote.sh: REVISION holds the commit" +[ ! -e "$WORK/srv/new" ]; check "remote.sh: new/ is removed after the swap" +grep -q "caddy validate" "$STUB_LOG" && [ "$(line_of "caddy validate")" -lt "$(line_of "caddy reload")" ] +check "remote.sh: the Caddyfile is validated before Caddy is reloaded" +grep -q "docker compose exec -T site caddy reload" "$STUB_LOG"; check "remote.sh: Caddy is reloaded in place" +teardown + +setup +for s in 1111111111110001 2222222222220002 3333333333330003 4444444444440004; do deploy_remote "$s"; done +[ "$(find "$WORK/srv/www/releases" -mindepth 1 -maxdepth 1 | wc -l)" -eq 3 ]; check "remote.sh: KEEP_RELEASES=3 keeps three releases" +! compgen -G "$WORK/srv/www/releases/111111111111-*" >/dev/null; check "remote.sh: the oldest release is pruned" +[ "$(live_version)" = 4444444444440004 ]; check "remote.sh: the latest deploy is live" +[ "$(wc -l < "$WORK/srv/www/history")" -eq 3 ] && [ "$(tail -n 1 "$WORK/srv/www/history")" = "$(basename "$(live)")" ] +check "remote.sh: history lists the kept releases, live one last" +teardown + +setup +export KEEP_RELEASES=1 +deploy_remote 1111111111110001; deploy_remote 2222222222220002 +[ "$(find "$WORK/srv/www/releases" -mindepth 1 -maxdepth 1 | wc -l)" -eq 1 ] && [ "$(live_version)" = 2222222222220002 ] +check "remote.sh: KEEP_RELEASES=1 keeps only the live release" +unset KEEP_RELEASES +teardown + +setup +deploy_remote aaaaaaaaaaaa1111 +before="$(live)" +mkdir -p "$WORK/srv/new/site" # no Caddyfile, no compose file +fails remote bbbbbbbbbbbb2222; check "remote.sh: refuses an incomplete upload" +[ "$(live)" = "$before" ]; check "remote.sh: an incomplete upload leaves the live release alone" +rm -rf "$WORK/srv/new" +stage bbbbbbbbbbbb2222 +export DOCKER_VALIDATE=fail +fails remote bbbbbbbbbbbb2222; check "remote.sh: stops on an invalid Caddyfile" +unset DOCKER_VALIDATE +[ "$(live)" = "$before" ] && cmp -s "$WORK/srv/conf/Caddyfile" "$ROOT/deploy/Caddyfile" +check "remote.sh: an invalid Caddyfile leaves the live release and the config alone" +teardown + +setup +export FAKE_DATE=20260101T000000Z +deploy_remote cccccccccccc3333 +rel="$(basename "$(live)")" +stage cccccccccccc3333 +fails remote cccccccccccc3333; check "remote.sh: refuses a release name that already exists (same commit, same second)" +[ ! -e "$WORK/srv/www/releases/$rel/site" ] && [ "$(live)" = "releases/$rel" ] +check "remote.sh: the existing release is not modified" +teardown + +# ---------------------------------------------------------------- rollback.sh +setup +for s in 1111111111110001 2222222222220002 3333333333330003; do deploy_remote "$s"; done +rollback >/dev/null +[ "$(live_version)" = 2222222222220002 ]; check "rollback.sh: with no argument goes back to the previous release" +grep -q "caddy reload" <(tail -n 1 "$STUB_LOG"); check "rollback.sh: reloads Caddy" +list="$(rollback --list)" +grep -q "^\* 222222222222-" <<<"$list"; check "rollback.sh: --list marks the live release with *" +rollback "$(head -n 1 "$WORK/srv/www/history")" >/dev/null +[ "$(live_version)" = 1111111111110001 ]; check "rollback.sh: a named release can be made live" +fails rollback does-not-exist; check "rollback.sh: rejects an unknown release" +[ "$(live_version)" = 1111111111110001 ]; check "rollback.sh: a rejected rollback leaves the live release alone" +teardown + +setup +deploy_remote 1111111111110001 +fails rollback; check "rollback.sh: fails when no earlier release is kept" +teardown + +# ---------------------------------------------------------------- deploy.sh, end to end +# A git checkout of the kit with a built dist/. ssh runs the remote command locally; curl reads +# version.txt from the live release on disk. +e2e_setup() { + setup + REPO="$WORK/repo" + mkdir -p "$REPO" + cp -R "$ROOT/deploy" "$REPO/" + printf 'dist/\n' > "$REPO/.gitignore" + git -C "$REPO" init -q + git -C "$REPO" add -A + git -C "$REPO" -c user.name=test -c user.email=test@example.com commit -q -m init + mkdir -p "$REPO/dist" + echo "

site

" > "$REPO/dist/index.html" + stub ssh <<'EOF' +#!/usr/bin/env bash +shift # the ssh target +exec bash -c "$*" +EOF + stub curl <<'EOF' +#!/usr/bin/env bash +cat "$REMOTE_DIR/www/current/version.txt" +EOF + export REMOTE_DIR="$WORK/server" BRANCH="" SITE_URL=http://site.test +} +e2e_teardown() { unset REMOTE_DIR BRANCH SITE_URL; teardown; } + +e2e_setup +out="$("$REPO/deploy/deploy.sh" deploy@site.test 2>&1)"; rc=$? +sha="$(git -C "$REPO" rev-parse HEAD)" +[ "$rc" -eq 0 ] && grep -q "deploy ready: $sha" <<<"$out"; check "deploy.sh: a clean checkout deploys and passes the health check" +[ "$(cat "$REMOTE_DIR/www/current/version.txt")" = "$sha" ]; check "deploy.sh: version.txt of the live release is the commit" +[ -f "$REMOTE_DIR/www/current/index.html" ]; check "deploy.sh: the built site is live" +[ -x "$REMOTE_DIR/remote.sh" ] && [ -x "$REMOTE_DIR/rollback.sh" ]; check "deploy.sh: remote.sh and rollback.sh are installed on the server" +e2e_teardown + +e2e_setup +echo "# local edit" >> "$REPO/deploy/Caddyfile" +out="$("$REPO/deploy/deploy.sh" deploy@site.test 2>&1)"; rc=$? +[ "$rc" -ne 0 ] && grep -q "uncommitted changes" <<<"$out"; check "deploy.sh: refuses a working tree with uncommitted changes" +[ ! -e "$REMOTE_DIR" ]; check "deploy.sh: nothing is uploaded from a dirty tree" +e2e_teardown + +e2e_setup +rm -rf "$REPO/dist" +out="$("$REPO/deploy/deploy.sh" deploy@site.test 2>&1)"; rc=$? +[ "$rc" -ne 0 ] && grep -q "dist not found" <<<"$out"; check "deploy.sh: refuses when the build output is missing" +e2e_teardown + +echo +echo "passed: $PASS, failed: $FAIL" +[ "$FAIL" -eq 0 ] From 34db8193e88b826d11a29dd2ff3529af56640f14 Mon Sep 17 00:00:00 2001 From: xnosir <283333996+xnosir@users.noreply.github.com> Date: Sat, 14 Nov 2026 21:28:14 +0500 Subject: [PATCH 2/2] fix: clear ShellCheck notes (SC2015, SC2029) Signed-off-by: xnosir <283333996+xnosir@users.noreply.github.com> --- deploy/deploy.sh | 2 ++ deploy/remote.sh | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/deploy/deploy.sh b/deploy/deploy.sh index 1ffa759..ea71f7b 100755 --- a/deploy/deploy.sh +++ b/deploy/deploy.sh @@ -40,6 +40,8 @@ printf '%s\n' "$SHA" > "$STAGE/new/site/version.txt" cp deploy/Caddyfile deploy/docker-compose.yml deploy/remote.sh deploy/rollback.sh "$STAGE/new/" # 3) Upload into new/ and let remote.sh do the swap. Nothing live changes before remote.sh validates. +# The settings are expanded here on purpose: the server gets the values chosen on this machine. +# shellcheck disable=SC2029 tar -czf - -C "$STAGE" new \ | ssh "$TARGET" "set -e; mkdir -p '$REMOTE_DIR'; cd '$REMOTE_DIR'; rm -rf new tar --no-same-owner -xzf - diff --git a/deploy/remote.sh b/deploy/remote.sh index 41bf995..bb56b5f 100755 --- a/deploy/remote.sh +++ b/deploy/remote.sh @@ -13,7 +13,9 @@ set -euo pipefail SHA="${1:?usage: remote.sh }" KEEP="${KEEP_RELEASES:-3}" cd "$(dirname "$0")" -[ -d new/site ] && [ -f new/Caddyfile ] && [ -f new/docker-compose.yml ] || { echo "new/ is incomplete" >&2; exit 1; } +if [ ! -d new/site ] || [ ! -f new/Caddyfile ] || [ ! -f new/docker-compose.yml ]; then + echo "new/ is incomplete" >&2; exit 1 +fi # 1) Validate the new Caddyfile in a throwaway container with no network, before anything live changes. docker run --rm --network none --tmpfs /var/log/caddy -v "$PWD/new:/c:ro" caddy:2-alpine \