diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 4f7dc51..a3a2409 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -65,9 +65,8 @@ jobs: cp lab4.env.example .env docker compose config --quiet docker compose up -d --build --scale api=2 --wait --wait-timeout 150 - network=$(docker inspect --format '{{range $name, $_ := .NetworkSettings.Networks}}{{$name}}{{end}}' "$(docker compose ps -q nginx)") - docker network connect "$network" "$HOSTNAME" - printf '%s\n' "$network" > .ci-network + docker network connect "${COMPOSE_PROJECT_NAME}_application" "$HOSTNAME" + docker network connect "${COMPOSE_PROJECT_NAME}_data" "$HOSTNAME" - name: Integration checks run: | @@ -89,5 +88,6 @@ jobs: - name: Stop local stack if: always() run: | - if [ -f .ci-network ]; then docker network disconnect "$(cat .ci-network)" "$HOSTNAME"; fi + docker network disconnect "${COMPOSE_PROJECT_NAME}_application" "$HOSTNAME" 2>/dev/null || true + docker network disconnect "${COMPOSE_PROJECT_NAME}_data" "$HOSTNAME" 2>/dev/null || true if [ -f .env ]; then docker compose --profile tests down -v --remove-orphans; fi diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 975fd62..bcf4cf7 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -13,12 +13,28 @@ jobs: if: github.ref == 'refs/heads/main' runs-on: ubuntu-latest timeout-minutes: 45 + outputs: + api_image: ${{ steps.publish.outputs.api_image }} + nginx_image: ${{ steps.publish.outputs.nginx_image }} + swagger_image: ${{ steps.publish.outputs.swagger_image }} + prometheus_image: ${{ steps.publish.outputs.prometheus_image }} + grafana_image: ${{ steps.publish.outputs.grafana_image }} + s3_image: ${{ steps.publish.outputs.s3_image }} + s3_bootstrap_image: ${{ steps.publish.outputs.s3_bootstrap_image }} env: COMPOSE_PROJECT_NAME: mtusi2026lab4-deploy-${{ github.run_id }} + COMPOSE_FILE: compose.yaml:deploy/compose.images.yaml API_BIND_ADDRESS: 127.0.0.1:0 SWAGGER_BIND_ADDRESS: 127.0.0.1:0 PROMETHEUS_BIND_ADDRESS: 127.0.0.1:0 GRAFANA_BIND_ADDRESS: 127.0.0.1:0 + API_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab4-api:${{ github.sha }} + NGINX_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab4-nginx:${{ github.sha }} + SWAGGER_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab4-swagger:${{ github.sha }} + PROMETHEUS_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab4-prometheus:${{ github.sha }} + GRAFANA_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab4-grafana:${{ github.sha }} + S3_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab4-minio:${{ github.sha }} + S3_BOOTSTRAP_IMAGE: gitea.privcloud.tech/${{ github.repository_owner }}/mtusi2026lab4-minio-client:${{ github.sha }} steps: - name: Checkout exact commit run: | @@ -63,9 +79,8 @@ jobs: cp lab4.env.example .env docker compose config --quiet docker compose up -d --build --scale api=2 --wait --wait-timeout 150 - network=$(docker inspect --format '{{range $name, $_ := .NetworkSettings.Networks}}{{$name}}{{end}}' "$(docker compose ps -q nginx)") - docker network connect "$network" "$HOSTNAME" - printf '%s\n' "$network" > .ci-network + docker network connect "${COMPOSE_PROJECT_NAME}_application" "$HOSTNAME" + docker network connect "${COMPOSE_PROJECT_NAME}_data" "$HOSTNAME" - name: Integration checks run: | @@ -79,6 +94,33 @@ jobs: make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://nginx:8080 make test-failover ACTION_SCRIPT=scripts/stop-one.sh BASE_URL=http://nginx:8080 + - name: Publish verified images + id: publish + env: + REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }} + REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }} + run: | + printf '%s' "$REGISTRY_PASSWORD" | docker login gitea.privcloud.tech --username "$REGISTRY_USERNAME" --password-stdin + + publish() { + output=$1 + image=$2 + push_output=$(docker push "$image") + printf '%s\n' "$push_output" + digest=$(printf '%s\n' "$push_output" | sed -n 's/^.*digest: \(sha256:[0-9a-f]*\).*$/\1/p' | tail -n 1) + test -n "$digest" + repository=${image%:*} + printf '%s=%s@%s\n' "$output" "$repository" "$digest" >> "$GITHUB_OUTPUT" + } + + publish api_image "$API_IMAGE" + publish nginx_image "$NGINX_IMAGE" + publish swagger_image "$SWAGGER_IMAGE" + publish prometheus_image "$PROMETHEUS_IMAGE" + publish grafana_image "$GRAFANA_IMAGE" + publish s3_image "$S3_IMAGE" + publish s3_bootstrap_image "$S3_BOOTSTRAP_IMAGE" + - name: Container logs if: failure() run: if [ -f .env ]; then docker compose logs --no-color --tail=200; fi @@ -86,7 +128,8 @@ jobs: - name: Stop local stack if: always() run: | - if [ -f .ci-network ]; then docker network disconnect "$(cat .ci-network)" "$HOSTNAME"; fi + docker network disconnect "${COMPOSE_PROJECT_NAME}_application" "$HOSTNAME" 2>/dev/null || true + docker network disconnect "${COMPOSE_PROJECT_NAME}_data" "$HOSTNAME" 2>/dev/null || true if [ -f .env ]; then docker compose --profile tests down -v --remove-orphans; fi deploy: @@ -99,6 +142,8 @@ jobs: DEPLOY_USER: ${{ secrets.DEPLOY_USER }} DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }} DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }} + REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }} + REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }} DEPLOY_SHA: ${{ github.sha }} steps: - name: Configure SSH @@ -108,6 +153,24 @@ jobs: printf '%s\n' "$DEPLOY_SSH_KEY" > ~/.ssh/id_ed25519 printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > ~/.ssh/known_hosts + - name: Prepare immutable image manifest + run: | + cat > deploy-images.env <\n' "$0" >&2 + exit 2 +fi + +manifest=$1 +deploy_dir=${DEPLOY_STATE_DIR:-"$HOME/.local/state/mtusi2026lab4"} +current_manifest=$deploy_dir/current-images.env +previous_manifest=$deploy_dir/previous-images.env +replicas=${API_REPLICAS:-2} +rollback_ready=false +switched=false + +mkdir -p "$deploy_dir" + +load_manifest() { + set -a + # shellcheck disable=SC1090 + . "$1" + set +a +} + +compose() { + docker compose -f compose.yaml -f deploy/compose.images.yaml "$@" +} + wait_for_replicas() { expected=$1 - deadline=$(($(date +%s) + 90)) + deadline=$(($(date +%s) + 120)) while [ "$(date +%s)" -lt "$deadline" ]; do - containers=$(docker compose ps -q api) + containers=$(compose ps -q api) count=0 healthy=0 for container in $containers; do count=$((count + 1)) status=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$container") - if [ "$status" = "healthy" ]; then + if [ "$status" = healthy ]; then healthy=$((healthy + 1)) fi done @@ -23,36 +49,93 @@ wait_for_replicas() { sleep 1 done - printf '%s\n' "API replicas did not become healthy: expected $expected" >&2 + printf 'API replicas did not become healthy: expected %s\n' "$expected" >&2 return 1 } -old_containers=$(docker compose ps -q api) -old_count=0 -for container in $old_containers; do - old_count=$((old_count + 1)) -done +smoke_check() { + compose exec -T nginx wget -q -O /dev/null http://127.0.0.1:8080/health/ready +} -docker compose build -docker compose up -d postgres valkey s3 -docker compose run --rm s3-bootstrap -docker compose run --rm migrate +running_image() { + containers=$(compose ps -q "$1") + for container in $containers; do + docker inspect --format '{{.Config.Image}}' "$container" + return 0 + done + return 1 +} -if [ "$old_count" -lt 2 ]; then - docker compose up -d --scale api=2 --wait --wait-timeout 120 - exit 0 +capture_running_images() { + output=$1 + api_image=$(running_image api) || return 1 + nginx_image=$(running_image nginx) || return 1 + swagger_image=$(running_image swagger) || swagger_image=$SWAGGER_IMAGE + prometheus_image=$(running_image prometheus) || prometheus_image=$PROMETHEUS_IMAGE + grafana_image=$(running_image grafana) || grafana_image=$GRAFANA_IMAGE + s3_image=$(running_image s3) || s3_image=$S3_IMAGE + s3_bootstrap_image=$(running_image s3-bootstrap) || s3_bootstrap_image=$S3_BOOTSTRAP_IMAGE + + { + printf 'API_IMAGE=%s\n' "$api_image" + printf 'NGINX_IMAGE=%s\n' "$nginx_image" + printf 'SWAGGER_IMAGE=%s\n' "$swagger_image" + printf 'PROMETHEUS_IMAGE=%s\n' "$prometheus_image" + printf 'GRAFANA_IMAGE=%s\n' "$grafana_image" + printf 'S3_IMAGE=%s\n' "$s3_image" + printf 'S3_BOOTSTRAP_IMAGE=%s\n' "$s3_bootstrap_image" + } > "$output" +} + +rollback() { + status=$? + trap - EXIT INT TERM + + if [ "$switched" = true ] && [ "$rollback_ready" = true ]; then + printf '%s\n' 'Deployment failed; restoring the previous images.' >&2 + compose stop nginx api >/dev/null 2>&1 || true + compose rm -f nginx api >/dev/null 2>&1 || true + load_manifest "$previous_manifest" + compose up -d --no-build --no-deps --force-recreate --scale api="$replicas" api + wait_for_replicas "$replicas" + compose up -d --no-build --no-deps --force-recreate nginx + smoke_check + elif [ "$switched" = true ]; then + printf '%s\n' 'Deployment failed and no previous image manifest is available.' >&2 + fi + + exit "$status" +} + +trap rollback EXIT +trap 'exit 130' INT +trap 'exit 143' TERM + +load_manifest "$manifest" + +if [ -f "$current_manifest" ]; then + cp "$current_manifest" "$previous_manifest" + rollback_ready=true +elif capture_running_images "$previous_manifest"; then + rollback_ready=true fi -docker compose up -d --no-deps --no-recreate --scale api=3 api -wait_for_replicas 3 +compose pull api nginx swagger prometheus grafana s3 s3-bootstrap +compose up -d --no-build --wait --wait-timeout 120 postgres valkey s3 +compose run --rm --no-deps s3-bootstrap +compose run --rm --no-deps migrate -for container in $old_containers; do - docker stop --time 6 "$container" >/dev/null - docker rm "$container" >/dev/null - docker compose up -d --no-deps --no-recreate --scale api=3 api - wait_for_replicas 3 -done +# Stop the entry point and every old API replica before starting the new pool. +# This gives an atomic version boundary at the cost of a short maintenance window. +switched=true +compose stop nginx api +compose rm -f nginx api +compose up -d --no-build --no-deps --force-recreate --scale api="$replicas" api +wait_for_replicas "$replicas" +compose up -d --no-build --no-deps --force-recreate nginx +smoke_check -docker compose up -d --no-deps --no-recreate --scale api=2 api -wait_for_replicas 2 -docker compose up -d --no-build --scale api=2 --wait --wait-timeout 120 +compose up -d --no-build swagger postgres-exporter valkey-exporter nginx-exporter prometheus grafana + +cp "$manifest" "$current_manifest" +trap - EXIT INT TERM