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