remvoed jks file
[it/otf.git] / otf-frontend / 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: ""]\r
26 ]]])\r
27 \r
28 echo "Build branch: ${env.BRANCH_NAME}"\r
29 \r
30 node("docker"){\r
31         stage 'Checkout'\r
32         checkout scm\r
33         PHASES=PHASE.tokenize( '_' );\r
34         echo "PHASES : " + PHASES\r
35 \r
36 \r
37         ARTIFACT_ID="otf-frontend";\r
38         VERSION="Camille.1.0.3";\r
39         //TODO: deal with namespace and docker registry\r
40         NAMESPACE=""\r
41         DOCKER_REGISTRY=""\r
42 \r
43         if( ENV.equalsIgnoreCase("dev") ){\r
44                 IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + "/" + ARTIFACT_ID +  ":" + VERSION\r
45         }\r
46         if( ENV.equalsIgnoreCase("prod") || ENV.equalsIgnoreCase("prod-dr")){\r
47                 IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + ".prod" + "/" + ARTIFACT_ID +  ":" + VERSION\r
48         }\r
49         if( ENV.equalsIgnoreCase("st") ){\r
50                 IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + ".st" + "/" + ARTIFACT_ID +  ":" + VERSION\r
51         }\r
52         echo "Artifact: " + IMAGE_NAME\r
53 \r
54         withEnv(["PATH=${env.PATH}:${env.WORKSPACE}/linux-amd64", "HELM_HOME=${env.WORKSPACE}"]) {\r
55 \r
56                 echo "PATH=${env.PATH}"\r
57                 echo "HELM_HOME=${env.HELM_HOME}"\r
58 \r
59                 withCredentials([usernamePassword(credentialsId: MECHID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {\r
60 \r
61                         // sh """\r
62                         //      docker login $DOCKER_REGISTRY --username $USERNAME --password $PASSWORD\r
63                         // """\r
64 \r
65                         if (PHASES.contains("BUILD")){\r
66                                 stage 'Publish Artifact'\r
67 \r
68 \r
69                                         echo "Artifact: " + IMAGE_NAME\r
70 \r
71                                         sh """\r
72                                                 docker login $DOCKER_REGISTRY --username $USERNAME --password $PASSWORD\r
73                                                 docker build --no-cache -t $IMAGE_NAME .\r
74                                                 docker push $IMAGE_NAME\r
75                                         """\r
76 \r
77 \r
78                         }\r
79                 }\r
80 \r
81                 if (PHASES.contains("DEPLOY") || PHASES.contains("UNDEPLOY")) {\r
82 \r
83                         stage 'Init Helm'\r
84 \r
85                         //check if helm exists if not install\r
86                         if(fileExists('linux-amd64/helm')){\r
87                                 sh """\r
88                                         echo "helm is already installed"\r
89                                 """\r
90                         }\r
91                         else{\r
92                         //download helm\r
93                                 sh """\r
94                                         echo "installing helm"\r
95                                         wget  https://storage.googleapis.com/kubernetes-helm/helm-v2.8.2-linux-amd64.tar.gz\r
96                                         tar -xf helm-v2.8.2-linux-amd64.tar.gz\r
97                                         rm helm-v2.8.2-linux-amd64.tar.gz\r
98                                 """\r
99                         }\r
100 \r
101                         withCredentials([file(credentialsId: KUBE_CONFIG,  variable: 'KUBECONFIG')]) {\r
102 \r
103                                 dir('helm'){\r
104                                 //check if charts are valid, and then perform dry run, if successful then upgrade/install charts\r
105 \r
106                                         if (PHASES.contains("UNDEPLOY") ) {\r
107                                                 stage 'Undeploy'\r
108 \r
109                                                 sh """\r
110                                                         helm delete --tiller-namespace=$TILLER_NAMESPACE --purge $ARTIFACT_ID\r
111                                                 """\r
112                                         }\r
113 \r
114                                 //NOTE Double quotes are used below to access groovy variables like artifact_id and tiller_namespace\r
115                         if (PHASES.contains("DEPLOY") ){\r
116                                 stage 'Deploy'\r
117                                                         withCredentials([\r
118                                                                 usernamePassword(credentialsId: OTF_MONGO_DB, usernameVariable: 'USERNAME_MONGO', passwordVariable: 'PASSWORD_MONGO'),\r
119                                                                 usernamePassword(credentialsId: 'FEATHERS_AUTH', usernameVariable: 'USER', passwordVariable: 'AUTHENTICATION_SECRET')\r
120                                                                 ]) {\r
121 \r
122                                                                 sh """\r
123                                                                         echo "Validate Yaml"\r
124                                                                         helm lint $ARTIFACT_ID\r
125 \r
126                                                                         echo "View Helm Templates"\r
127                                                                         helm template $ARTIFACT_ID \\r
128                                                                                 --set appName=$ARTIFACT_ID \\r
129                                                                                 --set version=$VERSION \\r
130                                                                                 --set image=$IMAGE_NAME \\r
131                                                                                 --set namespace=$TILLER_NAMESPACE \\r
132                                                                           --set env=$ENV \\r
133                                                                                 --set AUTHENTICATION_SECRET=$AUTHENTICATION_SECRET \\r
134                                                                                 --set mongo.username=$USERNAME_MONGO \\r
135                                                                                 --set mongo.password=$PASSWORD_MONGO\r
136 \r
137                                                                         echo "Perform Dry Run Of Install"\r
138                                                                         helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install --dry-run $ARTIFACT_ID $ARTIFACT_ID \\r
139                                                                                 --set appName=$ARTIFACT_ID \\r
140                                                                                 --set version=$VERSION \\r
141                                                                                 --set image=$IMAGE_NAME \\r
142                                                                                 --set namespace=$TILLER_NAMESPACE \\r
143                                                                           --set env=$ENV \\r
144                                                                                 --set AUTHENTICATION_SECRET=$AUTHENTICATION_SECRET \\r
145                                                                                 --set mongo.username=$USERNAME_MONGO \\r
146                                                                                 --set mongo.password=$PASSWORD_MONGO\r
147 \r
148                                                                         echo "Helm Install/Upgrade"\r
149                                                                 helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install --timeout 1000 $ARTIFACT_ID $ARTIFACT_ID \\r
150                                                                                 --set appName=$ARTIFACT_ID \\r
151                                                                                 --set version=$VERSION \\r
152                                                                                 --set image=$IMAGE_NAME \\r
153                                                                                 --set namespace=$TILLER_NAMESPACE \\r
154                                                                                 --set env=$ENV \\r
155                                                                                 --set AUTHENTICATION_SECRET=$AUTHENTICATION_SECRET \\r
156                                                                                 --set mongo.username=$USERNAME_MONGO \\r
157                                                                                 --set mongo.password=$PASSWORD_MONGO\r
158 \r
159                                                                 """\r
160                                                         }\r
161                         }\r
162 \r
163                                 }\r
164                         }\r
165         }\r
166 \r
167         }\r
168 }\r