added documentation and updated VTHs
[it/otf.git] / dmaap-vth / Jenkinsfile
1 #!/usr/bin/env groovy\r
2 \r
3 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
4 #                                                                              #\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
8 #                                                                              #\r
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
10 #                                                                              #\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
17 \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: "id_otf_dev"],\r
22         [$class: 'hudson.model.StringParameterDefinition', name: 'KUBE_CONFIG', defaultValue: "kubeConfig-dev"],\r
23         [$class: 'hudson.model.StringParameterDefinition', name: 'TILLER_NAMESPACE', defaultValue: "org-oran-otf"]\r
24 ]]])\r
25 \r
26 \r
27 echo "Build branch: ${env.BRANCH_NAME}"\r
28 \r
29 node("docker"){\r
30         stage 'Checkout'\r
31         checkout scm\r
32         PHASES=PHASE.tokenize( '_' );\r
33         echo "PHASES : " + PHASES\r
34 \r
35 \r
36         ARTIFACT_ID="dmaap-vth";\r
37         VERSION="0.0.1-SNAPSHOT";\r
38         NAMESPACE="org-oran-otf"\r
39         DOCKER_REGISTRY="registry.hub.docker.io"\r
40 \r
41   if( ENV.equalsIgnoreCase("dev") ){\r
42     IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + ".dev" + "/" + ARTIFACT_ID +  ":" + VERSION\r
43 \r
44   }\r
45   if( ENV.equalsIgnoreCase("prod") || ENV.equalsIgnoreCase("prod-dr")){\r
46     IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + ".prod" + "/" + ARTIFACT_ID +  ":" + VERSION\r
47 \r
48   }\r
49 \r
50   if( ENV.equalsIgnoreCase("st") ){\r
51     IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + ".st" + "/" + ARTIFACT_ID +  ":" + VERSION\r
52 \r
53   }\r
54 \r
55         echo "Artifact: " + IMAGE_NAME\r
56 \r
57         withEnv(["PATH=${env.PATH}:${env.WORKSPACE}/linux-amd64", "HELM_HOME=${env.WORKSPACE}"]) {\r
58 \r
59                 echo "PATH=${env.PATH}"\r
60                 echo "HELM_HOME=${env.HELM_HOME}"\r
61 \r
62                 if (PHASES.contains("BUILD")){\r
63 \r
64                         stage 'Publish Artifact'\r
65 \r
66                                 withCredentials([usernamePassword(credentialsId: MECHID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {\r
67                                         dir("docker")\r
68                                                 echo "Artifact: " + IMAGE_NAME\r
69 \r
70                                                 sh """\r
71                                                         docker login $DOCKER_REGISTRY --username $USERNAME --password $PASSWORD\r
72                                                         docker build -t $IMAGE_NAME .\r
73                                                         docker push $IMAGE_NAME\r
74                                                 """\r
75                                 }\r
76 \r
77                 }\r
78 \r
79                 if (PHASES.contains("DEPLOY") || PHASES.contains("UNDEPLOY")) {\r
80 \r
81                         stage 'Init Helm'\r
82 \r
83                         //check if helm exists if not install\r
84                         if(fileExists('linux-amd64/helm')){\r
85                                 sh """\r
86                                         echo "helm is already installed"\r
87                                 """\r
88                         }\r
89                         else{\r
90                         //download helm\r
91                                 sh """\r
92                                         echo "installing helm"\r
93                                         wget  https://storage.googleapis.com/kubernetes-helm/helm-v2.14.3-linux-amd64.tar.gz\r
94                                         tar -xf helm-v2.14.3-linux-amd64.tar.gz\r
95                                         rm helm-v2.14.3-linux-amd64.tar.gz\r
96                                 """\r
97                         }\r
98 \r
99                         withCredentials([file(credentialsId: KUBE_CONFIG, variable: 'KUBECONFIG')]) {\r
100 \r
101                                 dir('helm'){\r
102                                 //check if charts are valid, and then perform dry run, if successful then upgrade/install charts\r
103 \r
104                                         if (PHASES.contains("UNDEPLOY") ) {\r
105                                                 stage 'Undeploy'\r
106 \r
107                                                 sh """\r
108                                                         helm delete --tiller-namespace=$TILLER_NAMESPACE --purge $ARTIFACT_ID\r
109                                                 """\r
110                                         }\r
111 \r
112                                 //NOTE Double quotes are used below to access groovy variables like artifact_id and tiller_namespace\r
113                         if (PHASES.contains("DEPLOY") ){\r
114                                 stage 'Deploy'\r
115                                                         withCredentials([usernamePassword(credentialsId: MECHID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {\r
116 \r
117                                                                 sh """\r
118                                                                     helm version\r
119                                                                         echo "Validate Yaml"\r
120                                                                         helm lint $ARTIFACT_ID\r
121 \r
122                                                                         echo "View Helm Templates"\r
123                                                                         helm template $ARTIFACT_ID --set appName=$ARTIFACT_ID \\r
124                                         --set appName=$ARTIFACT_ID \\r
125                                         --set version=$VERSION  \\r
126                                         --set env=$ENV \\r
127                                         --set image=$IMAGE_NAME \\r
128                                         --set namespace=$TILLER_NAMESPACE \\r
129                                         --set credentials.username= $USERNAME \\r
130                                         --set credentials.password= $PASSWORD\r
131 \r
132                                                                         echo "Perform Dry Run Of Install"\r
133                                                                         helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install --dry-run $ARTIFACT_ID $ARTIFACT_ID \\r
134                                         --set appName=$ARTIFACT_ID \\r
135                                         --set version=$VERSION \\r
136                                         --set env=$ENV \\r
137                                         --set image=$IMAGE_NAME \\r
138                                         --set namespace=$TILLER_NAMESPACE \\r
139                                         --set credentials.username=$USERNAME \\r
140                                         --set credentials.password=$PASSWORD\r
141 \r
142 \r
143                                                                         echo "Helm Install/Upgrade"\r
144                                                                 helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install $ARTIFACT_ID $ARTIFACT_ID \\r
145                                         --set appName=$ARTIFACT_ID \\r
146                                         --set version=$VERSION \\r
147                                         --set env=$ENV \\r
148                                         --set image=$IMAGE_NAME \\r
149                                         --set namespace=$TILLER_NAMESPACE \\r
150                                         --set credentials.username=$USERNAME \\r
151                                         --set credentials.password=$PASSWORD\r
152 \r
153                                                                 """\r
154                                                         }\r
155                         }\r
156 \r
157                                 }\r
158                         }\r
159         }\r
160 \r
161         }\r
162 }\r