-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
158 lines (150 loc) · 7.97 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env groovy
pipeline {
agent { label 'devops' } // jenkins slave for this job
options {
timeout(time: 1, unit: 'HOURS') // timeout
disableConcurrentBuilds() // can multiple jobs run at same time
buildDiscarder(logRotator(numToKeepStr: '50')) // log history
}
parameters {
string(name: 'artifactId', defaultValue: '0', description: 'artifactId')
string(name: 'requirements_file', defaultValue: '0', description: 'raw url of requirements.yaml file')
string(name: 'branchName', defaultValue: 'master', description: 'branch for requirements.yaml file')
}
stages {
stage('Prepare Build') {
steps {
deleteDir() // start with a clean working directory
cleanWs() // clean jenkins workspace, each jenkins job runs in a workspace
// need the LocalBranch extension for the maven release plugin to work correctly
checkout([
$class : 'GitSCM',
branches : scm.branches,
extensions : scm.extensions + [[$class: 'LocalBranch']],
userRemoteConfigs: scm.userRemoteConfigs
])
script {
sh "echo '----------------------------------'"
workspace = pwd() // get the working directory
env.serviceName = params.artifactId // use artifactId as a ServiceName
echo params.artifactId
echo params.requirements_file
sh "echo '----------------------------------'"
//sh "git clone ${params.requirements_file} ${env.workspace}/project"
//sh "cd ${env.workspace}/project; git checkout ${params.branchName}"
env.imageTag = 'v18'
git branch: 'master', url: params.requirements_file
sh "pwd"
sh "ls -l"
}
}
}
stage('run test and create configmap') {
when {
anyOf {
branch "development"
branch "devops"
branch "master"
}
}
steps {
script {
withCredentials([usernamePassword(credentialsId: 'k8s_cluster_pwd_qa', passwordVariable: 'k8s_pwd', usernameVariable: 'k8s_user')]) {
sh "wget http://git.demo.com/projects/DP/repos/automation/raw/tests/unit/conftest.py -O conftest.py"
sh "wget http://git.demo.com/projects/DP/repos/automation/raw/tests/unit/config_test.py -O config_test.py"
//sh "pytest config_test.py --filename ./_kube/requirements.yaml"
sh "kubectl create configmap ${env.serviceName}-config --from-file=./_kube/requirements.yaml --namespace=sharedservices --server='https://qak8s-master.onpremtest.demo.com' --username=${k8s_user} --password=${k8s_pwd} --insecure-skip-tls-verify=true"
}
}
}
}
// Run below stage in parallel
// Reason: Setting up monitoring and cleanup can run at the sametime
stage("parallel") {
when {
anyOf {
branch "development"
branch "devops"
branch "master"
}
}
steps {
parallel (
'monitoring': {script {
withCredentials([usernamePassword(credentialsId: 'k8s_cluster_pwd_qa', passwordVariable: 'k8s_pwd', usernameVariable: 'k8s_user')]) {
sh "wget http://git.demo.com/projects/DP/repos/automation/raw/deployment/monitoring.yaml -O monitoring.yaml"
sh "sed -i 's/BASE/${env.serviceName}/g' monitoring.yaml"
sh "sed -i -e 's|image: aws.dkr.ecr.us-west-2.amazonaws.com/demo/devops/.*\$|image: aws.dkr.ecr.us-west-2.amazonaws.com/demo/devops/automation-base:${env.imageTag}|' monitoring.yaml"
sh "sed -i -e 's|python /automation/change_on_user_repo/main.py.*\$|python /automation/change_on_user_repo/main.py ONPREM-QA;|' monitoring.yaml"
sh "echo '======= monitoring.yaml =========='"
sh "cat monitoring.yaml"
sh "echo '======== monitoring.yaml after =========================='"
sh "kubectl create -f monitoring.yaml --namespace=sharedservices --server='https://qak8s-master.onpremtest.demo.com' --username=${k8s_user} --password=${k8s_pwd} --insecure-skip-tls-verify=true"
sh "sleep 300"
sh "kubectl delete -f monitoring.yaml --namespace=sharedservices --server='https://qak8s-master.onpremtest.demo.com' --username=${k8s_user} --password=${k8s_pwd} --insecure-skip-tls-verify=true"
}
}
},
'cleanup': {script {
withCredentials([usernamePassword(credentialsId: 'k8s_cluster_pwd_qa', passwordVariable: 'k8s_pwd', usernameVariable: 'k8s_user')]) {
sh "wget http://git.demo.com/projects/DP/repos/automation/raw/deployment/cleanup.yaml -O cleanup.yaml"
sh "sed -i 's/BASE/${env.serviceName}/g' cleanup.yaml"
sh "sed -i -e 's|image: aws.dkr.ecr.us-west-2.amazonaws.com/demo/devops/.*\$|image: aws.dkr.ecr.us-west-2.amazonaws.com/demo/devops/automation-base:${env.imageTag}|' cleanup.yaml"
sh "echo '======= cleanup.yaml =========='"
sh "cat cleanup.yaml"
sh "echo '======== cleanup.yaml after======================='"
sh "kubectl create -f cleanup.yaml --namespace=sharedservices --server='https://qak8s-master.onpremtest.demo.com' --username=${k8s_user} --password=${k8s_pwd} --insecure-skip-tls-verify=true"
sh "sleep 300"
sh "kubectl delete -f cleanup.yaml --namespace=sharedservices --server='https://qak8s-master.onpremtest.demo.com' --username=${k8s_user} --password=${k8s_pwd} --insecure-skip-tls-verify=true"
}
}
}
)
}
}
// If the branch name is:
// development
// devops
// master
// we are deleting the configmap
stage('cleanup: configmap') {
when {
anyOf {
branch "development"
branch "devops"
branch "master"
}
}
steps {
script {
withCredentials([usernamePassword(credentialsId: 'k8s_cluster_pwd_qa', passwordVariable: 'k8s_pwd', usernameVariable: 'k8s_user')]) {
sh "kubectl delete configmap ${env.serviceName}-config --namespace=sharedservices --server='https://qak8s-master.onpremtest.demo.com' --username=${k8s_user} --password=${k8s_pwd} --insecure-skip-tls-verify=true"
}
}
}
}
}
}
@NonCPS
def static cleanBranchName(String branchName) {
return branchName.replaceAll("/", "-")
}
@NonCPS
def getChangeString() {
MAX_MSG_LEN = 100
def changeString = ""
echo "Gathering SCM changes"
def changeLogSets = currentBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
truncated_msg = entry.msg.take(MAX_MSG_LEN)
changeString += " - ${truncated_msg} [${entry.author}]\n"
}
}
if (!changeString) {
changeString = " - No new changes"
}
return changeString
}