1- name : " App Deployment "
1+ name : " CI/CD Pipeline "
22
33on :
44 push :
5- branches :
6- - " main"
5+ branches : ["main"]
76
87jobs :
9- build-and- deploy :
8+ deploy :
109 runs-on : ubuntu-latest
1110 steps :
11+ # 1. SETUP & BUILD
1212 - name : Checkout Code
1313 uses : actions/checkout@v4
1414
15- # CI: Build image while server is still booting
1615 - name : Login to Docker Hub
1716 uses : docker/login-action@v3
1817 with :
@@ -24,37 +23,36 @@ jobs:
2423 docker build -t aaren17/devops-demo:latest .
2524 docker push aaren17/devops-demo:latest
2625
27- # NEW: Wait for the server to finish installing Docker
28- - name : Wait for Server Readiness
29- uses : appleboy/ssh-action@v1.0.3
30- with :
31- host : ${{ secrets.EC2_HOST }}
32- username : ubuntu
33- key : ${{ secrets.EC2_SSH_KEY }}
34- command_timeout : 10m
35- script : |
36- # This waits until the user_data script is done
37- sudo cloud-init status --wait
26+ # 2. SMART WAIT (Replaces sleep 60)
27+ - name : Wait for SSH Reachability
28+ timeout-minutes : 2
29+ run : |
30+ while ! nc -z ${{ secrets.EC2_HOST }} 22; do
31+ sleep 5
32+ done
3833
39- # CD: Teleport file with a longer timeout to prevent i/o errors
40- - name : Copy Compose File
34+ # 3. DEPLOY FILES
35+ - name : Copy Files
4136 uses : appleboy/scp-action@v0.1.7
4237 with :
4338 host : ${{ secrets.EC2_HOST }}
4439 username : ubuntu
4540 key : ${{ secrets.EC2_SSH_KEY }}
46- timeout : 300s # Patiently waits for the SSH port to open
4741 source : " docker-compose.yml,prometheus.yml"
4842 target : " /home/ubuntu/"
4943
50- # REFRESH
51- - name : Remote Docker Update
44+ # 4. START APPLICATION
45+ - name : Deploy App
5246 uses : appleboy/ssh-action@v1.0.3
5347 with :
5448 host : ${{ secrets.EC2_HOST }}
5549 username : ubuntu
5650 key : ${{ secrets.EC2_SSH_KEY }}
5751 script : |
52+ # Ensure Docker installation is actually finished
53+ sudo cloud-init status --wait
54+
55+ # Run the app
5856 cd /home/ubuntu
5957 docker compose pull
60- docker compose up -d
58+ docker compose up -d
0 commit comments