-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile.deploy
More file actions
122 lines (109 loc) · 3.93 KB
/
Jenkinsfile.deploy
File metadata and controls
122 lines (109 loc) · 3.93 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
119
120
121
122
node {
// Clean the workspace before starting the pipeline
cleanWs()
// Define environment variables
def NODE_VERSION = '22.11.0'
def NPM_VERSION = '10.8.3'
def FRONT_DIR = '/var/www/AM-FRONT'
def BACK_DIR = '/var/www/AM-BACK'
// Use credentials for sensitive data
withCredentials([
string(credentialsId: 'DATABASE_HOST', variable: 'DATABASE_HOST'),
string(credentialsId: 'DATABASE_PORT', variable: 'DATABASE_PORT'),
string(credentialsId: 'DATABASE_USER', variable: 'DATABASE_USER'),
string(credentialsId: 'DATABASE_PASSWORD', variable: 'DATABASE_PASSWORD'),
string(credentialsId: 'DATABASE_NAME', variable: 'DATABASE_NAME'),
string(credentialsId: 'PORT', variable: 'PORT'),
string(credentialsId: 'JWT_SECRET', variable: 'JWT_SECRET'),
string(credentialsId: 'FRONTEND_URL', variable: 'FRONTEND_URL'),
string(credentialsId: 'MAIL_USER', variable: 'MAIL_USER'),
string(credentialsId: 'MAIL_PASS', variable: 'MAIL_PASS'),
string(credentialsId: 'SALT_ROUNDS', variable: 'SALT_ROUNDS'),
string(credentialsId: 'STRIPE_SECRET_KEY', variable: 'STRIPE_SECRET_KEY'),
]) {
stage('Checkout') {
git branch: 'develop', url: 'git@github.com:Reednar/ambiance.git'
}
stage('Setup Environment') {
sh '''
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
echo "Using Node.js version: $(node -v)"
echo "Using npm version: $(npm -v)"
'''
}
stage('Install Dependencies') {
parallel(
"Install Frontend Dependencies": {
sh 'cd ambiance-front && npm install'
},
"Install Backend Dependencies": {
sh 'cd ambiance-back && npm ci'
}
)
}
stage('Build Projects') {
parallel(
"Build Angular Frontend": {
timeout(time: 30, unit: 'MINUTES') {
sh 'cd ambiance-front && node --max-old-space-size=4096 ./node_modules/@angular/cli/bin/ng build'
}
},
"Build NestJS Backend": {
timeout(time: 30, unit: 'MINUTES') {
sh 'cd ambiance-back && NODE_OPTIONS="--max-old-space-size=4096" npm run build'
}
}
)
}
stage('Deploy Angular') {
sh """
rm -rf ${FRONT_DIR}/*
cp -r ambiance-front/dist/* ${FRONT_DIR}/
"""
}
stage('Deploy NestJS') {
sh """
rm -rf ${BACK_DIR}/*
cp -r ambiance-back/dist/* ${BACK_DIR}/
cp ambiance-back/package*.json ${BACK_DIR}/
"""
}
stage('Install Backend Production Dependencies') {
sh """
cd ${BACK_DIR}
npm ci
"""
}
stage('Inject .env') {
sh """
rm -f ${BACK_DIR}/.env
cat <<EOF > ${BACK_DIR}/.env
PORT=${PORT}
DATABASE_HOST=${DATABASE_HOST}
DATABASE_PORT=${DATABASE_PORT}
DATABASE_USER=${DATABASE_USER}
DATABASE_PASSWORD=${DATABASE_PASSWORD}
DATABASE_NAME=${DATABASE_NAME}
JWT_SECRET=${JWT_SECRET}
FRONTEND_URL=${FRONTEND_URL}
MAIL_USER=${MAIL_USER}
MAIL_PASS=${MAIL_PASS}
SALT_ROUNDS=${SALT_ROUNDS}
STRIPE_SECRET_KEY=${STRIPE_SECRET_KEY}
EOF
"""
}
stage('Restart NestJS') {
sh """
cd ${BACK_DIR}
if pm2 describe nestjs > /dev/null; then
pm2 restart nestjs --update-env --cwd ${BACK_DIR}
else
pm2 start main.js --name nestjs --cwd ${BACK_DIR} --interpreter \$(which node)
fi
pm2 save
"""
}
}
}