-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
137 lines (104 loc) · 4.35 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!groovy
import com.cisco.k8spipeline.shared.K8sPipelineConfig
@Library('k8s-pipeline')
import com.cisco.k8spipeline.shared.Steps
def k8sSteps = new Steps()
// import PipelineUtils for sending SparkMessage
@Library('cccPipeline') _
def pipelineUtils = new PipelineUtils()
// Keep track of current stage being executed
def currentStage
// Boolean Flag to keep a check on if deploymentFailed in any of the stages.
// Used in finally block to check whether a success/failure Wxt alert
// should be sent
def deploymentFailed = false
// record any errors in the error variable and use to send alert in finally block
def error
// branch name used for build
def branchName = scm.branches[0].name
// appNamePrefix is used for isolated deployments
// canary and service validation tests
def appNamePrefix = env.APPNAME_PREFIX
appNamePrefix = (appNamePrefix.equals("NA")) ? "" : appNamePrefix
node(NODE_LABEL) {
try {
def path = 'knowledge-base/topic-redact'
withEnv(["APPNAME_PREFIX=$appNamePrefix"]) {
stage('Prep') {
echo 'Setting up env...'
currentStage = env.STAGE_NAME
echo "Running ${currentStage} stage"
def config = new K8sPipelineConfig()
.withAppName('topic-redact')
.withChartDir("${path}/chart")
.withReleaseType(env.RELEASE_TYPE)
k8sSteps.setEnv(config)
}
stage('Build and push the application container image') {
echo 'Building and publishing image...'
currentStage = env.STAGE_NAME
echo "Running ${currentStage} stage"
checkout scm
def buildCommand = { tag ->
sh "docker build ${path}/code/ -t ${tag}"
}
k8sSteps.buildAndPushDockerImage(buildCommand)
}
stage('Build and Publish helm Chart') {
echo 'Building and publishing chart...'
currentStage = env.STAGE_NAME
echo "Running ${currentStage} stage"
def chartVersion = k8sSteps.utils.getChartVersion()
def gitCommit = k8sSteps.utils.getGitCommit()
def split = chartVersion.split('\\.')
switch (env.RELEASE_TYPE) {
case "patch":
split[2] = Integer.parseInt(split[2]) + 1
break
case "minor":
split[1] = Integer.parseInt(split[1]) + 1
break
case "major":
split[0] = Integer.parseInt(split[0]) + 1
break
}
chartVersion = split.join('.')
withCredentials([string(credentialsId: APPSECRET, variable: 'SECRET_ID')]) {
withEnv(["VAULT_ROLE_SERVICES=$APPROLE", "VAULT_SECRET_SERVICES=${SECRET_ID}", "CHART_VERSION=$chartVersion", "GIT_COMMIT=$gitCommit"]) {
k8sSteps.buildAndPushHelmChart()
}
}
}
stage('Deploy application') {
echo "Deploying env.APPNAME..."
currentStage = env.STAGE_NAME
echo "Running ${currentStage} stage"
k8sSteps.deployHelmChart()
}
}
} catch (Exception e) {
// deployment failed. Set flag to true
deploymentFailed = true
echo "Deployment failed at ${currentStage}"
// copy exception msg into error
error = e.toString()
throw e
} catch (Error e) {
// deployment failed. Set flag to true
deploymentFailed = true
echo "Deployment failed at ${currentStage}"
// copy exception msg into error
error = e.toString()
throw e
} finally {
def message = "Job Name: $JOB_NAME (${env.APPNAME}) [${currentBuild.displayName}](${currentBuild.absoluteUrl})\n"
echo "Deployment failures?: ${deploymentFailed}"
if (deploymentFailed) {
message += "Deployment **Failed at ${currentStage}** stage\n"
message += "Exception: ${error}\n"
} else {
message += "Deployment Succeeded\n"
}
// pipelineUtils.sendSparkMessage(message, CRED_ID, SPACE_ID, SPACE_NAME)
}
}