-
Notifications
You must be signed in to change notification settings - Fork 5
183 lines (181 loc) · 7.38 KB
/
deploy.yml
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
name: Deploy DEV
env:
JWT_SIGNATURE: ${{ vars.JWT_SIGNATURE }}
CORS_ALLOW_URL: ${{ vars.CORS_ALLOW_URL }}
API_PORT: ${{ vars.API_PORT }}
on:
push:
branches:
- dev
- main
workflow_dispatch:
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: latest
registry-url: 'https://registry.npmjs.org'
- name: check triggering branch
run: echo ${{ github.ref }}
- name: Set Database URL
run: |
if [ "${{ github.ref }}" == "refs/heads/main" ]; then
echo "DATABASE_URL=${{ vars.PROD_PDATABASE_URL }}" >> $GITHUB_ENV
echo "SERVER_HOST=${{ vars.PROD_SERVER_HOST }}" >> $GITHUB_ENV
echo "SERVER_USERNAME=${{ vars.PROD_SERVER_USERNAME }}" >> $GITHUB_ENV
elif [ "${{ github.ref }}" == "refs/heads/dev" ]; then
echo "DATABASE_URL=${{ vars.DEV_PDATABASE_URL }}" >> $GITHUB_ENV
echo "SERVER_HOST=${{ vars.DEV_SERVER_HOST }}" >> $GITHUB_ENV
echo "SERVER_USERNAME=${{ vars.DEV_SERVER_USERNAME }}" >> $GITHUB_ENV
fi
- name: Check the Environment Variable
run: |
echo "DATABASE_URL is $DATABASE_URL"
- name: pwd
run: pwd
- name: ls
run: ls -la
- name: check which branch
run: git branch
- name: yarn install
run: yarn install
- name: Install wait-port
run: npm install -g wait-port
- name: start api
run: |
cd api
yarn build
yarn start:prod > api.log 2>&1 &
- name: Wait for API to start
run: wait-port -t 60000 localhost:3300
continue-on-error: true
- name: Test API health with curl
if: ${{ success() }}
run: curl -si http://localhost:3300/api/v1/hello
- name: Display API logs failure
if: ${{ failure() }}
run: |
cat api/api.log
exit 1
- name: Display API logs
run: cat api/api.log
- name: Install serve
run: npm install -g serve
- name: start frontend
run: |
cd frontend
yarn build
serve -l 3000 -s dist > front.log 2>&1 &
- name: Wait for front to start
run: wait-port -t 60000 localhost:3000
continue-on-error: true
- name: Test front health with curl
if: ${{ success() }}
run: curl -si http://localhost:3000/
- name: Display Front logs failure
if: ${{ failure() }}
run: |
cat frontend/front.log
exit 1
- name: Display Front logs
run: cat frontend/front.log
- name: Deploy to Server
uses: appleboy/ssh-action@master
with:
host: ${{ env.SERVER_HOST }}
username: ${{ env.SERVER_USERNAME }}
key: ${{ vars.SSH_PRIVATE_KEY }}
port: ${{ vars.SSH_PORT }}
script: |
echo "--------"
echo "### CMD: cd BikeHub"
cd BikeHub
echo "Done!"
echo "--------"
echo "### CMD: git reset --hard"
git reset --hard
echo "Done!"
echo "--------"
echo "### CMD: find . -name 'package-lock.json' -exec rm -f {} +"
find . -name 'package-lock.json' -exec rm -f {} +
echo "### CMD: find . -name 'yarn.lock' -exec rm -f {} +"
find . -name 'yarn.lock' -exec rm -f {} +
echo "Done!"
echo "--------"
branch_name=$(basename "${{ github.ref }}")
echo "### CMD: git switch $branch_name"
git switch "$branch_name"
echo "Done!"
echo "--------"
echo "### CMD: git pull origin $branch_name"
git pull origin $branch_name
echo "Done!"
echo "--------"
echo "### CMD: npm install &deploy.txt& yarn install"
npm install && yarn install
echo "Done!"
echo "--------"
echo "### CMD: cd ~/BikeHub/api && cp .env.example .env"
cd ~/BikeHub/api && cp .env.example .env
echo "Done!"
echo "--------"
if [ "${{ github.ref }}" = "refs/heads/main" ]; then
echo "start deploying in ${{ github.ref }}"
echo "### CMD: cd ~/BikeHub/api && yarn build"
cd ~/BikeHub/api && yarn build
echo "### CMD: pm2 reload startapi --update-env"
pm2 reload startapi --update-env
echo "### CMD: cd ~/BikeHub/frontend && yarn build"
cd ~/BikeHub/frontend && yarn build
echo "### CMD: pm2 reload startfrontend --update-env"
pm2 reload startfrontend --update-env
echo "### CMD: pm2 reload prisma --update-env"
pm2 reload prisma --update-env
echo "### CMD: update deploy.txt"
echo "---------------------------" >> deploy.txt
echo "Date: $(date)" >> deploy.txt
echo "Branch: ${{ github.ref }}" >> deploy.txt
echo "Last Commit Message: ${{ github.event.head_commit.message }}" >> deploy.txt
echo "Last Commit Author: ${{ github.event.head_commit.author.name }}" >> deploy.txt
echo "Pusher: ${{ github.actor }}" >> deploy.txt
if [ "${{ github.event_name }}" = "pull_request" ]; then
echo "Pull Request Author: ${{ github.event.pull_request.user.login }}" >> deploy.txt
echo "Merged By: ${{ github.event.pull_request.merged_by.login }}" >> deploy.txt
fi
echo "### CMD: cp deploy.txt dist/deploy.txt"
cp deploy.txt dist/deploy.txt
echo "Done!"
fi
if [ "${{ github.ref }}" = "refs/heads/dev" ]; then
echo "start deploying in ${{ github.ref }}"
echo "### CMD: cd ~/BikeHub/api && yarn build"
cd ~/BikeHub/api && yarn build
echo "### CMD: pm2 reload startapi --update-env"
pm2 reload startapi --update-env
echo "### CMD: cd ~/BikeHub/frontend && yarn build"
cd ~/BikeHub/frontend && yarn build
echo "### CMD: pm2 reload startfrontend --update-env"
pm2 reload startfrontend --update-env
echo "### CMD: pm2 reload prisma --update-env"
pm2 reload prisma --update-env
echo "### CMD: update deploy.txt"
echo "---------------------------" >> deploy.txt
echo "Date: $(date)" >> deploy.txt
echo "Branch: ${{ github.ref }}" >> deploy.txt
echo "Last Commit Message: ${{ github.event.head_commit.message }}" >> deploy.txt
echo "Last Commit Author: ${{ github.event.head_commit.author.name }}" >> deploy.txt
echo "Pusher: ${{ github.actor }}" >> deploy.txt
if [ "${{ github.event_name }}" = "pull_request" ]; then
echo "Pull Request Author: ${{ github.event.pull_request.user.login }}" >> deploy.txt
echo "Merged By: ${{ github.event.pull_request.merged_by.login }}" >> deploy.txt
fi
echo "### CMD: cp deploy.txt dist/deploy.txt"
cp deploy.txt dist/deploy.txt
echo "Done!"
fi
echo "--------"