Chen, Jackie | 63b197b | 2020-06-15 14:28:29 -0400 | [diff] [blame^] | 1 | #!/usr/bin/env groovy
|
| 2 |
|
| 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 |
|
| 18 |
|
| 19 | properties([[$class: 'ParametersDefinitionProperty', parameterDefinitions: [
|
| 20 | [$class: 'hudson.model.StringParameterDefinition', name: 'PHASE', defaultValue: "BUILD"],
|
| 21 | [$class: 'hudson.model.StringParameterDefinition', name: 'ENV', defaultValue: "dev"],
|
| 22 | [$class: 'hudson.model.StringParameterDefinition', name: 'MECHID', defaultValue: "m13591_otf_dev"],
|
| 23 | [$class: 'hudson.model.StringParameterDefinition', name: 'KUBE_CONFIG', defaultValue: "kubeConfig-dev"],
|
| 24 | [$class: 'hudson.model.StringParameterDefinition', name: 'TILLER_NAMESPACE', defaultValue: "com-att-ecomp-otf-dev"]
|
| 25 | ]]])
|
| 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="a1-sdnc-vth";
|
| 38 | VERSION="0.0.1-SNAPSHOT";
|
| 39 | NAMESPACE="com.att.ecomp.otf" //TODO change back to org-otf-oran when done testing
|
| 40 | DOCKER_REGISTRY="dockercentral.it.att.com:5100"
|
| 41 |
|
| 42 | if( ENV.equalsIgnoreCase("dev") ){
|
| 43 | IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + ".dev" + "/" + ARTIFACT_ID + ":" + VERSION
|
| 44 |
|
| 45 | }
|
| 46 | if( ENV.equalsIgnoreCase("prod") || ENV.equalsIgnoreCase("prod-dr")){
|
| 47 | IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + ".prod" + "/" + ARTIFACT_ID + ":" + VERSION
|
| 48 |
|
| 49 | }
|
| 50 |
|
| 51 | if( ENV.equalsIgnoreCase("st") ){
|
| 52 | IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + ".st" + "/" + ARTIFACT_ID + ":" + VERSION
|
| 53 |
|
| 54 | }
|
| 55 |
|
| 56 | echo "Artifact: " + IMAGE_NAME
|
| 57 |
|
| 58 | withEnv(["PATH=${env.PATH}:${env.WORKSPACE}/linux-amd64", "HELM_HOME=${env.WORKSPACE}"]) {
|
| 59 |
|
| 60 | echo "PATH=${env.PATH}"
|
| 61 | echo "HELM_HOME=${env.HELM_HOME}"
|
| 62 |
|
| 63 | if (PHASES.contains("BUILD")){
|
| 64 | dir("./a1-sdnc-vth"){
|
| 65 | stage 'Publish Artifact'
|
| 66 |
|
| 67 | withCredentials([usernamePassword(credentialsId: MECHID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
|
| 68 |
|
| 69 | echo "Artifact: " + IMAGE_NAME
|
| 70 |
|
| 71 | sh """
|
| 72 | docker login $DOCKER_REGISTRY --username $USERNAME --password $PASSWORD
|
| 73 | docker build -t $IMAGE_NAME .
|
| 74 | docker push $IMAGE_NAME
|
| 75 | """
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 |
|
| 80 | }
|
| 81 |
|
| 82 | if (PHASES.contains("DEPLOY") || PHASES.contains("UNDEPLOY")) {
|
| 83 |
|
| 84 | stage 'Init Helm'
|
| 85 |
|
| 86 | //check if helm exists if not install
|
| 87 | if(fileExists('linux-amd64/helm')){
|
| 88 | sh """
|
| 89 | echo "helm is already installed"
|
| 90 | """
|
| 91 | }
|
| 92 | else{
|
| 93 | //download helm
|
| 94 | sh """
|
| 95 | echo "installing helm"
|
| 96 | wget https://storage.googleapis.com/kubernetes-helm/helm-v2.14.3-linux-amd64.tar.gz
|
| 97 | tar -xf helm-v2.14.3-linux-amd64.tar.gz
|
| 98 | rm helm-v2.14.3-linux-amd64.tar.gz
|
| 99 | """
|
| 100 | }
|
| 101 |
|
| 102 | withCredentials([file(credentialsId: KUBE_CONFIG, variable: 'KUBECONFIG')]) {
|
| 103 |
|
| 104 | dir('a1-sdnc-vth/helm'){
|
| 105 | //check if charts are valid, and then perform dry run, if successful then upgrade/install charts
|
| 106 |
|
| 107 | if (PHASES.contains("UNDEPLOY") ) {
|
| 108 | stage 'Undeploy'
|
| 109 |
|
| 110 | sh """
|
| 111 | helm delete --tiller-namespace=$TILLER_NAMESPACE --purge $ARTIFACT_ID
|
| 112 | """
|
| 113 | }
|
| 114 |
|
| 115 | //NOTE Double quotes are used below to access groovy variables like artifact_id and tiller_namespace
|
| 116 | if (PHASES.contains("DEPLOY") ){
|
| 117 | stage 'Deploy'
|
| 118 | withCredentials([usernamePassword(credentialsId: MECHID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
|
| 119 |
|
| 120 | sh """
|
| 121 | echo "Validate Yaml"
|
| 122 | helm lint $ARTIFACT_ID
|
| 123 |
|
| 124 | echo "View Helm Templates"
|
| 125 | helm template $ARTIFACT_ID --set appName=$ARTIFACT_ID \
|
| 126 | --set appName=$ARTIFACT_ID \
|
| 127 | --set version=$VERSION \
|
| 128 | --set env=$ENV \
|
| 129 | --set image=$IMAGE_NAME \
|
| 130 | --set namespace=$TILLER_NAMESPACE
|
| 131 |
|
| 132 | echo "Perform Dry Run Of Install"
|
| 133 | helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install --dry-run $ARTIFACT_ID $ARTIFACT_ID \
|
| 134 | --set appName=$ARTIFACT_ID \
|
| 135 | --set version=$VERSION \
|
| 136 | --set env=$ENV \
|
| 137 | --set image=$IMAGE_NAME \
|
| 138 | --set namespace=$TILLER_NAMESPACE
|
| 139 |
|
| 140 |
|
| 141 | echo "Helm Install/Upgrade"
|
| 142 | helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install $ARTIFACT_ID $ARTIFACT_ID \
|
| 143 | --set appName=$ARTIFACT_ID \
|
| 144 | --set version=$VERSION \
|
| 145 | --set env=$ENV \
|
| 146 | --set image=$IMAGE_NAME \
|
| 147 | --set namespace=$TILLER_NAMESPACE
|
| 148 |
|
| 149 | """
|
| 150 | }
|
| 151 | }
|
| 152 |
|
| 153 | }
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | }
|
| 158 | }
|