-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
121 lines (114 loc) · 4.57 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
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
pipeline {
agent any
environment {
REMOTE_USER = 'ec2-user'
REMOTE_HOST = '3.35.128.151'
SSH_KEY = credentials('ssh-key-id')
}
stages {
stage('Prepare Environment') {
steps {
script {
// Install Docker if not already installed
sshagent(credentials: ['ssh-key-id']) {
sh '''
ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} '
if ! command -v docker &> /dev/null; then
echo "Docker is not installed. Installing Docker..."
sudo yum update -y
sudo amazon-linux-extras install docker -y
sudo service docker start
sudo usermod -a -G docker ${REMOTE_USER}
fi
'
'''
}
}
}
}
stage('Install Docker Compose') {
steps {
script {
// Install Docker Compose if not already installed
sshagent(credentials: ['ssh-key-id']) {
sh '''
ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} '
if ! command -v docker-compose &> /dev/null; then
echo "Docker Compose is not installed. Installing Docker Compose..."
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
fi
'
'''
}
}
}
}
stage('Clone Repository') {
steps {
script {
sshagent(credentials: ['ssh-key-id']) {
sh '''
ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} '
if [ -d "FoodGo-BE" ]; then
rm -rf FoodGo-BE
fi
git clone -b feat/security-login https://github.com/Food-GO/FoodGo-BE.git
'
'''
}
}
}
}
stage('Gradle Build') {
steps {
script {
sshagent(credentials: ['ssh-key-id']) {
sh '''
ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} '
cd FoodGo-BE/api-module &&
if [ -f "../gradlew" ]; then
chmod +x ../gradlew &&
../gradlew --no-daemon bootJar
else
echo "Gradle Wrapper (gradlew) not found"
exit 1
fi
'
'''
}
}
}
}
stage('Build Docker Image') {
steps {
script {
sshagent(credentials: ['ssh-key-id']) {
sh '''
ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} '
cd FoodGo-BE &&
docker build -t anjeonghoo/food_go .
'
'''
}
}
}
}
stage('Deploy with Docker Compose') {
steps {
script {
sshagent(credentials: ['ssh-key-id']) {
sh '''
ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} '
cd FoodGo-BE &&
docker-compose down &&
docker-compose up -d
'
'''
}
}
}
}
}
}