44 pull_request :
55 push :
66 branches : [ main ]
7+ tags : [ 'v*' ]
8+ release :
9+ types : [ published ]
710
811jobs :
912 scan_ruby :
@@ -129,6 +132,7 @@ jobs:
129132 images : ghcr.io/${{ github.repository }}
130133 tags : |
131134 type=raw,value=latest,enable={{is_default_branch}}
135+ type=raw,value=production,enable=${{ github.event_name == 'release' || startsWith(github.ref, 'refs/tags/v') }}
132136 type=ref,event=branch
133137 type=ref,event=tag
134138 type=sha
@@ -143,7 +147,7 @@ jobs:
143147 labels : ${{ steps.meta.outputs.labels }}
144148 no-cache : true
145149
146- deploy :
150+ deploy_uat :
147151 runs-on : ubuntu-latest
148152 needs : build_and_push_docker_image
149153 if : github.ref == 'refs/heads/main'
@@ -160,18 +164,18 @@ jobs:
160164 source : " docker-compose.yml,nginx.conf"
161165 target : " /root"
162166
163- - name : Deploy to server
167+ - name : Deploy to server (UAT)
164168165169 with :
166170 host : ${{ secrets.SERVER_HOST }}
167171 username : ${{ secrets.SERVER_USER }}
168172 key : ${{ secrets.SERVER_SSH_KEY }}
169173 script : |
170174 cd /root
171- docker compose pull
172- docker compose up -d
175+ docker compose -f docker-compose.yml pull
176+ docker compose -f docker-compose.yml up -d
173177 # Clean up old images from ghcr.io registry (keep only the latest)
174- docker images 'ghcr.io/oddsteam/dailynews-e-newspaper' --format '{{.ID}} {{.Tag}}' | grep -v 'latest' | awk '{print $1}' | xargs -r docker rmi -f || true
178+ docker images 'ghcr.io/oddsteam/dailynews-e-newspaper' --format '{{.ID}} {{.Tag}}' | grep -v 'latest' | grep -v 'production' | awk '{print $1}' | xargs -r docker rmi -f || true
175179
176180 - name : Health check
177181 run : |
@@ -190,4 +194,52 @@ jobs:
190194
191195 echo "✗ Health check failed after 5 attempts"
192196 exit 1
197+
198+ deploy_production :
199+ runs-on : ubuntu-latest
200+ needs : build_and_push_docker_image
201+ if : startsWith(github.ref, 'refs/tags/v') || github.event_name == 'release'
202+ steps :
203+ - name : Checkout code
204+ uses : actions/checkout@v4
205+
206+ - name : Copy docker-compose.prod.yml to server
207+ 208+ with :
209+ host : ${{ secrets.SERVER_PRODUCTION_HOST }}
210+ username : ${{ secrets.SERVER_PRODUCTION_USER }}
211+ key : ${{ secrets.SERVER_PRODUCTION_SSH_KEY }}
212+ source : " docker-compose.prod.yml,nginx.conf"
213+ target : " /home/${{ secrets.SERVER_PRODUCTION_USER }}"
214+
215+ - name : Deploy to server (Production)
216+ 217+ with :
218+ host : ${{ secrets.SERVER_PRODUCTION_HOST }}
219+ username : ${{ secrets.SERVER_PRODUCTION_USER }}
220+ key : ${{ secrets.SERVER_PRODUCTION_SSH_KEY }}
221+ script : |
222+ cd /home/${{ secrets.SERVER_PRODUCTION_USER }}
223+ docker compose -f docker-compose.prod.yml pull
224+ docker compose -f docker-compose.prod.yml up -d
225+ # Clean up old images from ghcr.io registry (keep only the latest and production)
226+ docker images 'ghcr.io/oddsteam/dailynews-e-newspaper' --format '{{.ID}} {{.Tag}}' | grep -v 'latest' | grep -v 'production' | awk '{print $1}' | xargs -r docker rmi -f || true
227+
228+ - name : Health check
229+ run : |
230+ echo "Waiting for services to start..."
231+ sleep 15
232+
233+ echo "Checking application health..."
234+ for i in {1..5}; do
235+ if curl -f -s http://${{ secrets.SERVER_PRODUCTION_HOST }}/e-newspaper/up > /dev/null; then
236+ echo "✓ Health check passed!"
237+ exit 0
238+ fi
239+ echo "Attempt $i failed, retrying in 10 seconds..."
240+ sleep 10
241+ done
242+
243+ echo "✗ Health check failed after 5 attempts"
244+ exit 1
193245
0 commit comments