55 branches : ["main"]
66
77jobs :
8+ # --- JOB 1: BUILD, TEST, AND PUSH (The Docker Way) ---
89 build-test-push :
910 runs-on : ubuntu-latest
1011 steps :
@@ -17,31 +18,58 @@ jobs:
1718 username : ${{ secrets.DOCKERHUB_USERNAME }}
1819 password : ${{ secrets.DOCKERHUB_TOKEN }}
1920
20- # --- 1. BUILD (Includes your Code + Tests) ---
21+ # 1. Build the image (Contains Code + Tests)
2122 - name : Build Docker Image
2223 run : docker build -t test-image:latest .
2324
24- # --- 2. TEST (Inside the container) ---
25+ # 2. Run Tests (Inside the container we just built)
2526 - name : Run Unit Tests
2627 run : docker run --rm test-image:latest python -m pytest
2728
28- # --- 3. PUSH (Only if tests passed) ---
29+ # 3. Push (Only if tests passed)
2930 - name : Push to Docker Hub
3031 run : |
3132 docker tag test-image:latest aaren17/devops-demo:latest
3233 docker push aaren17/devops-demo:latest
3334
35+ # --- JOB 2: DEPLOY (Your Robust Setup) ---
3436 deploy :
35- needs : build-test-push
37+ needs : build-test-push # Wait for tests to pass
3638 runs-on : ubuntu-latest
3739 steps :
38- - name : Deploy to EC2
40+ - name : Checkout Code
41+ uses : actions/checkout@v4
42+
43+ # 1. SMART WAIT (Kept your logic)
44+ - name : Wait for SSH Reachability
45+ timeout-minutes : 2
46+ run : |
47+ while ! nc -z ${{ secrets.EC2_HOST }} 22; do
48+ sleep 5
49+ done
50+
51+ # 2. COPY FILES (Restored Prometheus & Compose)
52+ - name : Copy Files
53+ uses : appleboy/scp-action@v0.1.7
54+ with :
55+ host : ${{ secrets.EC2_HOST }}
56+ username : ubuntu
57+ key : ${{ secrets.EC2_SSH_KEY }}
58+ source : " docker-compose.yml,prometheus.yml"
59+ target : " /home/ubuntu/"
60+
61+ # 3. START APPLICATION (Restored Cloud-init check)
62+ - name : Deploy App
3963 uses : appleboy/ssh-action@v1.0.3
4064 with :
4165 host : ${{ secrets.EC2_HOST }}
4266 username : ubuntu
4367 key : ${{ secrets.EC2_SSH_KEY }}
4468 script : |
69+ # Ensure instance is fully ready
70+ sudo cloud-init status --wait
71+
72+ # Pull the image we just pushed in Job 1
4573 cd /home/ubuntu
4674 docker compose pull
4775 docker compose up -d
0 commit comments