-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
jenkins-oct-19-pipeline
73 lines (61 loc) · 1.7 KB
/
jenkins-oct-19-pipeline
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
pipeline{
agent any
environment {
PATH = "${PATH}:${getMavenPath()}"
DOCKER_TAG = "${getLatestCommitId()}"
NEXUS_HOST = "172.31.45.145:8083"
DEV_IP = "172.31.6.150"
}
stages{
stage('SCM - Checkout'){
steps{
git url: 'https://github.com/javahometech/myweb'
}
}
stage('Maven - Build'){
steps{
sh "mvn clean package"
}
}
stage('Docker - Build'){
steps{
withCredentials([string(credentialsId: 'nexus-docker', variable: 'nexusPwd')]) {
sh "docker login -u admin -p ${nexusPwd} ${NEXUS_HOST}"
}
sh "docker build . -t ${NEXUS_HOST}/myweb:${DOCKER_TAG} "
}
}
stage('Docker - Push'){
steps{
sh "docker push ${NEXUS_HOST}/myweb:${DOCKER_TAG}"
}
}
stage('Docker - Deploy - Dev'){
steps{
sh returnStatus: true, script: "ssh ec2-user@${DEV_IP} docker rm -f myweb"
withCredentials([string(credentialsId: 'nexus-docker', variable: 'nexusPwd')]) {
sh returnStatus: true, script: "ssh ec2-user@${DEV_IP} docker login -u admin -p ${nexusPwd} ${NEXUS_HOST}"
}
sh returnStatus: true, script: 'ssh ec2-user@${DEV_IP} docker rmi $(docker images | grep 172.31.45.145:8083/myweb | awk \'{print $3}\')'
sh "ssh ec2-user@${DEV_IP} docker run -d -p 8080:8080 --name=myweb ${NEXUS_HOST}/myweb:${DOCKER_TAG}"
}
}
}
post{
always{
mail body: """Hi Dev Team,
${env.BUILD_URL}
This Job ran
Thanks,
DevOps Team""", subject: "${env.JOB_NAME} Ran", to: '[email protected]'
}
}
}
def getMavenPath(){
def mvnHome = tool name: 'maven-3', type: 'maven'
return "${mvnHome}/bin"
}
def getLatestCommitId(){
def commitId = sh returnStdout: true, script: 'git rev-parse HEAD'
return commitId
}