-
Notifications
You must be signed in to change notification settings - Fork 0
121 lines (99 loc) · 3.71 KB
/
deployment.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
name: Production
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Deploy to server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.DEV_HOST }}
username: ${{ secrets.DEV_USERNAME }}
port: ${{ secrets.DEV_PORT }}
key: ${{ secrets.DEV_SSHKEY }}
script: |
#!/usr/bin/env bash
# Initialization
PROJECT="ZentinelJS"
REPOSITORY="https://github.com/surelle-ha/ZentinelJS"
BRANCH="main"
DIRECTORY="/var/www/html"
DEPENDENCY="wget curl git btop nodejs npm figlet"
# PREPARE
if ! sudo apt install $DEPENDENCY; then
echo "[$( date '+%F_%H:%M:%S' )] Dependency installation failed."
exit 1
fi
# CHECK IF GIT AUTHENTICATED
git ls-remote $REPOSITORY &> /dev/null
if [ $? -eq 0 ]; then
echo "[$( date '+%F_%H:%M:%S' )] Git is authenticated."
else
echo "[$( date '+%F_%H:%M:%S' )] Git is not authenticated."
exit 1
fi
# DEPLOYMENT
clear
figlet $PROJECT
echo "[$( date '+%F_%H:%M:%S' )] Deployment started"
if [ ! -d "$DIRECTORY" ]; then
mkdir -p "$DIRECTORY"
echo "[$( date '+%F_%H:%M:%S' )] Directory $DIRECTORY created."
else
echo "[$( date '+%F_%H:%M:%S' )] Directory $DIRECTORY already exists."
fi
cd $DIRECTORY
# IF PROJECT NOT EXIST
if [ ! -d "$PROJECT" ]; then
echo "[$( date '+%F_%H:%M:%S' )] Clone project from the repository"
if ! git clone $REPOSITORY; then
echo "[$( date '+%F_%H:%M:%S' )] Failed to clone repository"
exit 1
fi
cd $PROJECT
echo "[$( date '+%F_%H:%M:%S' )] NPM installation started"
if ! npm install; then
echo "[$( date '+%F_%H:%M:%S' )] NPM install failed"
exit 1
fi
echo "[$( date '+%F_%H:%M:%S' )] Create environment template"
if ! cp .env.example .env; then
echo "[$( date '+%F_%H:%M:%S' )] Creation environment template failed"
exit 1
fi
echo "[$( date '+%F_%H:%M:%S' )] Migration started"
if ! npm run db:up; then
echo "[$( date '+%F_%H:%M:%S' )] Migration failed"
exit 1
fi
echo "[$( date '+%F_%H:%M:%S' )] Run Application"
if ! npm run dev; then
echo "[$( date '+%F_%H:%M:%S' )] Execution failed"
exit 1
fi
# IF PROJECT ALREADY EXIST
else
cd $PROJECT
echo "[$( date '+%F_%H:%M:%S' )] Pull new resources from the Repository"
if ! git pull origin $BRANCH; then
echo "Failed to pull latest code"
exit 1
fi
echo "[$( date '+%F_%H:%M:%S' )] NPM installation started"
if ! npm install; then
echo "NPM install failed"
exit 1
fi
if ! npm run db:up; then
echo "Database migration failed"
exit 1
fi
fi
echo "[$( date '+%F_%H:%M:%S' )] Deployment Complete"