-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
81 lines (76 loc) · 2.87 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
pipeline {
agent any
environment {
dockerImage = ''
APP_NAME = 'hwanstory-frontend'
IMAGE_NAME = 'akon47/hwanstory-frontend'
IMAGE_TAG = "${env.BUILD_NUMBER}"
GITHUB_CREDENTIALS_ID = 'git-hub'
DOCKER_CREDENTIALS_ID = 'docker-hub'
}
stages {
stage('Clone') {
steps {
echo 'Clonning Repository'
git url: '[email protected]:akon47/hwanstory.git', branch: 'master', credentialsId: GITHUB_CREDENTIALS_ID, changelog: false
}
post {
success {
echo 'Successfully Cloned Repository'
}
failure {
error 'This pipeline stops here...'
}
}
}
stage('Bulid Docker') {
steps {
echo 'Bulid Docker'
script {
dockerImage = docker.build("${IMAGE_NAME}")
}
}
post {
failure {
error 'This pipeline stops here...'
}
}
}
stage('Push Docker') {
steps {
echo 'Push Docker'
script {
docker.withRegistry('', DOCKER_CREDENTIALS_ID) {
dockerImage.push("${IMAGE_TAG}")
dockerImage.push("latest")
}
}
}
post {
success {
sh 'docker rmi $(docker images -q -f dangling=true) || true'
}
failure {
error 'This pipeline stops here...'
}
}
}
stage('Docker Run') {
steps {
echo 'Pull Docker Image & Docker Image Run'
sshagent(credentials: ['ssh']) {
sh "ssh -o StrictHostKeyChecking=no [email protected] 'docker pull ${IMAGE_NAME}'"
sh "ssh -o StrictHostKeyChecking=no [email protected] 'docker ps -q -a --filter name=${APP_NAME} | grep -q . && docker rm -f \$(docker ps -aq --filter name=${APP_NAME}) || true'"
sh "ssh -o StrictHostKeyChecking=no [email protected] 'docker run -d --restart always --name ${APP_NAME} -v /var/www/html/sitemap:/usr/share/nginx/html/sitemap -v /var/www/html/static:/usr/share/nginx/html/static --net=host ${IMAGE_NAME}'"
sh "ssh -o StrictHostKeyChecking=no [email protected] 'docker images -qf dangling=true | xargs -I{} docker rmi {} || true'"
sh "ssh -o StrictHostKeyChecking=no [email protected] 'docker rmi ${IMAGE_NAME}:${IMAGE_TAG} || true'"
}
}
post {
failure {
error 'This pipeline stops here...'
}
}
}
}
}