-
Notifications
You must be signed in to change notification settings - Fork 0
54 lines (43 loc) · 1.76 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
name: Deploy to Vultr
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up SSH
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.VULTR_SSH_PRIVATE_KEY }}
- name: Verify SSH connection
run: ssh -o StrictHostKeyChecking=no root@${{ secrets.VULTR_IP }} "echo 'SSH connection successful'"
# Git clone instead of SCP
- name: Deploy code to Vultr
run: ssh -o StrictHostKeyChecking=no root@${{ secrets.VULTR_IP }} "cd /root/deploy/happiness-backend && git pull || git clone [email protected]:WAI-laboratory/happiness-backend.git /root/deploy/happiness-backend"
# Add a step to install dependencies and build the project
- name: Install dependencies and build project
run: |
npm install
npm run build
- name: Build and run Docker container
run: |
ssh -o StrictHostKeyChecking=no root@${{ secrets.VULTR_IP }} "
# Build the image from the source code
docker build -t happiness-backend-image /root/deploy/happiness-backend;
# Stop the running container if it exists
CONTAINER_ID=\$(docker ps -q -f name=happiness-backend);
if [ ! -z \"\$CONTAINER_ID\" ]; then
docker container stop \$CONTAINER_ID;
fi;
# Remove the stopped container if it exists
CONTAINER_ID=\$(docker ps -a -q -f name=happiness-backend);
if [ ! -z \"\$CONTAINER_ID\" ]; then
docker container rm \$CONTAINER_ID;
fi;
# Run the newly built image
docker container run -d -p 3010:3010 --name happiness-backend --network app-network happiness-backend-image;
"