added release notes and updated license file
[it/otf.git] / otf-camunda / 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: "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
26 \r
27 ]]])\r
28 \r
29 \r
30 echo "Build branch: ${env.BRANCH_NAME}"\r
31 \r
32 node("docker") {\r
33     stage 'Checkout'\r
34     checkout scm\r
35     PHASES = PHASE.tokenize('_');\r
36     echo "PHASES : " + PHASES\r
37     pom = readMavenPom file: 'pom.xml'\r
38     ARTIFACT_ID = pom.artifactId;\r
39     VERSION = pom.version;\r
40     LABEL_VERSION = pom.version.replaceAll("\\.", "-");\r
41     echo "LabelVerion: " + LABEL_VERSION\r
42     NAMESPACE = pom.groupId\r
43     echo "Tiller Namespace: " + TILLER_NAMESPACE\r
44     DOCKER_REGISTRY = pom.properties['docker.registry']\r
45 \r
46         if( ENV.equalsIgnoreCase("dev") ){\r
47             IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE  + "/" + ARTIFACT_ID + ":" + VERSION\r
48         }\r
49         if( ENV.equalsIgnoreCase("prod") || ENV.equalsIgnoreCase("prod-dr") ){\r
50             IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE + ".prod" + "/" + ARTIFACT_ID + ":" + VERSION\r
51         }\r
52 \r
53     if( ENV.equalsIgnoreCase("st") ){\r
54         IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE + ".st" + "/" + ARTIFACT_ID + ":" + VERSION\r
55     }\r
56     echo "Artifact: " + IMAGE_NAME\r
57 \r
58 \r
59         if( ENV.equalsIgnoreCase("dev") ){\r
60                 ROUTER_CONFIG="mysqlRouterConfig-dev.ini"\r
61         }\r
62     if( ENV.equalsIgnoreCase("st") ){\r
63         ROUTER_CONFIG="mysqlRouterConfig-st.ini"\r
64     }\r
65         if( ENV.equalsIgnoreCase("prod") || ENV.equalsIgnoreCase("prod-dr")){\r
66                 ROUTER_CONFIG="mysqlRouterConfig-prod.ini"\r
67         }\r
68 \r
69     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
70 \r
71         echo "JAVA_HOME=${env.JAVA_HOME}"\r
72         echo "MAVEN_HOME=${env.MAVEN_HOME}"\r
73         echo "PATH=${env.PATH}"\r
74         echo "HELM_HOME=${env.HELM_HOME}"\r
75 \r
76         wrap([$class: 'ConfigFileBuildWrapper', managedFiles: [\r
77                 [fileId: 'maven-settings.xml', variable: 'MAVEN_SETTINGS'],\r
78                 [fileId: 'maven-settings-security.xml', variable: 'MAVEN_SETTINGS_SECURITY']\r
79         ]]) {\r
80 \r
81 \r
82             if (PHASES.contains("BUILD")) {\r
83                 stage 'Compile'\r
84                 sh 'mvn -s $MAVEN_SETTINGS -Dsettings.security=$MAVEN_SETTINGS_SECURITY clean compile'\r
85 \r
86                 stage 'Unit Test'\r
87                 sh 'mvn -s $MAVEN_SETTINGS -Dsettings.security=$MAVEN_SETTINGS_SECURITY test'\r
88 \r
89                 stage 'Package'\r
90                 sh 'mvn -s $MAVEN_SETTINGS -Dsettings.security=$MAVEN_SETTINGS_SECURITY package'\r
91                 //sh 'mvn -DskipTests -Dmaven.test.skip=true -s $MAVEN_SETTINGS package'\r
92 \r
93                 stage 'Verify'\r
94                 sh 'mvn -s $MAVEN_SETTINGS -Dsettings.security=$MAVEN_SETTINGS_SECURITY verify'\r
95 \r
96                 stage 'Publish Artifact'\r
97 \r
98                 withCredentials([usernamePassword(credentialsId: MECHID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {\r
99 \r
100                     echo "Artifact: " + DOCKER_REGISTRY\r
101 \r
102                     sh """\r
103                                                 docker login $DOCKER_REGISTRY --username $USERNAME --password $PASSWORD\r
104                                                 docker build -t $IMAGE_NAME -f target/Dockerfile target\r
105                                                 docker push $IMAGE_NAME\r
106                                         """\r
107                 }\r
108 \r
109             }\r
110             if (PHASES.contains("DEPLOY") || PHASES.contains("UNDEPLOY") || PHASES.contains("UNDEPLOYFORCE")) {\r
111 \r
112                 stage 'Init Helm'\r
113 \r
114                 //check if helm exists if not install\r
115                 if (fileExists('linux-amd64/helm')) {\r
116                     sh """\r
117                                                 echo "helm is already installed"\r
118                                         """\r
119                 } else {\r
120                     //download helm\r
121                     sh """\r
122                                                 echo "installing helm"\r
123                                                 wget  https://storage.googleapis.com/kubernetes-helm/helm-v2.8.2-linux-amd64.tar.gz\r
124                                                 tar -xf helm-v2.8.2-linux-amd64.tar.gz\r
125                                                 rm helm-v2.8.2-linux-amd64.tar.gz\r
126                                         """\r
127                 }\r
128 \r
129                 withCredentials([file(credentialsId: KUBE_CONFIG, variable: 'KUBECONFIG')]) {\r
130 \r
131                     dir('helm') {\r
132                         //check if charts are valid, and then perform dry run, if successful then upgrade/install charts\r
133 \r
134                         if (PHASES.contains("UNDEPLOY")) {\r
135                             stage 'Undeploy'\r
136 \r
137                             sh """\r
138                                                         helm delete --tiller-namespace=$TILLER_NAMESPACE --purge $ARTIFACT_ID\r
139                                                 """\r
140                         }\r
141                         if (PHASES.contains("UNDEPLOYFORCE")) {\r
142                             stage 'Undeploy Force'\r
143 \r
144                             sh """\r
145                                 \r
146                                                         helm delete --tiller-namespace=$TILLER_NAMESPACE --purge $ARTIFACT_ID &\r
147                                 chmod 755 forceDelete.sh\r
148                                 ./forceDelete.sh $ARTIFACT_ID\r
149                                                 """\r
150 \r
151                         }\r
152 \r
153                         //NOTE Double quotes are used below to access groovy variables like artifact_id and tiller_namespace\r
154                         if (PHASES.contains("DEPLOY")) {\r
155                             stage 'Deploy'\r
156                             withCredentials([\r
157                                         usernamePassword(credentialsId: OTF_MONGO_DB, usernameVariable: 'USERNAME_MONGO', passwordVariable: 'PASSWORD_MONGO'),\r
158                                         usernamePassword(credentialsId: OTF_CAMUNDA_DB, usernameVariable: 'USERNAME_CAMUNDA', passwordVariable: 'PASSWORD_CAMUNDA')\r
159                                 ]) {\r
160 \r
161                                 sh """                                                                          \r
162                                                                                 \r
163                                                                                 echo "Validate Yaml"\r
164                                                                                 helm lint $ARTIFACT_ID\r
165 \r
166                                                                                 echo "View Helm Templates"\r
167                                                                                 helm template $ARTIFACT_ID \\r
168                                                                                         --set appName=$ARTIFACT_ID \\r
169                                                                                         --set version=$VERSION  \\r
170                                                                                         --set image=$IMAGE_NAME \\r
171                                                                                         --set namespace=$TILLER_NAMESPACE \\r
172                                                                                         --set env=$ENV \\r
173                                                                                         --set otf.mongo.username=$USERNAME_MONGO \\r
174                                                                                         --set otf.mongo.password=$PASSWORD_MONGO \\r
175                                                                                         --set otf.camunda.db.username=$USERNAME_CAMUNDA \\r
176                                                                                         --set otf.camunda.db.password=$PASSWORD_CAMUNDA \\r
177                                                                                         \r
178 \r
179                                                                                 echo "Perform Dry Run Of Install"\r
180                                                                                 helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install --dry-run $ARTIFACT_ID $ARTIFACT_ID \\r
181                                                                                         --set version=$VERSION  \\r
182                                                                                         --set image=$IMAGE_NAME \\r
183                                                                                         --set namespace=$TILLER_NAMESPACE \\r
184                                                                                         --set env=$ENV \\r
185                                                                                         --set otf.mongo.username=$USERNAME_MONGO \\r
186                                                                                         --set otf.mongo.password=$PASSWORD_MONGO \\r
187                                                                                         --set otf.camunda.db.username=$USERNAME_CAMUNDA \\r
188                                                                                         --set otf.camunda.db.password=$PASSWORD_CAMUNDA \\r
189         \r
190                                                                                         \r
191                                                                                 echo "Helm Install/Upgrade"\r
192                                                                         helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install $ARTIFACT_ID $ARTIFACT_ID \\r
193                                                                                         --set version=$VERSION  \\r
194                                                                                         --set image=$IMAGE_NAME \\r
195                                                                                         --set namespace=$TILLER_NAMESPACE \\r
196                                                                                         --set env=$ENV \\r
197                                                                                         --set otf.mongo.username=$USERNAME_MONGO \\r
198                                                                                         --set otf.mongo.password=$PASSWORD_MONGO \\r
199                                                                                         --set otf.camunda.db.username=$USERNAME_CAMUNDA \\r
200                                                                                         --set otf.camunda.db.password=$PASSWORD_CAMUNDA \\r
201 \r
202                                                                         """\r
203                             }\r
204                         }\r
205 \r
206                     }\r
207                 }\r
208             }\r
209         }\r
210     }\r
211 }