-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathJenkinsfile
117 lines (96 loc) · 3.56 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
113
114
115
116
117
def CONTAINER_NAME = "calculator"
def ENV_NAME = getEnvName(env.BRANCH_NAME)
def CONTAINER_TAG = getTag(env.BUILD_NUMBER, env.BRANCH_NAME)
def HTTP_PORT = getHTTPPort(env.BRANCH_NAME)
def EMAIL_RECIPIENTS = "[email protected]"
node {
try {
stage('Initialize') {
def dockerHome = tool 'DockerLatest'
def mavenHome = tool 'MavenLatest'
env.PATH = "${dockerHome}/bin:${mavenHome}/bin:${env.PATH}"
}
stage('Checkout') {
checkout scm
}
stage('Build with test') {
sh "mvn clean install"
}
stage('Sonarqube Analysis') {
withSonarQubeEnv('SonarQubeLocalServer') {
sh " mvn sonar:sonar -Dintegration-tests.skip=true -Dmaven.test.failure.ignore=true"
}
timeout(time: 1, unit: 'MINUTES') {
def qg = waitForQualityGate() // Reuse taskId previously collected by withSonarQubeEnv
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
stage("Image Prune") {
imagePrune(CONTAINER_NAME)
}
stage('Image Build') {
imageBuild(CONTAINER_NAME, CONTAINER_TAG)
}
stage('Push to Docker Registry') {
withCredentials([usernamePassword(credentialsId: 'DockerhubCredentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
pushToImage(CONTAINER_NAME, CONTAINER_TAG, USERNAME, PASSWORD)
}
}
stage('Run App') {
withCredentials([usernamePassword(credentialsId: 'DockerhubCredentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
runApp(CONTAINER_NAME, CONTAINER_TAG, USERNAME, HTTP_PORT, ENV_NAME)
}
}
} finally {
deleteDir()
sendEmail(EMAIL_RECIPIENTS);
}
}
def imagePrune(containerName) {
try {
sh "docker image prune -f"
sh "docker stop $containerName"
} catch (ignored) {
}
}
def imageBuild(containerName, tag) {
sh "docker build -t $containerName:$tag -t $containerName --pull --no-cache ."
echo "Image build complete"
}
def pushToImage(containerName, tag, dockerUser, dockerPassword) {
sh "docker login -u $dockerUser -p $dockerPassword"
sh "docker tag $containerName:$tag $dockerUser/$containerName:$tag"
sh "docker push $dockerUser/$containerName:$tag"
echo "Image push complete"
}
def runApp(containerName, tag, dockerHubUser, httpPort, envName) {
sh "docker pull $dockerHubUser/$containerName"
sh "docker run --rm --env SPRING_ACTIVE_PROFILES=$envName -d -p $httpPort:$httpPort --name $containerName $dockerHubUser/$containerName:$tag"
echo "Application started on port: ${httpPort} (http)"
}
def sendEmail(recipients) {
mail(
to: recipients,
subject: "Build ${env.BUILD_NUMBER} - ${currentBuild.currentResult} - (${currentBuild.fullDisplayName})",
body: "Check console output at: ${env.BUILD_URL}/console" + "\n")
}
String getEnvName(String branchName) {
if (branchName == 'main') {
return 'prod'
}
return (branchName == 'ready') ? 'uat' : 'dev'
}
String getHTTPPort(String branchName) {
if (branchName == 'main') {
return '9999'
}
return (branchName == 'ready') ? '8888' : '8090'
}
String getTag(String buildNumber, String branchName) {
if (branchName == 'main') {
return buildNumber + '-stable'
}
return buildNumber + '-unstable'
}