1- name : Build, Test and Publish Docker image & Deploy to EKS
1+ name : Build, Publish and Deploy Docker Image to EC2
22
33on :
44 push :
55 branches : [ main ]
66 workflow_dispatch :
77
88jobs :
9- smoke-and-publish :
9+ # ================================================================= #
10+ # 1. CI PHASE: BUILD, TEST, AND PUBLISH #
11+ # (Renamed from 'smoke-and-publish' to clearly reflect its role) #
12+ # ================================================================= #
13+ build-and-publish :
1014 runs-on : ubuntu-latest
1115 steps :
1216 - name : ⬇️ Checkout repository
1317 uses : actions/checkout@v4
14-
15- # ... (Smoke Test and Setup Steps Omitted) ...
1618
17- # --- DOCKER HUB PUSH STEPS (Updated to use DOCKER_USERNAME/PASSWORD) ---
19+ # ... (Smoke Test and Setup Steps Omitted) ...
1820
1921 - name : Set up Docker Buildx
2022 uses : docker/setup-buildx-action@v2
2123
2224 - name : 🐳 Login to Docker Hub
2325 uses : docker/login-action@v2
2426 with :
25- # Using your existing secrets
2627 username : ${{ secrets.DOCKER_USERNAME }}
2728 password : ${{ secrets.DOCKER_PASSWORD }}
2829
@@ -36,24 +37,69 @@ jobs:
3637 ${{ secrets.DOCKER_USERNAME }}/car_classifier:latest
3738 ${{ secrets.DOCKER_USERNAME }}/car_classifier:${{ github.sha }}
3839
39- # --- DEPLOYMENT STEP (No change needed here, as it uses SSH keys) ---
40- - name : 🚀 Deploy to EKS Server via SSH
41- uses : appleboy/ssh-action@v1.0.3
42- with :
43- host : ${{ secrets.EC2_PUBLIC_IP }}
44- username : ec2-user # Confirm your EC2 username
45- key : ${{ secrets.EC2_SSH_KEY }}
40+ ---
41+
42+ # ================================================================= #
43+ # 2. CD PHASE: DEPLOY TO SINGLE EC2 HOST (Free Tier Recommended) #
44+ # ================================================================= #
45+ deploy :
46+ name : Deploy Container to EC2
47+ runs-on : ubuntu-latest
48+ needs : build-and-publish # Make sure the name matches your first CI job
49+
50+ steps :
51+ - name : 🚀 SSH and Deploy Container
52+ uses : appleboy/ssh-action@v1.0.3
53+ with :
54+ host : ${{ secrets.EC2_PUBLIC_IP }}
55+ # FIX 1: Changed username to 'ec2-user' for Amazon Linux
56+ username : ec2-user
57+ key : ${{ secrets.EC2_SSH_KEY }}
58+ script : |
59+ # --- 1. Define Variables ---
60+ APP_NAME="ai-car-inspector"
61+ IMAGE_NAME="${{ secrets.DOCKER_USERNAME }}/car_classifier:latest"
62+ HOST_PORT="80"
63+ CONTAINER_PORT="7860"
64+
65+ # --- 2. Install Docker (Idempotent - **UPDATED for Amazon Linux**) ---
66+ if ! command -v docker &> /dev/null
67+ then
68+ echo "Docker not found, installing via yum/dnf..."
69+
70+ # FIX 2: Use yum/dnf commands for Docker installation
71+ # This works for both Amazon Linux 2 (yum) and Amazon Linux 2023 (dnf/yum)
72+ sudo yum update -y
73+ sudo yum install -y docker
74+
75+ # Start and enable the Docker service
76+ sudo systemctl start docker
77+ sudo systemctl enable docker
78+ fi
79+
80+ # --- 3. Log in to Docker Hub ---
81+ echo "Logging into Docker Hub..."
82+ echo "${{ secrets.DOCKER_PASSWORD }}" | sudo docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
83+
84+ # --- 4. Pull and Run the New Image ---
85+
86+ # Stop and remove the old container with the same name (if it exists)
87+ echo "Stopping and removing old container $APP_NAME..."
88+ sudo docker stop $APP_NAME || true
89+ sudo docker rm $APP_NAME || true
90+
91+ # Pull the new image
92+ echo "Pulling image $IMAGE_NAME"
93+ sudo docker pull $IMAGE_NAME
94+
95+ # Run the new container, mapping the host port to the container port
96+ echo "Running new container $IMAGE_NAME on port $HOST_PORT:$CONTAINER_PORT"
97+ sudo docker run -d \
98+ -p $HOST_PORT:$CONTAINER_PORT \
99+ -p 6443:6443 \
100+ --name $APP_NAME \
101+ --restart always \
102+ $IMAGE_NAME
46103
47- # The script runs on the EKS worker node to apply manifests
48- script : |
49- # 1. Download the latest deployment manifests from the GitHub repository
50- # These files contain the image tag and secret names
51- curl -o deployment.yaml https://raw.githubusercontent.com/${{ github.repository }}/main/deployment.yaml
52- curl -o service.yaml https://raw.githubusercontent.com/${{ github.repository }}/main/service.yaml
53-
54- # 2. Apply the manifests using the local kubectl
55- sudo kubectl apply -f deployment.yaml
56- sudo kubectl apply -f service.yaml
57-
58- # 3. Wait for the new Pod to become Ready
59- sudo kubectl rollout status deployment/gradio-gemini-deployment
104+ echo "Deployment complete. Running containers:"
105+ sudo docker ps
0 commit comments