Skip to content

Commit 27f1dd8

Browse files
committed
fix: refactor deployment workflow and enhance staging/production scripts
1 parent e3bf0aa commit 27f1dd8

1 file changed

Lines changed: 36 additions & 8 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,61 @@ env:
99
FRONTEND_IMAGE: ${{ secrets.DOCKERHUB_USERNAME }}/mern-chat-app-frontend
1010

1111
jobs:
12-
build-and-deploy:
12+
build:
1313
runs-on: ubuntu-latest
14-
1514
steps:
1615
- name: Checkout code
1716
uses: actions/checkout@v4
18-
17+
- name: Set up Docker Buildx
18+
uses: docker/setup-buildx-action@v3
1919
- name: Log in to Docker Hub
2020
uses: docker/login-action@v3
2121
with:
2222
username: ${{ secrets.DOCKERHUB_USERNAME }}
2323
password: ${{ secrets.DOCKERHUB_TOKEN }}
24-
25-
- name: Build and push backend image
24+
- name: Build and push backend
2625
uses: docker/build-push-action@v5
2726
with:
2827
context: ./Back-end
2928
push: true
3029
tags: ${{ env.BACKEND_IMAGE }}:latest
31-
32-
- name: Build and push frontend image
30+
cache-from: type=gha
31+
cache-to: type=gha,mode=max
32+
- name: Build and push frontend
3333
uses: docker/build-push-action@v5
3434
with:
3535
context: ./Front-end
3636
push: true
3737
tags: ${{ env.FRONTEND_IMAGE }}:latest
38+
cache-from: type=gha
39+
cache-to: type=gha,mode=max
40+
41+
deploy-staging:
42+
needs: build
43+
runs-on: ubuntu-latest
44+
environment: staging
45+
steps:
46+
- name: Deploy to Staging EC2
47+
uses: appleboy/ssh-action@v1
48+
with:
49+
host: ${{ secrets.STG_EC2_HOST }}
50+
username: ${{ secrets.STG_EC2_USER }}
51+
key: ${{ secrets.STG_EC2_SSH_KEY }}
52+
timeout: 60s
53+
script: |
54+
cd ~/MERN-CHAT-APP
55+
docker-compose pull
56+
sleep 5
57+
docker-compose up -d --remove-orphans
58+
docker image prune -f
59+
echo "✅ Staging deployed"
3860
39-
- name: Deploy to EC2
61+
deploy-production:
62+
needs: deploy-staging
63+
runs-on: ubuntu-latest
64+
environment: production
65+
steps:
66+
- name: Deploy to Production EC2
4067
uses: appleboy/ssh-action@v1
4168
with:
4269
host: ${{ secrets.EC2_HOST }}
@@ -49,3 +76,4 @@ jobs:
4976
sleep 5
5077
docker-compose up -d --remove-orphans
5178
docker image prune -f
79+
echo "✅ Production deployed"

0 commit comments

Comments
 (0)