blob: 92511b565e94ecd8009988336f7337fa3164ee4a [file] [log] [blame]
Rohan Patel6f7e46b2019-09-25 14:35:28 -04001#!/usr/bin/env groovy
2
Rohan Patele19624e2019-11-11 16:30:46 -05003/* Copyright (c) 2019 AT&T Intellectual Property. #
4# #
5# Licensed under the Apache License, Version 2.0 (the "License"); #
6# you may not use this file except in compliance with the License. #
7# You may obtain a copy of the License at #
8# #
9# http://www.apache.org/licenses/LICENSE-2.0 #
10# #
11# Unless required by applicable law or agreed to in writing, software #
12# distributed under the License is distributed on an "AS IS" BASIS, #
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
14# See the License for the specific language governing permissions and #
15# limitations under the License. #
16##############################################################################*/
17
Rohan Patel6f7e46b2019-09-25 14:35:28 -040018properties([[$class: 'ParametersDefinitionProperty', parameterDefinitions: [
19 [$class: 'hudson.model.StringParameterDefinition', name: 'PHASE', defaultValue: "BUILD"],
20 [$class: 'hudson.model.StringParameterDefinition', name: 'ENV', defaultValue: "dev"],
21 [$class: 'hudson.model.StringParameterDefinition', name: 'MECHID', defaultValue: "username"],
22 [$class: 'hudson.model.StringParameterDefinition', name: 'KUBE_CONFIG', defaultValue: "kubeConfig-dev"],
23 [$class: 'hudson.model.StringParameterDefinition', name: 'OTF_MONGO_DB', defaultValue: "otf_mongo_dev_db"],
24 [$class: 'hudson.model.StringParameterDefinition', name: 'OTF_CAMUNDA_DB', defaultValue: "otf_camunda_dev_db"],
25 [$class: 'hudson.model.StringParameterDefinition', name: 'TILLER_NAMESPACE', defaultValue: "org-oran-otf"]
26
27]]])
28
29echo "Build branch: ${env.BRANCH_NAME}"
30
31node("docker") {
32 stage 'Checkout'
33 checkout scm
34 PHASES = PHASE.tokenize('_')
35 echo "PHASES : " + PHASES
36 pom = readMavenPom file: 'pom.xml'
37 ARTIFACT_ID = pom.artifactId
38 VERSION = pom.version
39 LABEL_VERSION = pom.version.replaceAll("\\.", "-")
40 echo "LabelVerion: " + LABEL_VERSION
41 NAMESPACE = pom.groupId
42 echo "Tiller Namespace: " + TILLER_NAMESPACE
43 DOCKER_REGISTRY = pom.properties['docker.registry']
44
45
46
47 if( ENV.equalsIgnoreCase("dev") ){
48 IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE + "/" + ARTIFACT_ID + ":" + VERSION
49
50 }
51 if( ENV.equalsIgnoreCase("prod") || ENV.equalsIgnoreCase("prod-dr")){
52 IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE + ".prod" + "/" + ARTIFACT_ID + ":" + VERSION
53
54 }
55 if( ENV.equalsIgnoreCase("st") ){
56 IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE + ".st" + "/" + ARTIFACT_ID + ":" + VERSION
57
58 }
59
60 echo "Artifact: " + IMAGE_NAME
61
62 withEnv(["PATH=${env.PATH}:${tool 'mvn352'}/bin:${tool 'jdk180'}/bin:${env.WORKSPACE}/linux-amd64", "JAVA_HOME=${tool 'jdk180'}", "MAVEN_HOME=${tool 'mvn352'}", "HELM_HOME=${env.WORKSPACE}"]) {
63
64 echo "JAVA_HOME=${env.JAVA_HOME}"
65 echo "MAVEN_HOME=${env.MAVEN_HOME}"
66 echo "PATH=${env.PATH}"
67 echo "HELM_HOME=${env.HELM_HOME}"
68
69 wrap([$class: 'ConfigFileBuildWrapper', managedFiles: [
70 [fileId: 'maven-settings.xml', variable: 'MAVEN_SETTINGS']
71 ]]) {
72
73
74 if (PHASES.contains("BUILD")) {
75 stage 'Compile'
76 sh 'mvn -s $MAVEN_SETTINGS clean compile'
77
78 //stage 'Unit Test'
79 sh 'mvn -s $MAVEN_SETTINGS test -DskipTests'
80
81 stage 'Package'
82 sh 'mvn -s $MAVEN_SETTINGS package -DskipTests'
83// sh 'mvn -DskipTests -Dmaven.test.skip=true -s $MAVEN_SETTINGS package'
84
85// stage 'Verify'
86 sh 'mvn -s $MAVEN_SETTINGS verify -DskipTests'
87
88 stage 'Publish Artifact'
89
90 withCredentials([usernamePassword(credentialsId: MECHID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
91
92 echo "Artifact: " + IMAGE_NAME
93
94 sh """
95 docker login $DOCKER_REGISTRY --username $USERNAME --password $PASSWORD
96 docker build -t $IMAGE_NAME -f target/Dockerfile target
97 docker push $IMAGE_NAME
98 """
99 }
100
101 }
102 if (PHASES.contains("DEPLOY") || PHASES.contains("UNDEPLOY")) {
103
104 stage 'Init Helm'
105
106 //check if helm exists if not install
107 if (fileExists('linux-amd64/helm')) {
108 sh """
109 echo "helm is already installed"
110 """
111 } else {
112 //download helm
113 sh """
114 echo "installing helm"
115 wget https://storage.googleapis.com/kubernetes-helm/helm-v2.8.2-linux-amd64.tar.gz
116 tar -xf helm-v2.8.2-linux-amd64.tar.gz
117 rm helm-v2.8.2-linux-amd64.tar.gz
118 """
119 }
120
121 withCredentials([file(credentialsId: KUBE_CONFIG, variable: 'KUBECONFIG')]) {
122
123 dir('helm') {
124 //check if charts are valid, and then perform dry run, if successful then upgrade/install charts
125
126 if (PHASES.contains("UNDEPLOY")) {
127 stage 'Undeploy'
128
129 sh """
130 helm delete --tiller-namespace=$TILLER_NAMESPACE --purge $ARTIFACT_ID
131 """
132 }
133
134 //NOTE Double quotes are used below to access groovy variables like artifact_id and tiller_namespace
135 if (PHASES.contains("DEPLOY")) {
136 stage 'Deploy'
137 withCredentials([
138 usernamePassword(credentialsId: OTF_MONGO_DB, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD'),
139 ]) {
140 sh """
141 echo "Validate Yaml"
142 helm lint $ARTIFACT_ID
143
144 echo "View Helm Templates"
145 helm template $ARTIFACT_ID \
146 --set appName=$ARTIFACT_ID \
147 --set version=$VERSION \
148 --set image=$IMAGE_NAME\
149 --set env=$ENV \
150 --set otf.mongo.username=$USERNAME \
151 --set otf.mongo.password=$PASSWORD \
152 --set namespace=$TILLER_NAMESPACE
153
154
155 echo "Perform Dry Run Of Install"
156 helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install --dry-run $ARTIFACT_ID $ARTIFACT_ID \
157 --set appName=$ARTIFACT_ID \
158 --set version=$VERSION \
159 --set image=$IMAGE_NAME\
160 --set env=$ENV \
161 --set otf.mongo.username=$USERNAME \
162 --set otf.mongo.password=$PASSWORD \
163 --set namespace=$TILLER_NAMESPACE
164
165 echo "Helm Install/Upgrade"
166 helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install $ARTIFACT_ID $ARTIFACT_ID \
167 --set appName=$ARTIFACT_ID \
168 --set version=$VERSION \
169 --set image=$IMAGE_NAME\
170 --set env=$ENV \
171 --set otf.mongo.username=$USERNAME \
172 --set otf.mongo.password=$PASSWORD \
173 --set namespace=$TILLER_NAMESPACE
174
175 """
176 }
177 }
178
179 }
180 }
181 }
182 }
183 }
184}