-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
105 lines (83 loc) · 2.22 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
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('sonar and maven'){
parallel{
stage('Sonar Report'){
steps{
withSonarQubeEnv('sonar-7.5') {
sh "mvn sonar:sonar"
}
timeout(time: 1, unit: 'HOURS') {
script{
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
}
}
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{
// input 'Do you want to deploy to dev servers?'
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
}