-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
112 lines (108 loc) · 3.32 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
// run:
// docker run --name jenkins -d -p 8080:8080 -p 50000:50000 -v ~/jenkins:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -u root jenkins/jenkins:v1
def mainDir="./"
def ecrLoginHelper="docker-credential-ecr-login"
def region="ap-northeast-2"
def ecrUrl="152011405160.dkr.ecr.ap-northeast-2.amazonaws.com"
def repository="jenkins"
def deployHost="172.32.24.124"
def aws_credential_name="aws-key"
pipeline {
agent any
tools {
jdk("docker -java")
}
stages {
stage('Pull Codes from Github'){
steps{
checkout scm
}
post {
success {
echo 'success pull project'
}
failure {
error 'fail pull project' // exit pipeline
}
}
}
stage('Build Codes by Gradle') {
steps {
sh """
cd ${mainDir}
chmod +x ./gradlew
./gradlew bootJar
"""
}
post {
success {
echo 'success build project'
}
failure {
error 'fail build project' // exit pipeline
}
}
}
stage('dockerizing project by dockerfile') {
steps {
script{
sh """
docker build -t ${ecrUrl}/${repository}:${currentBuild.number} .
docker tag ${ecrUrl}/${repository}:${currentBuild.number} ${ecrUrl}/${repository}:latest
"""
}
}
post {
success {
echo 'success dockerizing project'
}
failure {
error 'fail dockerizing project' // exit pipeline
}
}
}
stage('upload aws ECR') {
steps {
script{
// cleanup current user docker credentials
sh """
rm -f ~/.dockercfg ~/.docker/config.json || true
"""
docker.withRegistry("https://${ecrUrl}", "ecr:${region}:${aws_credential_name}") {
docker.image("${ecrUrl}/${repository}:${currentBuild.number}").push()
docker.image("${ecrUrl}/${repository}:latest").push()
}
}
}
post {
success {
echo 'success upload image'
}
failure {
error 'fail upload image' // exit pipeline
}
}
}
stage('Deploy to AWS EC2 VM'){
steps{
sshagent(credentials : ["deploy-key"]) {
sh """
ssh -o StrictHostKeyChecking=no ubuntu@${deployHost} \
'docker stop sb-server && docker kill sb-server;' \
'docker rm -f sb-server;' \
'aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin ${ecrUrl};' \
'docker run -d -p 8080:8080 --name sb-server -t ${ecrUrl}/${repository}:${currentBuild.number}'
"""
}
}
post {
success {
echo 'success Deploy'
}
failure {
error 'fail Deploy' // exit pipeline
}
}
}
}
}