diff --git a/README.md b/README.md index 352efbf..f874937 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ przy pobieraniu akcji, patrz „Dostęp" niżej. | Akcja | Do czego | | --- | --- | | [`build-site-with-hugo`](build-site-with-hugo/) | Hugo w podanej wersji + build do `public/`, opcjonalny serwer dla bramek jakości | +| [`deploy-static-site`](deploy-static-site/) | rsync zbudowanej strony do `releases//` i atomowe `activate` przez `deploy-static` | | [`pnpm-install`](pnpm-install/) | corepack + `pnpm install` z lockfile'a, opcjonalny cache store'a | | [`resolve-buildx-cache`](resolve-buildx-cache/) | `DOCKER_CACHE_*` z runnera → `driver-opts` i refy cache'a dla buildx | diff --git a/deploy-static-site/README.md b/deploy-static-site/README.md new file mode 100644 index 0000000..1886393 --- /dev/null +++ b/deploy-static-site/README.md @@ -0,0 +1,53 @@ +# deploy-static-site + +Wgrywa zbudowaną stronę statyczną na serwer, na którym klucz CI ma wymuszone polecenie +`deploy-static` (repo `prepare-server-for-multi-client`), i atomowo ją aktywuje. + +```yaml +on: + push: + tags: + - "[0-9][0-9][0-9][0-9].[0-9][0-9].[0-9][0-9]" + +# ... + - uses: https://git.cierzniak.it/gitea-runner/actions/build-site-with-hugo@v1 + with: + version: ${{ env.HUGO_VERSION }} + + - uses: https://git.cierzniak.it/gitea-runner/actions/deploy-static-site@v1 + with: + ssh-key: ${{ secrets.DEPLOY_SSH_KEY }} + ssh-user: ${{ vars.DEPLOY_SSH_USER }} + host: ${{ vars.DEPLOY_HOST }} + known-hosts: ${{ vars.DEPLOY_KNOWN_HOSTS }} +``` + +Kolejno: + +1. Zapisuje klucz, przypięty `known_hosts` i alias `prod` w `~/.ssh`. +2. Wpisuje identyfikator wydania do `/version.txt`. +3. `rsync` do `releases//` na serwerze. +4. `ssh prod activate ` przełącza `current`, a serwer zostawia 3 ostatnie wydania. + Potem `ssh prod list` wypisuje je do logu. + +## Wejście + +| Input | Wymagany | Domyślnie | Co to | +| --- | --- | --- | --- | +| `ssh-key` | tak | — | Klucz prywatny konta strony | +| `ssh-user` | tak | — | Konto strony na serwerze (`www-*`) | +| `host` | tak | — | Host serwera | +| `known-hosts` | tak | — | Przypięta linia `known_hosts`. Nigdy `ssh-keyscan` w CI | +| `release` | nie | nazwa refa (tag) | Identyfikator wydania: katalog na serwerze i treść `version.txt` | +| `path` | nie | `public` | Katalog ze zbudowaną stroną | + +Outputów nie ma. Pusty któryś z wymaganych inputów kończy krok błędem z jego nazwą. Runner +nie egzekwuje `required`, a pusty sekret objawiłby się dopiero niejasnym błędem ssh. + +`deploy-static` przyjmuje identyfikator `[A-Za-z0-9][A-Za-z0-9._-]{0,63}`, więc pasują +i tagi CalVer (`2026.11.01`), i SHA. Wycofanie: `ssh … activate `. + +Obraz joba musi mieć `ssh` i `rsync`. Ma je `git.cierzniak.it/gitea-runner/deploy:latest`. + +Bez `--delete` w rsync: ten sam identyfikator wypchnięty drugi raz, np. przepięty tag, +dogra pliki do istniejącego katalogu i stare tam zostaną. diff --git a/deploy-static-site/action.yaml b/deploy-static-site/action.yaml new file mode 100644 index 0000000..d92c2c7 --- /dev/null +++ b/deploy-static-site/action.yaml @@ -0,0 +1,84 @@ +name: Deploy static site +description: >- + Ships a built static site to a host guarded by the deploy-static forced command: writes + version.txt, rsyncs the files into releases// and atomically activates them. + +inputs: + ssh-key: + description: Private key of the site account; the server pins it to deploy-static. + required: true + ssh-user: + description: Site account on the server (www-*). + required: true + host: + description: Server host name. + required: true + known-hosts: + description: Pinned known_hosts line for the host. Never generate it with ssh-keyscan in CI. + required: true + release: + description: Release id - directory name on the server and content of version.txt. Empty means the pushed ref name (tag). + required: false + default: '' + path: + description: Directory with the built site. + required: false + default: public + +runs: + using: composite + steps: + - name: Configure SSH + shell: sh + env: + DEPLOY_SSH_KEY: ${{ inputs.ssh-key }} + DEPLOY_SSH_USER: ${{ inputs.ssh-user }} + DEPLOY_HOST: ${{ inputs.host }} + DEPLOY_KNOWN_HOSTS: ${{ inputs.known-hosts }} + # The runner does not enforce `required`, and an empty secret would surface later as a + # cryptic ssh error - name what is missing instead. + run: | + missing="" + [ -n "$DEPLOY_SSH_KEY" ] || missing="$missing ssh-key" + [ -n "$DEPLOY_SSH_USER" ] || missing="$missing ssh-user" + [ -n "$DEPLOY_HOST" ] || missing="$missing host" + [ -n "$DEPLOY_KNOWN_HOSTS" ] || missing="$missing known-hosts" + if [ -n "$missing" ]; then + echo "::error::deploy-static-site: empty inputs:$missing" + exit 1 + fi + install -d -m 700 ~/.ssh + (umask 077 && printf '%s\n' "$DEPLOY_SSH_KEY" > ~/.ssh/deploy) + printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > ~/.ssh/known_hosts + cat > ~/.ssh/config < "$SITE_PATH/version.txt" + + # No --delete (the release directory is new) and no -p/-o/-g (activate sets permissions). + - name: Upload release + shell: sh + env: + RELEASE: ${{ inputs.release || github.ref_name }} + SITE_PATH: ${{ inputs.path }} + run: rsync -rlz "$SITE_PATH/" "prod:$RELEASE/" + + - name: Activate release + shell: sh + env: + RELEASE: ${{ inputs.release || github.ref_name }} + run: | + # shellcheck disable=SC2029 # the id is meant to expand on the CI side + ssh prod activate "$RELEASE" + ssh prod list