forked from andongmin94/kkakka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
166 lines (149 loc) Β· 6.74 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
pipeline {
agent any
environment {
BuildGradle = credentials('build.gradle')
Mat_Endpoint = credentials('CICD_mat_endpoint')
// AWS_SECRET_ACCESS_KEY = credentials('jenkins-aws-secret-access-key')
}
stages {
stage('MM-Alarm'){
steps{
script {
def Author_ID = sh(script: "git show -s --pretty=%an", returnStdout: true).trim()
def Author_Name = sh(script: "git show -s --pretty=%ae", returnStdout: true).trim()
mattermostSend (
color: '#D0E0E3',
icon: "https://jenkins.io/images/logos/jenkins/jenkins.png",
message: "νμ΄νλΌμΈ μμ: ${env.JOB_NAME} #${env.BUILD_NUMBER} by ${Author_ID}(${Author_Name})\n(<${env.BUILD_URL}|Details>)"
)
}
}
}
stage('Clone') {
steps {
echo 'ν΄λ‘ μ μμ!'
git branch: 'dev', credentialsId: 'docker-hub', url: 'https://lab.ssafy.com/s10-webmobile2-sub2/S10P12D110.git'
echo 'ν΄λ‘ μ μλ£!'
}
}
stage('BE-Build') {
steps {
echo 'λ°±μλ λΉλ λ° ν
μ€νΈ μμ!'
dir("./backend") {
sh "chmod +x ./gradlew"
// sh "touch ./build.gradle"
// application properties νμΌ λ³΅μ¬
// sh "echo $BuildGradle > ./build.gradle"
sh "./gradlew clean build --exclude-task test"
}
echo 'λ°±μλ λΉλ λ° ν
μ€νΈ μλ£!'
}
}
stage('Build Back Docker Image') {
steps {
echo 'λ°±μλ λ컀 μ΄λ―Έμ§ λΉλ μμ!'
dir("./backend") {
// λΉλλ JAR νμΌμ Docker μ΄λ―Έμ§λ‘ λΉλ
sh "docker build -t osy9536/ssafy-be:latest ."
}
echo 'λ°±μλ λ컀 μ΄λ―Έμ§ λΉλ μλ£!'
}
}
stage('Push to Docker Hub-BE') {
steps {
echo 'λ°±μλ λ컀 μ΄λ―Έμ§λ₯Ό Docker Hubμ νΈμ μμ!'
withCredentials([usernamePassword(credentialsId: 'docker-hub', usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
sh "docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD"
}
dir("./backend") {
sh "docker push osy9536/ssafy-be:latest"
}
echo 'λ°±μλ λ컀 μ΄λ―Έμ§λ₯Ό Docker Hubμ νΈμ μλ£!'
}
}
stage('Deploy to EC2-BE') {
steps {
echo 'λ°±μλ EC2μ λ°°ν¬ μμ!'
// μ¬κΈ°μμλ SSH νλ¬κ·ΈμΈμ΄λ SSH μ€ν¬λ¦½νΈλ₯Ό μ¬μ©νμ¬ EC2λ‘ μ°κ²°νκ³ Docker 컨ν
μ΄λ μ€ν
sshagent(['aws-key']) {
sh "docker rm -f backend"
sh "docker rmi osy9536/ssafy-be:latest"
sh "docker image prune -f"
sh "docker pull osy9536/ssafy-be:latest && docker run -d -p 8080:8080 --name backend osy9536/ssafy-be:latest"
}
echo 'λ°±μλ EC2μ λ°°ν¬ μλ£!'
}
}
stage('FE-Build') {
steps {
echo 'νλ‘ νΈ λΉλ λ° ν
μ€νΈ μμ!'
dir("./frontend") {
sh "npm install"
sh "npm run build"
}
echo 'νλ‘ νΈ λΉλ λ° ν
μ€νΈ μλ£!'
}
}
stage('Build Front Docker Image') {
steps {
echo 'νλ‘ νΈ λ컀 μ΄λ―Έμ§ λΉλ μμ!'
dir("./frontend") {
// λΉλλ νμΌμ Docker μ΄λ―Έμ§λ‘ λΉλ
sh "docker build -t osy9536/ssafy-fe:latest ."
}
echo 'νλ‘ νΈ λ컀 μ΄λ―Έμ§ λΉλ μλ£!'
}
}
stage('Push to Docker Hub-FE') {
steps {
echo 'νλ‘ νΈ λ컀 μ΄λ―Έμ§λ₯Ό Docker Hubμ νΈμ μμ!'
withCredentials([usernamePassword(credentialsId: 'docker-hub', usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
sh "docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD"
}
dir("./frontend") {
sh "docker push osy9536/ssafy-fe:latest"
}
echo 'νλ‘ νΈ λ컀 μ΄λ―Έμ§λ₯Ό Docker Hubμ νΈμ μλ£!'
}
}
stage('Deploy to EC2-FE') {
steps {
echo 'νλ‘ νΈ EC2μ λ°°ν¬ μμ!'
// μ¬κΈ°μμλ SSH νλ¬κ·ΈμΈμ΄λ SSH μ€ν¬λ¦½νΈλ₯Ό μ¬μ©νμ¬ EC2λ‘ μ°κ²°νκ³ Docker 컨ν
μ΄λ μ€ν
sshagent(['aws-key']) {
sh "docker rm -f frontend"
sh "docker rmi osy9536/ssafy-fe:latest"
sh "docker image prune -f"
sh "docker pull osy9536/ssafy-fe:latest && docker run -d -p 3000:3000 --name frontend osy9536/ssafy-fe:latest"
}
echo 'νλ‘ νΈ EC2μ λ°°ν¬ μλ£!'
}
}
}
post {
success {
echo 'νμ΄νλΌμΈμ΄ μ±κ³΅μ μΌλ‘ μλ£λμμ΅λλ€!'
script {
def Author_ID = sh(script: "git show -s --pretty=%an", returnStdout: true).trim()
def Author_Name = sh(script: "git show -s --pretty=%ae", returnStdout: true).trim()
mattermostSend (
color: '#D0E0E3',
icon: "https://jenkins.io/images/logos/jenkins/jenkins.png",
message: "λΉλ μ±κ³΅: ${env.JOB_NAME} #${env.BUILD_NUMBER} by ${Author_ID}(${Author_Name})\n(<${env.BUILD_URL}|Details>)"
)
}
}
failure {
echo 'νμ΄νλΌμΈμ΄ μ€ν¨νμμ΅λλ€. μλ¬λ₯Ό νμΈνμΈμ.'
script {
def Author_ID = sh(script: "git show -s --pretty=%an", returnStdout: true).trim()
def Author_Name = sh(script: "git show -s --pretty=%ae", returnStdout: true).trim()
mattermostSend (
color: '#D0E0E3',
icon: "https://4.bp.blogspot.com/-52EtGjEhW-k/UtOBXa1fhVI/AAAAAAAABbU/Lk4ZBYcvZrY/s1600/download.jpeg",
message: "λΉλ μ€ν¨: ${env.JOB_NAME} #${env.BUILD_NUMBER} by ${Author_ID}(${Author_Name})\n(<${env.BUILD_URL}|Details>)"
)
}
}
}
}