diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 16b00ce..faf607f 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -1,12 +1,14 @@ -# Tests, image build and deployment on every push to main. +# main: tests and image build (tags: commit hash, `main`). +# prod: the same, then deployment to the server (tags: commit hash, `prod`, `latest`). # # Runs on the self-hosted runner in ci/runner (label `linux`). Jobs use the -# host's Docker daemon, so image builds share its BuildKit cache. +# host's Docker daemon, so image builds share its BuildKit cache; a commit +# already built on main is rebuilt from cache in seconds. # # Repository settings (Settings → Actions): # secrets.REGISTRY_TOKEN Gitea access token with package write access # secrets.DEPLOY_SSH_KEY private key of the deploy user on the server -# vars.DEPLOY_HOST user@host of the server; deployment is skipped when unset +# vars.DEPLOY_HOST user@host of the server # vars.DEPLOY_KNOWN_HOSTS `ssh-keyscan ` output # vars.DEPLOY_PATH directory on the server (default /opt/sorsfonalak) @@ -14,7 +16,7 @@ name: CI on: push: - branches: [main] + branches: [main, prod] workflow_dispatch: env: @@ -77,11 +79,16 @@ jobs: run: | set -eu tag=$(echo "${{ github.sha }}" | cut -c1-12) + extra="${{ github.ref_name }}" + [ "$extra" = prod ] && extra="prod latest" build() { image="$IMAGE_PREFIX-$1"; shift - docker build --label sorsfonalak.ci=1 -t "$image:$tag" -t "$image:latest" "$@" + docker build --label sorsfonalak.ci=1 -t "$image:$tag" "$@" docker push -q "$image:$tag" - docker push -q "$image:latest" + for t in $extra; do + docker tag "$image:$tag" "$image:$t" + docker push -q "$image:$t" + done } build horoscope-api -f services/horoscope-api/Dockerfile . build backend -f services/backend/Dockerfile . @@ -95,9 +102,12 @@ jobs: deploy: needs: build - if: vars.DEPLOY_HOST != '' + if: github.ref == 'refs/heads/prod' runs-on: linux steps: + - name: Check settings + run: | + [ -n "${{ vars.DEPLOY_HOST }}" ] || { echo "vars.DEPLOY_HOST is not set (Settings → Actions → Variables)" >&2; exit 1; } - name: Checkout run: | apk add --no-cache -q git openssh-client diff --git a/deploy/README.md b/deploy/README.md index 4094442..c179a86 100644 --- a/deploy/README.md +++ b/deploy/README.md @@ -1,10 +1,15 @@ # Build és telepítés -Minden `main`-re pusholt commit után lefut a `.gitea/workflows/ci.yml` (Gitea Actions): +A `.gitea/workflows/ci.yml` (Gitea Actions) két ágra fut: + +- **main**: tesztek, majd image-build. Címkék: a commit hash első 12 karaktere és `main`. +- **prod**: ugyanez, majd telepítés a szerverre. Címkék: hash, `prod` és `latest`. Élesíteni tehát úgy kell, hogy a main-t a prod-ba mergeled (vagy pusholod). + +A lépések: 1. **test**: `cargo test` a backendre, a workerre, a db-re, a horoscope-clientre és a service-kitre, egy Postgres szolgáltatás-konténerrel. -2. **build**: megépíti a négy image-et, és feltölti őket a `registry.sagifer.com/sagifer/sorsfonalak-` címre. A címke a commit hash első 12 karaktere, plusz `latest`. A frontend buildje a TypeScript-típusokat is ellenőrzi. -3. **deploy** (csak ha a `DEPLOY_HOST` be van állítva): felmásolja a `docker-compose.yml`-t és a `Caddyfile`-t a szerverre, beírja az új `TAG`-et a szerveren lévő `.env`-be, majd `docker compose pull && up -d`. Az adatbázis-migrációk a backend indulásakor futnak le. +2. **build**: megépíti a négy image-et, és feltölti őket a `registry.sagifer.com/sagifer/sorsfonalak-` címre. A frontend buildje a TypeScript-típusokat is ellenőrzi. Egy main-en már megépült commit a cache-ből pár mp alatt újraépül. +3. **deploy** (csak prod): felmásolja a `docker-compose.yml`-t és a `Caddyfile`-t a szerverre, beírja az új `TAG`-et a szerveren lévő `.env`-be, majd `docker compose pull && up -d`. Az adatbázis-migrációk a backend indulásakor futnak le. Kézzel is indítható a repó Actions oldaláról (workflow_dispatch).