-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
95 lines (84 loc) · 2.42 KB
/
Jenkinsfile
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
pipeline {
agent any
environment {
PRODUCTION_IP_ADDRESS = '172.31.16.34'
}
tools {
nodejs "nodejs"
}
stages {
stage('Install Packages') {
steps {
script {
sh 'yarn install'
}
}
}
stage('Run the App') {
steps {
script {
sh 'yarn start:pm2'
sleep 5
}
}
}
stage('Test the app') {
steps {
script {
sh 'curl http://localhost:3000/health'
}
}
}
stage('Stop the App') {
steps {
script {
sh 'pm2 stop todos-app'
}
}
}
stage('Debug Environment Variable') {
steps {
script {
echo "PRODUCTION_IP_ADDRESS: $PRODUCTION_IP_ADDRESS"
}
}
}
stage('Add Host to known_hosts') {
steps {
script {
sh '''
mkdir -p /var/lib/jenkins/.ssh
ssh-keyscan -H $PRODUCTION_IP_ADDRESS >> /var/lib/jenkins/.ssh/known_hosts
'''
}
}
}
stage('Deploy') {
environment {
DEPLOY_SSH_KEY = credentials('AWS_INSTANCE_SSH')
}
steps {
script {
sh '''
chmod 600 $DEPLOY_SSH_KEY
ssh -v -i $DEPLOY_SSH_KEY ubuntu@$PRODUCTION_IP_ADDRESS '
if [ ! -d "todos-app" ]; then
git clone https://github.com/sagarmondi/code_analysis.git code_analysis
cd code_analysis
else
cd code_analysis
git pull
fi
yarn install
if pm2 describe code_analysis > /dev/null ; then
pm2 restart code_analysis
else
yarn start:pm2
fi
'
'''
}
}
}
}
}