Rohan Patel | 6f7e46b | 2019-09-25 14:35:28 -0400 | [diff] [blame] | 1 | #!/usr/bin/env groovy
|
| 2 |
|
Rohan Patel | e19624e | 2019-11-11 16:30:46 -0500 | [diff] [blame] | 3 | /* Copyright (c) 2019 AT&T Intellectual Property. #
|
| 4 | # #
|
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); #
|
| 6 | # you may not use this file except in compliance with the License. #
|
| 7 | # You may obtain a copy of the License at #
|
| 8 | # #
|
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 #
|
| 10 | # #
|
| 11 | # Unless required by applicable law or agreed to in writing, software #
|
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, #
|
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
|
| 14 | # See the License for the specific language governing permissions and #
|
| 15 | # limitations under the License. #
|
| 16 | ##############################################################################*/
|
| 17 |
|
Rohan Patel | 6f7e46b | 2019-09-25 14:35:28 -0400 | [diff] [blame] | 18 | properties([[$class: 'ParametersDefinitionProperty', parameterDefinitions: [
|
| 19 | [$class: 'hudson.model.StringParameterDefinition', name: 'PHASE', defaultValue: "BUILD"],
|
| 20 | [$class: 'hudson.model.StringParameterDefinition', name: 'ENV', defaultValue: "dev"],
|
| 21 | [$class: 'hudson.model.StringParameterDefinition', name: 'MECHID', defaultValue: "username"],
|
| 22 | [$class: 'hudson.model.StringParameterDefinition', name: 'KUBE_CONFIG', defaultValue: "kubeConfig-dev"],
|
| 23 | [$class: 'hudson.model.StringParameterDefinition', name: 'OTF_MONGO_DB', defaultValue: "otf_mongo_dev_db"],
|
| 24 | [$class: 'hudson.model.StringParameterDefinition', name: 'OTF_CAMUNDA_DB', defaultValue: "otf_camunda_dev_db"],
|
| 25 | [$class: 'hudson.model.StringParameterDefinition', name: 'TILLER_NAMESPACE', defaultValue: ""]
|
| 26 | ]]])
|
| 27 |
|
| 28 | echo "Build branch: ${env.BRANCH_NAME}"
|
| 29 |
|
| 30 | node("docker"){
|
| 31 | stage 'Checkout'
|
| 32 | checkout scm
|
| 33 | PHASES=PHASE.tokenize( '_' );
|
| 34 | echo "PHASES : " + PHASES
|
| 35 |
|
| 36 |
|
| 37 | ARTIFACT_ID="otf-frontend";
|
| 38 | VERSION="Camille.1.0.3";
|
| 39 | //TODO: deal with namespace and docker registry
|
| 40 | NAMESPACE=""
|
| 41 | DOCKER_REGISTRY=""
|
| 42 |
|
| 43 | if( ENV.equalsIgnoreCase("dev") ){
|
| 44 | IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + "/" + ARTIFACT_ID + ":" + VERSION
|
| 45 | }
|
| 46 | if( ENV.equalsIgnoreCase("prod") || ENV.equalsIgnoreCase("prod-dr")){
|
| 47 | IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + ".prod" + "/" + ARTIFACT_ID + ":" + VERSION
|
| 48 | }
|
| 49 | if( ENV.equalsIgnoreCase("st") ){
|
| 50 | IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + ".st" + "/" + ARTIFACT_ID + ":" + VERSION
|
| 51 | }
|
| 52 | echo "Artifact: " + IMAGE_NAME
|
| 53 |
|
| 54 | withEnv(["PATH=${env.PATH}:${env.WORKSPACE}/linux-amd64", "HELM_HOME=${env.WORKSPACE}"]) {
|
| 55 |
|
| 56 | echo "PATH=${env.PATH}"
|
| 57 | echo "HELM_HOME=${env.HELM_HOME}"
|
| 58 |
|
| 59 | withCredentials([usernamePassword(credentialsId: MECHID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
|
| 60 |
|
| 61 | // sh """
|
| 62 | // docker login $DOCKER_REGISTRY --username $USERNAME --password $PASSWORD
|
| 63 | // """
|
| 64 |
|
| 65 | if (PHASES.contains("BUILD")){
|
| 66 | stage 'Publish Artifact'
|
| 67 |
|
| 68 |
|
| 69 | echo "Artifact: " + IMAGE_NAME
|
| 70 |
|
| 71 | sh """
|
| 72 | docker login $DOCKER_REGISTRY --username $USERNAME --password $PASSWORD
|
| 73 | docker build --no-cache -t $IMAGE_NAME .
|
| 74 | docker push $IMAGE_NAME
|
| 75 | """
|
| 76 |
|
| 77 |
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | if (PHASES.contains("DEPLOY") || PHASES.contains("UNDEPLOY")) {
|
| 82 |
|
| 83 | stage 'Init Helm'
|
| 84 |
|
| 85 | //check if helm exists if not install
|
| 86 | if(fileExists('linux-amd64/helm')){
|
| 87 | sh """
|
| 88 | echo "helm is already installed"
|
| 89 | """
|
| 90 | }
|
| 91 | else{
|
| 92 | //download helm
|
| 93 | sh """
|
| 94 | echo "installing helm"
|
| 95 | wget https://storage.googleapis.com/kubernetes-helm/helm-v2.8.2-linux-amd64.tar.gz
|
| 96 | tar -xf helm-v2.8.2-linux-amd64.tar.gz
|
| 97 | rm helm-v2.8.2-linux-amd64.tar.gz
|
| 98 | """
|
| 99 | }
|
| 100 |
|
| 101 | withCredentials([file(credentialsId: KUBE_CONFIG, variable: 'KUBECONFIG')]) {
|
| 102 |
|
| 103 | dir('helm'){
|
| 104 | //check if charts are valid, and then perform dry run, if successful then upgrade/install charts
|
| 105 |
|
| 106 | if (PHASES.contains("UNDEPLOY") ) {
|
| 107 | stage 'Undeploy'
|
| 108 |
|
| 109 | sh """
|
| 110 | helm delete --tiller-namespace=$TILLER_NAMESPACE --purge $ARTIFACT_ID
|
| 111 | """
|
| 112 | }
|
| 113 |
|
| 114 | //NOTE Double quotes are used below to access groovy variables like artifact_id and tiller_namespace
|
| 115 | if (PHASES.contains("DEPLOY") ){
|
| 116 | stage 'Deploy'
|
| 117 | withCredentials([
|
| 118 | usernamePassword(credentialsId: OTF_MONGO_DB, usernameVariable: 'USERNAME_MONGO', passwordVariable: 'PASSWORD_MONGO'),
|
| 119 | usernamePassword(credentialsId: 'FEATHERS_AUTH', usernameVariable: 'USER', passwordVariable: 'AUTHENTICATION_SECRET')
|
| 120 | ]) {
|
| 121 |
|
| 122 | sh """
|
| 123 | echo "Validate Yaml"
|
| 124 | helm lint $ARTIFACT_ID
|
| 125 |
|
| 126 | echo "View Helm Templates"
|
| 127 | helm template $ARTIFACT_ID \
|
| 128 | --set appName=$ARTIFACT_ID \
|
| 129 | --set version=$VERSION \
|
| 130 | --set image=$IMAGE_NAME \
|
| 131 | --set namespace=$TILLER_NAMESPACE \
|
| 132 | --set env=$ENV \
|
| 133 | --set AUTHENTICATION_SECRET=$AUTHENTICATION_SECRET \
|
| 134 | --set mongo.username=$USERNAME_MONGO \
|
| 135 | --set mongo.password=$PASSWORD_MONGO
|
| 136 |
|
| 137 | echo "Perform Dry Run Of Install"
|
| 138 | helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install --dry-run $ARTIFACT_ID $ARTIFACT_ID \
|
| 139 | --set appName=$ARTIFACT_ID \
|
| 140 | --set version=$VERSION \
|
| 141 | --set image=$IMAGE_NAME \
|
| 142 | --set namespace=$TILLER_NAMESPACE \
|
| 143 | --set env=$ENV \
|
| 144 | --set AUTHENTICATION_SECRET=$AUTHENTICATION_SECRET \
|
| 145 | --set mongo.username=$USERNAME_MONGO \
|
| 146 | --set mongo.password=$PASSWORD_MONGO
|
| 147 |
|
| 148 | echo "Helm Install/Upgrade"
|
| 149 | helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install --timeout 1000 $ARTIFACT_ID $ARTIFACT_ID \
|
| 150 | --set appName=$ARTIFACT_ID \
|
| 151 | --set version=$VERSION \
|
| 152 | --set image=$IMAGE_NAME \
|
| 153 | --set namespace=$TILLER_NAMESPACE \
|
| 154 | --set env=$ENV \
|
| 155 | --set AUTHENTICATION_SECRET=$AUTHENTICATION_SECRET \
|
| 156 | --set mongo.username=$USERNAME_MONGO \
|
| 157 | --set mongo.password=$PASSWORD_MONGO
|
| 158 |
|
| 159 | """
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | }
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | }
|
| 168 | }
|