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
"