This commit is contained in:
+14
-74
@@ -68,87 +68,27 @@ jobs:
|
||||
docker push mikhaillobanov/devops-platform:latest
|
||||
docker push mikhaillobanov/devops-platform:${GITHUB_SHA}
|
||||
|
||||
- name: Deploy application
|
||||
- name: Deploy application via Ansible
|
||||
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
||||
env:
|
||||
ANSIBLE_PRIVATE_KEY: ${{ secrets.ID_RSA }}
|
||||
IMAGE: mikhaillobanov/devops-platform:${{ github.sha }}
|
||||
run: |
|
||||
set -eu
|
||||
|
||||
OLD_IMAGE=$(docker inspect --format='{{.Config.Image}}' devops-platform)
|
||||
apt-get update
|
||||
apt-get install -y ansible openssh-client
|
||||
|
||||
docker pull "$IMAGE"
|
||||
docker stop devops-platform
|
||||
docker rm devops-platform
|
||||
mkdir -p /root/.ssh
|
||||
chmod 700 /root/.ssh
|
||||
|
||||
if IMAGE="$IMAGE" docker compose up -d --no-build app; then
|
||||
echo "New container started"
|
||||
else
|
||||
echo "Failed to start new container; restoring old image"
|
||||
docker rm -f devops-platform || true
|
||||
if ! IMAGE="$OLD_IMAGE" docker compose up -d --no-build app; then
|
||||
echo "ERROR: rollback failed; manual recovery required"
|
||||
exit 1
|
||||
fi
|
||||
printf '%s\n' "$ANSIBLE_PRIVATE_KEY" > /root/.ssh/id_rsa
|
||||
chmod 600 /root/.ssh/id_rsa
|
||||
|
||||
ROLLBACK_HEALTHY=false
|
||||
ssh-keyscan -H 178.154.209.109 >> /root/.ssh/known_hosts
|
||||
chmod 600 /root/.ssh/known_hosts
|
||||
|
||||
for attempt in $(seq 1 15); do
|
||||
if docker exec devops-platform python -c \
|
||||
"import urllib.request; r=urllib.request.urlopen('http://127.0.0.1:8000/health', timeout=2); assert r.status == 200"; then
|
||||
ROLLBACK_HEALTHY=true
|
||||
break
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
|
||||
if [ "$ROLLBACK_HEALTHY" = true ]; then
|
||||
echo "Rollback successful; previous version is healthy"
|
||||
else
|
||||
echo "ERROR: rollback container failed health check; manual recovery required"
|
||||
fi
|
||||
|
||||
exit 1
|
||||
fi
|
||||
|
||||
HEALTHY=false
|
||||
|
||||
for attempt in $(seq 1 15); do
|
||||
if docker exec devops-platform python -c \
|
||||
"import urllib.request; r=urllib.request.urlopen('http://127.0.0.1:8000/health', timeout=2); assert r.status == 200"; then
|
||||
HEALTHY=true
|
||||
break
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
|
||||
if [ "$HEALTHY" = true ]; then
|
||||
echo "Deployment successful"
|
||||
else
|
||||
echo "Health check failed; rolling back"
|
||||
docker stop devops-platform || true
|
||||
docker rm devops-platform || true
|
||||
if ! IMAGE="$OLD_IMAGE" docker compose up -d --no-build app; then
|
||||
echo "ERROR: rollback failed; manual recovery required"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
ROLLBACK_HEALTHY=false
|
||||
|
||||
for attempt in $(seq 1 15); do
|
||||
if docker exec devops-platform python -c \
|
||||
"import urllib.request; r=urllib.request.urlopen('http://127.0.0.1:8000/health', timeout=2); assert r.status == 200"; then
|
||||
ROLLBACK_HEALTHY=true
|
||||
break
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
|
||||
if [ "$ROLLBACK_HEALTHY" = true ]; then
|
||||
echo "Rollback successful; previous version is healthy"
|
||||
else
|
||||
echo "ERROR: rollback container failed health check; manual recovery required"
|
||||
fi
|
||||
|
||||
exit 1
|
||||
fi
|
||||
ansible-playbook \
|
||||
-i ansible/inventory.ini \
|
||||
ansible/playbooks/deploy_app.yml \
|
||||
-e "deploy_image=$IMAGE"
|
||||
|
||||
Reference in New Issue
Block a user