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
35 changes: 35 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions .github/workflows/dco.yml
Original file line number Diff line number Diff line change
@@ -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 <email>' 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
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
58 changes: 58 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down Expand Up @@ -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).
39 changes: 39 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -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**: <https://github.com/wonmortech/deploy-kit/security/advisories/new>.
2. If you cannot use GitHub: a private message on Telegram to <https://t.me/xnosir>. 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.
2 changes: 2 additions & 0 deletions deploy/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
Expand Down
7 changes: 6 additions & 1 deletion deploy/remote.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ set -euo pipefail
SHA="${1:?usage: remote.sh <sha>}"
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 \
Expand All @@ -23,7 +25,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
Expand Down
Loading
Loading