1 #!/usr/bin/env groovy
\r
3 /* Copyright (c) 2019 AT&T Intellectual Property. #
\r
5 # Licensed under the Apache License, Version 2.0 (the "License"); #
\r
6 # you may not use this file except in compliance with the License. #
\r
7 # You may obtain a copy of the License at #
\r
9 # http://www.apache.org/licenses/LICENSE-2.0 #
\r
11 # Unless required by applicable law or agreed to in writing, software #
\r
12 # distributed under the License is distributed on an "AS IS" BASIS, #
\r
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
\r
14 # See the License for the specific language governing permissions and #
\r
15 # limitations under the License. #
\r
16 ##############################################################################*/
\r
18 properties([[$class: 'ParametersDefinitionProperty', parameterDefinitions: [
\r
19 [$class: 'hudson.model.StringParameterDefinition', name: 'PHASE', defaultValue: "BUILD"],
\r
20 [$class: 'hudson.model.StringParameterDefinition', name: 'ENV', defaultValue: "dev"],
\r
21 [$class: 'hudson.model.StringParameterDefinition', name: 'MECHID', defaultValue: "username"],
\r
22 [$class: 'hudson.model.StringParameterDefinition', name: 'KUBE_CONFIG', defaultValue: "kubeConfig-dev"],
\r
23 [$class: 'hudson.model.StringParameterDefinition', name: 'OTF_MONGO_DB', defaultValue: "otf_mongo_dev_db"],
\r
24 [$class: 'hudson.model.StringParameterDefinition', name: 'OTF_CAMUNDA_DB', defaultValue: "otf_camunda_dev_db"],
\r
25 [$class: 'hudson.model.StringParameterDefinition', name: 'TILLER_NAMESPACE', defaultValue: "org-oran-otf"]
\r
29 echo "Build branch: ${env.BRANCH_NAME}"
\r
34 PHASES = PHASE.tokenize('_')
\r
35 echo "PHASES : " + PHASES
\r
36 pom = readMavenPom file: 'pom.xml'
\r
37 ARTIFACT_ID = pom.artifactId
\r
38 VERSION = pom.version
\r
39 LABEL_VERSION = pom.version.replaceAll("\\.", "-")
\r
40 echo "LabelVerion: " + LABEL_VERSION
\r
41 NAMESPACE = pom.groupId
\r
42 echo "Tiller Namespace: " + TILLER_NAMESPACE
\r
43 DOCKER_REGISTRY = pom.properties['docker.registry']
\r
47 if( ENV.equalsIgnoreCase("dev") ){
\r
48 IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE + "/" + ARTIFACT_ID + ":" + VERSION
\r
51 if( ENV.equalsIgnoreCase("prod") || ENV.equalsIgnoreCase("prod-dr")){
\r
52 IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE + ".prod" + "/" + ARTIFACT_ID + ":" + VERSION
\r
55 if( ENV.equalsIgnoreCase("st") ){
\r
56 IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE + ".st" + "/" + ARTIFACT_ID + ":" + VERSION
\r
60 echo "Artifact: " + IMAGE_NAME
\r
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}"]) {
\r
64 echo "JAVA_HOME=${env.JAVA_HOME}"
\r
65 echo "MAVEN_HOME=${env.MAVEN_HOME}"
\r
66 echo "PATH=${env.PATH}"
\r
67 echo "HELM_HOME=${env.HELM_HOME}"
\r
69 wrap([$class: 'ConfigFileBuildWrapper', managedFiles: [
\r
70 [fileId: 'maven-settings.xml', variable: 'MAVEN_SETTINGS']
\r
74 if (PHASES.contains("BUILD")) {
\r
76 sh 'mvn -s $MAVEN_SETTINGS clean compile'
\r
79 sh 'mvn -s $MAVEN_SETTINGS test -DskipTests'
\r
82 sh 'mvn -s $MAVEN_SETTINGS package -DskipTests'
\r
83 // sh 'mvn -DskipTests -Dmaven.test.skip=true -s $MAVEN_SETTINGS package'
\r
86 sh 'mvn -s $MAVEN_SETTINGS verify -DskipTests'
\r
88 stage 'Publish Artifact'
\r
90 withCredentials([usernamePassword(credentialsId: MECHID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
\r
92 echo "Artifact: " + IMAGE_NAME
\r
95 docker login $DOCKER_REGISTRY --username $USERNAME --password $PASSWORD
\r
96 docker build -t $IMAGE_NAME -f target/Dockerfile target
\r
97 docker push $IMAGE_NAME
\r
102 if (PHASES.contains("DEPLOY") || PHASES.contains("UNDEPLOY")) {
\r
106 //check if helm exists if not install
\r
107 if (fileExists('linux-amd64/helm')) {
\r
109 echo "helm is already installed"
\r
114 echo "installing helm"
\r
115 wget https://storage.googleapis.com/kubernetes-helm/helm-v2.8.2-linux-amd64.tar.gz
\r
116 tar -xf helm-v2.8.2-linux-amd64.tar.gz
\r
117 rm helm-v2.8.2-linux-amd64.tar.gz
\r
121 withCredentials([file(credentialsId: KUBE_CONFIG, variable: 'KUBECONFIG')]) {
\r
124 //check if charts are valid, and then perform dry run, if successful then upgrade/install charts
\r
126 if (PHASES.contains("UNDEPLOY")) {
\r
130 helm delete --tiller-namespace=$TILLER_NAMESPACE --purge $ARTIFACT_ID
\r
134 //NOTE Double quotes are used below to access groovy variables like artifact_id and tiller_namespace
\r
135 if (PHASES.contains("DEPLOY")) {
\r
138 usernamePassword(credentialsId: OTF_MONGO_DB, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD'),
\r
141 echo "Validate Yaml"
\r
142 helm lint $ARTIFACT_ID
\r
144 echo "View Helm Templates"
\r
145 helm template $ARTIFACT_ID \
\r
146 --set appName=$ARTIFACT_ID \
\r
147 --set version=$VERSION \
\r
148 --set image=$IMAGE_NAME\
\r
150 --set otf.mongo.username=$USERNAME \
\r
151 --set otf.mongo.password=$PASSWORD \
\r
152 --set namespace=$TILLER_NAMESPACE
\r
155 echo "Perform Dry Run Of Install"
\r
156 helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install --dry-run $ARTIFACT_ID $ARTIFACT_ID \
\r
157 --set appName=$ARTIFACT_ID \
\r
158 --set version=$VERSION \
\r
159 --set image=$IMAGE_NAME\
\r
161 --set otf.mongo.username=$USERNAME \
\r
162 --set otf.mongo.password=$PASSWORD \
\r
163 --set namespace=$TILLER_NAMESPACE
\r
165 echo "Helm Install/Upgrade"
\r
166 helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install $ARTIFACT_ID $ARTIFACT_ID \
\r
167 --set appName=$ARTIFACT_ID \
\r
168 --set version=$VERSION \
\r
169 --set image=$IMAGE_NAME\
\r
171 --set otf.mongo.username=$USERNAME \
\r
172 --set otf.mongo.password=$PASSWORD \
\r
173 --set namespace=$TILLER_NAMESPACE
\r