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