1010
1111 steps :
1212 - uses : actions/checkout@v4
13-
13+
1414 - name : Set up Python
1515 uses : actions/setup-python@v4
1616 with :
2525 - name : Fix permissions on tar file
2626 run : chmod 644 flask-api.tar
2727
28-
2928 - name : Copy Docker image to VM
3029 uses : appleboy/scp-action@v0.1.7
3130 with :
@@ -35,14 +34,33 @@ jobs:
3534 source : " flask-api.tar"
3635 target : " ~/"
3736
38- - name : Deploy on VM via SSH
37+ - name : Deploy on VM via SSH (Zero Downtime)
3938 uses : appleboy/ssh-action@v1.0.0
4039 with :
4140 host : ${{ secrets.VM_HOST }}
4241 username : ${{ secrets.VM_USER }}
4342 key : ${{ secrets.VM_SSH_KEY }}
4443 script : |
44+ # 1. Load the new Docker image
4545 docker load -i ~/flask-api.tar
46+
47+ # 2. Start the new container on a different port (e.g., 8080)
48+ docker run -d --name flask-api-new -p 8080:80 flask-api:latest
49+
50+ # 3. Health check: Wait until the new container is healthy
51+ until curl -s http://localhost:8080/sayHello | grep -q "Hello User"; do
52+ echo "Waiting for new container to be ready..."
53+ sleep 2
54+ done
55+
56+ # 4. Stop and remove the old container
4657 docker stop flask-api || true
4758 docker rm flask-api || true
59+
60+ # 5. Start the new container on the main port (80:80)
4861 docker run -d --name flask-api -p 80:80 flask-api:latest
62+
63+ # 6. Remove the temporary new container
64+ docker stop flask-api-new
65+ docker rm flask-api-new
66+
0 commit comments