1+ name : 🚀 Deploy to Production
2+
3+ on :
4+ push :
5+ branches : [ main ]
6+ workflow_dispatch :
7+ inputs :
8+ force_rebuild :
9+ description : ' Force rebuild all images'
10+ required : false
11+ default : ' false'
12+ type : boolean
13+
14+ env :
15+ IMAGE_TAG : ${{ github.sha }}
16+
17+ jobs :
18+ step2-build-images :
19+ name : 🏗️ Step 2 - Build & Push Images
20+ runs-on : ubuntu-latest
21+
22+ steps :
23+ - name : 📥 Checkout code
24+ uses : actions/checkout@v4
25+
26+ - name : 🐍 Set up Python
27+ uses : actions/setup-python@v4
28+ with :
29+ python-version : ' 3.11'
30+
31+ - name : 📦 Install Ansible
32+ run : |
33+ python -m pip install --upgrade pip
34+ pip install ansible
35+
36+ - name : � Read infrastructure outputs
37+ run : |
38+ if [ -f "infrastructure_outputs.txt" ]; then
39+ cat infrastructure_outputs.txt
40+ echo "EC2_IP=$(grep 'ec2_public_ip:' infrastructure_outputs.txt | cut -d' ' -f2)" >> $GITHUB_ENV
41+ else
42+ echo "EC2_IP=${{ secrets.EC2_HOST }}" >> $GITHUB_ENV
43+ fi
44+
45+ - name : 🐳 Login to DockerHub
46+ run : |
47+ echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin
48+
49+ - name : 🏗️ Step 2 - Build and Push Images
50+ working-directory : ./ansible
51+ run : |
52+ ansible-playbook step2-build-images.yml \
53+ -e "dockerhub_username=${{ secrets.DOCKERHUB_USERNAME }}" \
54+ -e "dockerhub_password=${{ secrets.DOCKERHUB_TOKEN }}" \
55+ -e "image_tag=${{ env.IMAGE_TAG }}" \
56+ -e "ec2_public_ip=${{ env.EC2_IP }}"
57+
58+ step3-deploy :
59+ name : 🚀 Step 3 - Deploy to EC2
60+ runs-on : ubuntu-latest
61+ needs : step2-build-images
62+
63+ steps :
64+ - name : 📥 Checkout code
65+ uses : actions/checkout@v4
66+
67+ - name : 🐍 Set up Python
68+ uses : actions/setup-python@v4
69+ with :
70+ python-version : ' 3.11'
71+
72+ - name : 📦 Install Ansible
73+ run : |
74+ python -m pip install --upgrade pip
75+ pip install ansible
76+
77+ - name : 🔑 Setup SSH key
78+ run : |
79+ mkdir -p ~/.ssh
80+ echo "${{ secrets.EC2_PRIVATE_KEY }}" > ~/.ssh/housescanner-key.pem
81+ chmod 600 ~/.ssh/housescanner-key.pem
82+ ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts
83+
84+ - name : 📂 Create inventory file
85+ working-directory : ./ansible
86+ run : |
87+ cat > inventory.yml << EOF
88+ all:
89+ hosts:
90+ production:
91+ ansible_host: ${{ secrets.EC2_HOST }}
92+ ansible_user: ubuntu
93+ ansible_ssh_private_key_file: ~/.ssh/housescanner-key.pem
94+ ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
95+ EOF
96+
97+ - name : 🚀 Step 3 - Deploy Application
98+ working-directory : ./ansible
99+ run : |
100+ ansible-playbook -i inventory.yml step3-deploy-application.yml \
101+ -e "dockerhub_username=${{ secrets.DOCKERHUB_USERNAME }}" \
102+ -e "image_tag=${{ env.IMAGE_TAG }}"
103+
104+ - name : ✅ Deployment Success
105+ run : |
106+ echo "🎉 Deployment completed successfully!"
107+ echo "🔖 Image tag: ${{ env.IMAGE_TAG }}"
108+ echo "🌐 Application URL: http://${{ secrets.EC2_HOST }}:8080"
109+ echo "🔧 Backend API: http://${{ secrets.EC2_HOST }}:3000"
110+ echo "🤖 Agents Service: http://${{ secrets.EC2_HOST }}:8000"
111+
112+ notify :
113+ name : 📢 Notify Status
114+ runs-on : ubuntu-latest
115+ needs : [step2-build-images, step3-deploy]
116+ if : always()
117+
118+ steps :
119+ - name : 📢 Success Notification
120+ if : needs.step3-deploy.result == 'success'
121+ run : |
122+ echo "✅ Deployment successful!"
123+ echo "🔖 Image tag: ${{ github.sha }}"
124+ echo "👤 Author: ${{ github.actor }}"
125+ echo "💾 Commit: ${{ github.event.head_commit.message }}"
126+
127+ - name : ❌ Failure Notification
128+ if : needs.step2-build-images.result == 'failure' || needs.step3-deploy.result == 'failure'
129+ run : |
130+ echo "❌ Deployment failed!"
131+ echo "🔖 Image tag: ${{ github.sha }}"
132+ echo "👤 Author: ${{ github.actor }}"
133+ echo "💾 Commit: ${{ github.event.head_commit.message }}"
134+ if [[ "${{ needs.step2-build-images.result }}" == "failure" ]]; then
135+ echo "🏗️ Failed at: Step 2 - Build Images"
136+ fi
137+ if [[ "${{ needs.step3-deploy.result }}" == "failure" ]]; then
138+ echo "🚀 Failed at: Step 3 - Deploy"
139+ fi
140+ exit 1
0 commit comments