Add release file for it-otf-service-api:0.0.1
[it/otf.git] / otf-service-api / 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 echo "Build branch: ${env.BRANCH_NAME}"\r
30 \r
31 node("docker") {\r
32     stage 'Checkout'\r
33     checkout scm\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
44 \r
45 \r
46 \r
47         if( ENV.equalsIgnoreCase("dev") ){\r
48             IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE  + "/" + ARTIFACT_ID + ":" + VERSION\r
49         \r
50         }\r
51         if( ENV.equalsIgnoreCase("prod") || ENV.equalsIgnoreCase("prod-dr")){\r
52             IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE + ".prod" + "/" + ARTIFACT_ID + ":" + VERSION\r
53         \r
54         }    \r
55     if( ENV.equalsIgnoreCase("st") ){\r
56         IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE + ".st" + "/" + ARTIFACT_ID + ":" + VERSION\r
57     \r
58     } \r
59         \r
60         echo "Artifact: " + IMAGE_NAME\r
61 \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
63 \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
68 \r
69         wrap([$class: 'ConfigFileBuildWrapper', managedFiles: [\r
70                 [fileId: 'maven-settings.xml', variable: 'MAVEN_SETTINGS']\r
71         ]]) {\r
72 \r
73 \r
74             if (PHASES.contains("BUILD")) {\r
75                 stage 'Compile'\r
76                 sh 'mvn -s $MAVEN_SETTINGS clean compile'\r
77 \r
78                 //stage 'Unit Test'\r
79                 sh 'mvn -s $MAVEN_SETTINGS test -DskipTests'\r
80 \r
81                 stage 'Package'\r
82                 sh 'mvn -s $MAVEN_SETTINGS package -DskipTests'\r
83 //                sh 'mvn -DskipTests -Dmaven.test.skip=true -s $MAVEN_SETTINGS package'\r
84 \r
85 //                stage 'Verify'\r
86                 sh 'mvn -s $MAVEN_SETTINGS verify -DskipTests'\r
87 \r
88                 stage 'Publish Artifact'\r
89 \r
90                 withCredentials([usernamePassword(credentialsId: MECHID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {\r
91 \r
92                     echo "Artifact: " + IMAGE_NAME\r
93 \r
94                     sh """\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
98                                         """\r
99                 }\r
100 \r
101             }\r
102             if (PHASES.contains("DEPLOY") || PHASES.contains("UNDEPLOY")) {\r
103 \r
104                 stage 'Init Helm'\r
105 \r
106                 //check if helm exists if not install\r
107                 if (fileExists('linux-amd64/helm')) {\r
108                     sh """\r
109                                                 echo "helm is already installed"\r
110                                         """\r
111                 } else {\r
112                     //download helm\r
113                     sh """\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
118                                         """\r
119                 }\r
120 \r
121                 withCredentials([file(credentialsId: KUBE_CONFIG, variable: 'KUBECONFIG')]) {\r
122 \r
123                     dir('helm') {\r
124                         //check if charts are valid, and then perform dry run, if successful then upgrade/install charts\r
125 \r
126                         if (PHASES.contains("UNDEPLOY")) {\r
127                             stage 'Undeploy'\r
128 \r
129                             sh """\r
130                                                         helm delete --tiller-namespace=$TILLER_NAMESPACE --purge $ARTIFACT_ID\r
131                                                 """\r
132                         }\r
133 \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
136                             stage 'Deploy'\r
137                             withCredentials([\r
138                                     usernamePassword(credentialsId: OTF_MONGO_DB, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD'),\r
139                             ]) {\r
140                                 sh """                                                                          \r
141                                                                     echo "Validate Yaml"\r
142                                     helm lint $ARTIFACT_ID\r
143 \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
149                                         --set env=$ENV \\r
150                                         --set otf.mongo.username=$USERNAME \\r
151                                         --set otf.mongo.password=$PASSWORD \\r
152                                         --set namespace=$TILLER_NAMESPACE\r
153                                         \r
154 \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
160                                         --set env=$ENV \\r
161                                         --set otf.mongo.username=$USERNAME \\r
162                                         --set otf.mongo.password=$PASSWORD \\r
163                                         --set namespace=$TILLER_NAMESPACE\r
164 \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
170                                         --set env=$ENV \\r
171                                         --set otf.mongo.username=$USERNAME \\r
172                                         --set otf.mongo.password=$PASSWORD \\r
173                                         --set namespace=$TILLER_NAMESPACE\r
174                                     \r
175                                                                 """\r
176                             }\r
177                         }\r
178 \r
179                     }\r
180                 }\r
181             }\r
182         }\r
183     }\r
184 }