blob: 68e8d6603852f443d54c18127afed4dcf88ef531 [file] [log] [blame]
Rohan Patel5f5c7ec2019-09-25 14:35:28 -04001#!/usr/bin/env groovy
2
3properties([[$class: 'ParametersDefinitionProperty', parameterDefinitions: [
4 [$class: 'hudson.model.StringParameterDefinition', name: 'PHASE', defaultValue: "BUILD"],
5 [$class: 'hudson.model.StringParameterDefinition', name: 'ENV', defaultValue: "dev"],
6 [$class: 'hudson.model.StringParameterDefinition', name: 'MECHID', defaultValue: "username"],
7 [$class: 'hudson.model.StringParameterDefinition', name: 'KUBE_CONFIG', defaultValue: "kubeConfig-dev"],
8 [$class: 'hudson.model.StringParameterDefinition', name: 'OTF_MONGO_DB', defaultValue: "otf_mongo_dev_db"],
9 [$class: 'hudson.model.StringParameterDefinition', name: 'OTF_CAMUNDA_DB', defaultValue: "otf_camunda_dev_db"],
10 [$class: 'hudson.model.StringParameterDefinition', name: 'TILLER_NAMESPACE', defaultValue: "org-oran-otf"]
11
12]]])
13
14echo "Build branch: ${env.BRANCH_NAME}"
15
16node("docker") {
17 stage 'Checkout'
18 checkout scm
19 PHASES = PHASE.tokenize('_')
20 echo "PHASES : " + PHASES
21 pom = readMavenPom file: 'pom.xml'
22 ARTIFACT_ID = pom.artifactId
23 VERSION = pom.version
24 LABEL_VERSION = pom.version.replaceAll("\\.", "-")
25 echo "LabelVerion: " + LABEL_VERSION
26 NAMESPACE = pom.groupId
27 echo "Tiller Namespace: " + TILLER_NAMESPACE
28 DOCKER_REGISTRY = pom.properties['docker.registry']
29
30
31
32 if( ENV.equalsIgnoreCase("dev") ){
33 IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE + "/" + ARTIFACT_ID + ":" + VERSION
34
35 }
36 if( ENV.equalsIgnoreCase("prod") || ENV.equalsIgnoreCase("prod-dr")){
37 IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE + ".prod" + "/" + ARTIFACT_ID + ":" + VERSION
38
39 }
40 if( ENV.equalsIgnoreCase("st") ){
41 IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE + ".st" + "/" + ARTIFACT_ID + ":" + VERSION
42
43 }
44
45 echo "Artifact: " + IMAGE_NAME
46
47 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}"]) {
48
49 echo "JAVA_HOME=${env.JAVA_HOME}"
50 echo "MAVEN_HOME=${env.MAVEN_HOME}"
51 echo "PATH=${env.PATH}"
52 echo "HELM_HOME=${env.HELM_HOME}"
53
54 wrap([$class: 'ConfigFileBuildWrapper', managedFiles: [
55 [fileId: 'maven-settings.xml', variable: 'MAVEN_SETTINGS']
56 ]]) {
57
58
59 if (PHASES.contains("BUILD")) {
60 stage 'Compile'
61 sh 'mvn -s $MAVEN_SETTINGS clean compile'
62
63 //stage 'Unit Test'
64 sh 'mvn -s $MAVEN_SETTINGS test -DskipTests'
65
66 stage 'Package'
67 sh 'mvn -s $MAVEN_SETTINGS package -DskipTests'
68// sh 'mvn -DskipTests -Dmaven.test.skip=true -s $MAVEN_SETTINGS package'
69
70// stage 'Verify'
71 sh 'mvn -s $MAVEN_SETTINGS verify -DskipTests'
72
73 stage 'Publish Artifact'
74
75 withCredentials([usernamePassword(credentialsId: MECHID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
76
77 echo "Artifact: " + IMAGE_NAME
78
79 sh """
80 docker login $DOCKER_REGISTRY --username $USERNAME --password $PASSWORD
81 docker build -t $IMAGE_NAME -f target/Dockerfile target
82 docker push $IMAGE_NAME
83 """
84 }
85
86 }
87 if (PHASES.contains("DEPLOY") || PHASES.contains("UNDEPLOY")) {
88
89 stage 'Init Helm'
90
91 //check if helm exists if not install
92 if (fileExists('linux-amd64/helm')) {
93 sh """
94 echo "helm is already installed"
95 """
96 } else {
97 //download helm
98 sh """
99 echo "installing helm"
100 wget https://storage.googleapis.com/kubernetes-helm/helm-v2.8.2-linux-amd64.tar.gz
101 tar -xf helm-v2.8.2-linux-amd64.tar.gz
102 rm helm-v2.8.2-linux-amd64.tar.gz
103 """
104 }
105
106 withCredentials([file(credentialsId: KUBE_CONFIG, variable: 'KUBECONFIG')]) {
107
108 dir('helm') {
109 //check if charts are valid, and then perform dry run, if successful then upgrade/install charts
110
111 if (PHASES.contains("UNDEPLOY")) {
112 stage 'Undeploy'
113
114 sh """
115 helm delete --tiller-namespace=$TILLER_NAMESPACE --purge $ARTIFACT_ID
116 """
117 }
118
119 //NOTE Double quotes are used below to access groovy variables like artifact_id and tiller_namespace
120 if (PHASES.contains("DEPLOY")) {
121 stage 'Deploy'
122 withCredentials([
123 usernamePassword(credentialsId: OTF_MONGO_DB, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD'),
124 ]) {
125 sh """
126 echo "Validate Yaml"
127 helm lint $ARTIFACT_ID
128
129 echo "View Helm Templates"
130 helm template $ARTIFACT_ID \
131 --set appName=$ARTIFACT_ID \
132 --set version=$VERSION \
133 --set image=$IMAGE_NAME\
134 --set env=$ENV \
135 --set otf.mongo.username=$USERNAME \
136 --set otf.mongo.password=$PASSWORD \
137 --set namespace=$TILLER_NAMESPACE
138
139
140 echo "Perform Dry Run Of Install"
141 helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install --dry-run $ARTIFACT_ID $ARTIFACT_ID \
142 --set appName=$ARTIFACT_ID \
143 --set version=$VERSION \
144 --set image=$IMAGE_NAME\
145 --set env=$ENV \
146 --set otf.mongo.username=$USERNAME \
147 --set otf.mongo.password=$PASSWORD \
148 --set namespace=$TILLER_NAMESPACE
149
150 echo "Helm Install/Upgrade"
151 helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install $ARTIFACT_ID $ARTIFACT_ID \
152 --set appName=$ARTIFACT_ID \
153 --set version=$VERSION \
154 --set image=$IMAGE_NAME\
155 --set env=$ENV \
156 --set otf.mongo.username=$USERNAME \
157 --set otf.mongo.password=$PASSWORD \
158 --set namespace=$TILLER_NAMESPACE
159
160 """
161 }
162 }
163
164 }
165 }
166 }
167 }
168 }
169}