Rohan Patel | a99e574 | 2019-09-23 15:04:19 -0400 | [diff] [blame^] | 1 | #!/usr/bin/env groovy
|
| 2 |
|
| 3 |
|
| 4 | properties([[$class: 'ParametersDefinitionProperty', parameterDefinitions: [
|
| 5 | [$class: 'hudson.model.StringParameterDefinition', name: 'PHASE', defaultValue: "BUILD"],
|
| 6 | [$class: 'hudson.model.StringParameterDefinition', name: 'ENV', defaultValue: "dev"],
|
| 7 | [$class: 'hudson.model.StringParameterDefinition', name: 'MECHID', defaultValue: "id_otf_dev"],
|
| 8 | [$class: 'hudson.model.StringParameterDefinition', name: 'KUBE_CONFIG', defaultValue: "kubeConfig-dev"],
|
| 9 | [$class: 'hudson.model.StringParameterDefinition', name: 'TILLER_NAMESPACE', defaultValue: "org-oran-otf"]
|
| 10 | ]]])
|
| 11 |
|
| 12 |
|
| 13 | echo "Build branch: ${env.BRANCH_NAME}"
|
| 14 |
|
| 15 | node("docker"){
|
| 16 | stage 'Checkout'
|
| 17 | checkout scm
|
| 18 | PHASES=PHASE.tokenize( '_' );
|
| 19 | echo "PHASES : " + PHASES
|
| 20 |
|
| 21 |
|
| 22 | ARTIFACT_ID="otf-ssh-test-head";
|
| 23 | VERSION="Blitzcrank.1.1";
|
| 24 | NAMESPACE="org-oran-otf"
|
| 25 | DOCKER_REGISTRY="registry.hub.docker.io"
|
| 26 |
|
| 27 | if( ENV.equalsIgnoreCase("dev") ){
|
| 28 | IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + "/" + ARTIFACT_ID + ":" + VERSION
|
| 29 |
|
| 30 | }
|
| 31 | if( ENV.equalsIgnoreCase("prod") || ENV.equalsIgnoreCase("prod-dr")){
|
| 32 | IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + ".prod" + "/" + ARTIFACT_ID + ":" + VERSION
|
| 33 |
|
| 34 | }
|
| 35 |
|
| 36 | if( ENV.equalsIgnoreCase("st") ){
|
| 37 | IMAGE_NAME=DOCKER_REGISTRY + "/" + NAMESPACE + ".st" + "/" + ARTIFACT_ID + ":" + VERSION
|
| 38 |
|
| 39 | }
|
| 40 |
|
| 41 | echo "Artifact: " + IMAGE_NAME
|
| 42 |
|
| 43 | withEnv(["PATH=${env.PATH}:${env.WORKSPACE}/linux-amd64", "HELM_HOME=${env.WORKSPACE}"]) {
|
| 44 |
|
| 45 | echo "PATH=${env.PATH}"
|
| 46 | echo "HELM_HOME=${env.HELM_HOME}"
|
| 47 |
|
| 48 | if (PHASES.contains("BUILD")){
|
| 49 |
|
| 50 | stage 'Publish Artifact'
|
| 51 |
|
| 52 | withCredentials([usernamePassword(credentialsId: MECHID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
|
| 53 |
|
| 54 | echo "Artifact: " + IMAGE_NAME
|
| 55 |
|
| 56 | sh """
|
| 57 | docker login $DOCKER_REGISTRY --username $USERNAME --password $PASSWORD
|
| 58 | docker build -t $IMAGE_NAME .
|
| 59 | docker push $IMAGE_NAME
|
| 60 | """
|
| 61 | }
|
| 62 |
|
| 63 | }
|
| 64 |
|
| 65 | if (PHASES.contains("DEPLOY") || PHASES.contains("UNDEPLOY")) {
|
| 66 |
|
| 67 | stage 'Init Helm'
|
| 68 |
|
| 69 | //check if helm exists if not install
|
| 70 | if(fileExists('linux-amd64/helm')){
|
| 71 | sh """
|
| 72 | echo "helm is already installed"
|
| 73 | """
|
| 74 | }
|
| 75 | else{
|
| 76 | //download helm
|
| 77 | sh """
|
| 78 | echo "installing helm"
|
| 79 | wget https://storage.googleapis.com/kubernetes-helm/helm-v2.8.2-linux-amd64.tar.gz
|
| 80 | tar -xf helm-v2.8.2-linux-amd64.tar.gz
|
| 81 | rm helm-v2.8.2-linux-amd64.tar.gz
|
| 82 | """
|
| 83 | }
|
| 84 |
|
| 85 | withCredentials([file(credentialsId: KUBE_CONFIG, variable: 'KUBECONFIG')]) {
|
| 86 |
|
| 87 | dir('helm'){
|
| 88 | //check if charts are valid, and then perform dry run, if successful then upgrade/install charts
|
| 89 |
|
| 90 | if (PHASES.contains("UNDEPLOY") ) {
|
| 91 | stage 'Undeploy'
|
| 92 |
|
| 93 | sh """
|
| 94 | helm delete --tiller-namespace=$TILLER_NAMESPACE --purge $ARTIFACT_ID
|
| 95 | """
|
| 96 | }
|
| 97 |
|
| 98 | //NOTE Double quotes are used below to access groovy variables like artifact_id and tiller_namespace
|
| 99 | if (PHASES.contains("DEPLOY") ){
|
| 100 | stage 'Deploy'
|
| 101 | withCredentials([
|
| 102 | usernamePassword(credentialsId: MECHID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD'),
|
| 103 | file(credentialsId: 'id_PRIVATE_KEY', variable: 'VAR_id_PRIVATE_KEY'),
|
| 104 | string(credentialsId: 'id_otf_key_passphrase', variable: 'VAR_id_otf_key_passphrase')
|
| 105 | ]) {
|
| 106 |
|
| 107 | sh """
|
| 108 |
|
| 109 | cp $VAR_id_PRIVATE_KEY $ARTIFACT_ID
|
| 110 | FILE_id_PRIVATE_KEY=`basename $VAR_id_PRIVATE_KEY`
|
| 111 |
|
| 112 | echo "Validate Yaml"
|
| 113 | helm lint $ARTIFACT_ID
|
| 114 |
|
| 115 | echo "View Helm Templates"
|
| 116 | helm template $ARTIFACT_ID --set appName=$ARTIFACT_ID \
|
| 117 | --set appName=$ARTIFACT_ID \
|
| 118 | --set version=$VERSION \
|
| 119 | --set env=$ENV \
|
| 120 | --set image=$IMAGE_NAME \
|
| 121 | --set namespace=$TILLER_NAMESPACE \
|
| 122 | --set Secret.id_private_key=\$FILE_id_PRIVATE_KEY \
|
| 123 | --set Secret.id_private_key_passphrase=$VAR_id_otf_key_passphrase \
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 | echo "Perform Dry Run Of Install"
|
| 128 | helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install --dry-run $ARTIFACT_ID $ARTIFACT_ID \
|
| 129 | --set appName=$ARTIFACT_ID \
|
| 130 | --set version=$VERSION \
|
| 131 | --set env=$ENV \
|
| 132 | --set image=$IMAGE_NAME \
|
| 133 | --set namespace=$TILLER_NAMESPACE \
|
| 134 | --set Secret.id_private_key=\$FILE_id_PRIVATE_KEY \
|
| 135 | --set Secret.id_private_key_passphrase=$VAR_id_otf_key_passphrase \
|
| 136 |
|
| 137 |
|
| 138 | echo "Helm Install/Upgrade"
|
| 139 | helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install $ARTIFACT_ID $ARTIFACT_ID \
|
| 140 | --set appName=$ARTIFACT_ID \
|
| 141 | --set version=$VERSION \
|
| 142 | --set env=$ENV \
|
| 143 | --set image=$IMAGE_NAME \
|
| 144 | --set namespace=$TILLER_NAMESPACE \
|
| 145 | --set Secret.id_private_key=\$FILE_id_PRIVATE_KEY \
|
| 146 | --set Secret.id_private_key_passphrase=$VAR_id_otf_key_passphrase \
|
| 147 |
|
| 148 | """
|
| 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | }
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | }
|
| 157 | }
|