fix: deploy verified images atomically

This commit is contained in:
2026-10-09 13:14:11 +03:00
parent cc4ac1ddd7
commit f8c31de7c0
6 changed files with 238 additions and 37 deletions
+4 -4
View File
@@ -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
+75 -6
View File
@@ -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 <<EOF
API_IMAGE=${{ needs.verify.outputs.api_image }}
NGINX_IMAGE=${{ needs.verify.outputs.nginx_image }}
SWAGGER_IMAGE=${{ needs.verify.outputs.swagger_image }}
PROMETHEUS_IMAGE=${{ needs.verify.outputs.prometheus_image }}
GRAFANA_IMAGE=${{ needs.verify.outputs.grafana_image }}
S3_IMAGE=${{ needs.verify.outputs.s3_image }}
S3_BOOTSTRAP_IMAGE=${{ needs.verify.outputs.s3_bootstrap_image }}
EOF
- name: Authenticate registry on deployment host
run: printf '%s' "$REGISTRY_PASSWORD" | ssh "$DEPLOY_USER@$DEPLOY_HOST" "docker login gitea.privcloud.tech --username '$REGISTRY_USERNAME' --password-stdin"
- name: Upload image manifest
run: scp deploy-images.env "$DEPLOY_USER@$DEPLOY_HOST:/tmp/mtusi2026lab4-${{ github.run_id }}.env"
- name: Deploy and verify
run: |
ssh "$DEPLOY_USER@$DEPLOY_HOST" "
@@ -116,8 +179,14 @@ jobs:
git fetch origin main
git cat-file -e '$DEPLOY_SHA^{commit}'
git reset --hard '$DEPLOY_SHA'
git clean -fd
scripts/rolling-update.sh
manifest='/tmp/mtusi2026lab4-${{ github.run_id }}.env'
if scripts/rolling-update.sh \"\$manifest\"; then
rm -f \"\$manifest\"
else
status=\$?
rm -f \"\$manifest\"
exit \"\$status\"
fi
test \"\$(git rev-parse HEAD)\" = '$DEPLOY_SHA'
docker compose ps
"
+24
View File
@@ -15,6 +15,7 @@ services:
cpus: ${POSTGRES_CPUS}
mem_limit: ${POSTGRES_MEMORY}
restart: unless-stopped
networks: [data]
valkey:
image: valkey/valkey:8.1.4-alpine
@@ -31,6 +32,7 @@ services:
cpus: ${VALKEY_CPUS}
mem_limit: ${VALKEY_MEMORY}
restart: unless-stopped
networks: [data]
s3:
build:
@@ -57,6 +59,7 @@ services:
cpus: ${S3_CPUS}
mem_limit: ${S3_MEMORY}
restart: unless-stopped
networks: [data]
s3-bootstrap:
build:
@@ -80,6 +83,7 @@ services:
cpus: ${S3_BOOTSTRAP_CPUS}
mem_limit: ${S3_BOOTSTRAP_MEMORY}
restart: "no"
networks: [data]
migrate:
image: ${API_IMAGE:-mtusi-messenger:lab4}
@@ -101,6 +105,7 @@ services:
cpus: ${MIGRATE_CPUS}
mem_limit: ${MIGRATE_MEMORY}
restart: "no"
networks: [data]
api:
build: .
@@ -140,6 +145,7 @@ services:
S3_TIMEOUT_SECONDS: ${S3_TIMEOUT_SECONDS}
ATTACHMENT_MAX_BYTES: ${ATTACHMENT_MAX_BYTES}
ATTACHMENT_MAX_PIXELS: ${ATTACHMENT_MAX_PIXELS}
ATTACHMENT_BODY_MEMORY_BYTES: ${ATTACHMENT_BODY_MEMORY_BYTES}
ATTACHMENT_DECODE_MEMORY_BYTES: ${ATTACHMENT_DECODE_MEMORY_BYTES}
expose:
- "8080"
@@ -162,6 +168,7 @@ services:
mem_limit: ${API_MEMORY}
stop_grace_period: 8s
restart: unless-stopped
networks: [application, data]
nginx:
build:
@@ -181,6 +188,7 @@ services:
cpus: ${NGINX_CPUS}
mem_limit: ${NGINX_MEMORY}
restart: unless-stopped
networks: [edge, application, monitoring]
swagger:
build:
@@ -195,6 +203,7 @@ services:
cpus: ${SWAGGER_CPUS}
mem_limit: ${SWAGGER_MEMORY}
restart: unless-stopped
networks: [edge]
tests:
profiles: ["tests"]
@@ -211,6 +220,7 @@ services:
condition: service_healthy
cpus: ${TEST_CPUS}
mem_limit: ${TEST_MEMORY}
networks: [data]
postgres-exporter:
image: quay.io/prometheuscommunity/postgres-exporter:v0.20.1
@@ -225,6 +235,7 @@ services:
cpus: ${EXPORTER_CPUS}
mem_limit: ${EXPORTER_MEMORY}
restart: unless-stopped
networks: [data, monitoring]
valkey-exporter:
image: oliver006/redis_exporter:v1.89.0-alpine
@@ -238,6 +249,7 @@ services:
cpus: ${EXPORTER_CPUS}
mem_limit: ${EXPORTER_MEMORY}
restart: unless-stopped
networks: [data, monitoring]
nginx-exporter:
image: nginx/nginx-prometheus-exporter:1.5.1
@@ -248,6 +260,7 @@ services:
cpus: ${EXPORTER_CPUS}
mem_limit: ${EXPORTER_MEMORY}
restart: unless-stopped
networks: [monitoring]
prometheus:
build:
@@ -276,6 +289,7 @@ services:
cpus: ${PROMETHEUS_CPUS}
mem_limit: ${PROMETHEUS_MEMORY}
restart: unless-stopped
networks: [edge, application, data, monitoring]
grafana:
build:
@@ -295,6 +309,7 @@ services:
cpus: ${GRAFANA_CPUS}
mem_limit: ${GRAFANA_MEMORY}
restart: unless-stopped
networks: [edge, monitoring]
volumes:
postgres_data:
@@ -302,3 +317,12 @@ volumes:
prometheus_data:
grafana_data:
s3_data:
networks:
edge:
application:
internal: true
data:
internal: true
monitoring:
internal: true
+24
View File
@@ -0,0 +1,24 @@
services:
api:
image: ${API_IMAGE:?API_IMAGE is required}
migrate:
image: ${API_IMAGE:?API_IMAGE is required}
nginx:
image: ${NGINX_IMAGE:?NGINX_IMAGE is required}
swagger:
image: ${SWAGGER_IMAGE:?SWAGGER_IMAGE is required}
prometheus:
image: ${PROMETHEUS_IMAGE:?PROMETHEUS_IMAGE is required}
grafana:
image: ${GRAFANA_IMAGE:?GRAFANA_IMAGE is required}
s3:
image: ${S3_IMAGE:?S3_IMAGE is required}
s3-bootstrap:
image: ${S3_BOOTSTRAP_IMAGE:?S3_BOOTSTRAP_IMAGE is required}
+1
View File
@@ -40,6 +40,7 @@ S3_USE_TLS=false
S3_TIMEOUT_SECONDS=5
ATTACHMENT_MAX_BYTES=10485760
ATTACHMENT_MAX_PIXELS=40000000
ATTACHMENT_BODY_MEMORY_BYTES=67108864
ATTACHMENT_DECODE_MEMORY_BYTES=402653184
# Размер пула и обслуживание соединений PostgreSQL.
+110 -27
View File
@@ -2,18 +2,44 @@
set -eu
if [ "$#" -ne 1 ] || [ ! -f "$1" ]; then
printf 'usage: %s <image-manifest>\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