-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
72 lines (67 loc) · 2.21 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
def branch = "main"
def repo = "https://github.com/wilsonakbar/wayshub-backend.git"
def cred = "ditoihkam"
def dir = "~/wayshub-backend"
def server = "[email protected]"
def imagename = "wayshub-be"
def dockerusername = "wilsonakbar"
pipeline {
agent any
stages {
stage('Pull From Repository') {
steps {
sshagent([cred]) {
sh """ssh -o StrictHostKeyChecking=no ${server} << EOF
cd ${dir}
git remote add origin ${repo} || git remote set-url origin ${repo}
git pull origin ${branch}
exit
EOF
"""
}
}
}
stage('Dockerize') {
steps {
sshagent([cred]) {
sh """ssh -o StrictHostKeyChecking=no ${server} << EOF
cd ${dir}
docker build -t ${imagename}:latest .
exit
EOF
"""
}
}
}
stage('Deploy Docker') {
steps {
sshagent([cred]) {
sh """ssh -o StrictHostKeyChecking=no ${server} << EOF
cd ${dir}
docker container stop ${imagename}
docker container rm ${imagename}
docker run -d -p 5000:5000 --name="${imagename}" ${imagename}:latest
docker container stop ${imagename}
docker container rm ${imagename}
exit
EOF
"""
}
}
}
stage('Push to Docker Hub') {
steps {
sshagent([cred]) {
sh """ssh -o StrictHostKeyChecking=no ${server} << EOF
docker tag ${imagename}:latest ${dockerusername}/${imagename}:latest
docker image push ${dockerusername}/${imagename}:latest
docker image rm ${dockerusername}/${imagename}:latest
docker image rm ${imagename}:latest
exit
EOF
"""
}
}
}
}
}