-
Notifications
You must be signed in to change notification settings - Fork 7
118 lines (102 loc) · 2.88 KB
/
Copy pathbackend-deploy.yml
File metadata and controls
118 lines (102 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
name: Deploy Backend
on:
workflow_dispatch:
env:
EC2_HOST: ${{ secrets.EC2_HOST }}
EC2_USER: ${{ secrets.EC2_USER }}
permissions:
contents: read
id-token: write
jobs:
test:
name: Test Backend
runs-on: ubuntu-latest
defaults:
run:
working-directory: backend
services:
postgres:
image: postgres:15-alpine
env:
POSTGRES_DB: communityhub_test
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: backend/package-lock.json
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm run test
env:
DB_HOST: localhost
DB_PORT: 5432
DB_USERNAME: postgres
DB_PASSWORD: password
DB_DATABASE: communityhub_test
JWT_SECRET: test-secret
- name: Build application
run: npm run build
deploy:
name: Deploy to EC2
runs-on: ubuntu-latest
needs: test
if: github.ref == 'refs/heads/main'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Deploy to EC2 via SSH
uses: appleboy/ssh-action@v1.1.0
with:
host: ${{ env.EC2_HOST }}
username: ${{ env.EC2_USER }}
key: ${{ secrets.EC2_SSH_KEY }}
script: |
cd ~/CommunityHub
git pull origin main
cd backend
cat > .env << 'EOL'
NODE_ENV=production
DB_HOST=postgres
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=password
DB_DATABASE=communityhub
JWT_SECRET=${{ secrets.JWT_SECRET }}
GOOGLE_CLIENT_ID=${{ secrets.GOOGLE_CLIENT_ID }}
GOOGLE_CLIENT_SECRET=${{ secrets.GOOGLE_CLIENT_SECRET }}
FRONTEND_URL=http://${{ env.EC2_HOST }}:3001
EOL
docker-compose down
docker-compose up --build -d
sleep 30
curl -f http://localhost:3000/api/meetings || echo "Health check failed but continuing..."
notify:
name: Notify Deployment
runs-on: ubuntu-latest
needs: [test, deploy]
if: always()
steps:
- name: Notify Success
if: needs.deploy.result == 'success'
run: |
echo "Deployment successful"
echo "Application URL: http://${{ secrets.EC2_HOST }}:3000"
- name: Notify Failure
if: needs.deploy.result == 'failure'
run: |
echo "Deployment failed"
exit 1