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 | ##############################################################################*/
|
Rohan Patel | 6f7e46b | 2019-09-25 14:35:28 -0400 | [diff] [blame] | 17 |
|
| 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: "org.oran.otf"]
|
| 26 |
|
| 27 | ]]])
|
| 28 |
|
| 29 |
|
| 30 | echo "Build branch: ${env.BRANCH_NAME}"
|
| 31 |
|
| 32 | node("docker") {
|
| 33 | stage 'Checkout'
|
| 34 | checkout scm
|
| 35 | PHASES = PHASE.tokenize('_');
|
| 36 | echo "PHASES : " + PHASES
|
| 37 | pom = readMavenPom file: 'pom.xml'
|
| 38 | ARTIFACT_ID = pom.artifactId;
|
| 39 | VERSION = pom.version;
|
| 40 | LABEL_VERSION = pom.version.replaceAll("\\.", "-");
|
| 41 | echo "LabelVerion: " + LABEL_VERSION
|
| 42 | NAMESPACE = pom.groupId
|
| 43 | echo "Tiller Namespace: " + TILLER_NAMESPACE
|
| 44 | DOCKER_REGISTRY = pom.properties['docker.registry']
|
| 45 |
|
| 46 | if( ENV.equalsIgnoreCase("dev") ){
|
| 47 | IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE + "/" + ARTIFACT_ID + ":" + VERSION
|
| 48 | }
|
| 49 | if( ENV.equalsIgnoreCase("prod") || ENV.equalsIgnoreCase("prod-dr") ){
|
| 50 | IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE + ".prod" + "/" + ARTIFACT_ID + ":" + VERSION
|
| 51 | }
|
| 52 |
|
| 53 | if( ENV.equalsIgnoreCase("st") ){
|
| 54 | IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE + ".st" + "/" + ARTIFACT_ID + ":" + VERSION
|
| 55 | }
|
| 56 | echo "Artifact: " + IMAGE_NAME
|
| 57 |
|
| 58 |
|
| 59 | if( ENV.equalsIgnoreCase("dev") ){
|
| 60 | ROUTER_CONFIG="mysqlRouterConfig-dev.ini"
|
| 61 | }
|
| 62 | if( ENV.equalsIgnoreCase("st") ){
|
| 63 | ROUTER_CONFIG="mysqlRouterConfig-st.ini"
|
| 64 | }
|
| 65 | if( ENV.equalsIgnoreCase("prod") || ENV.equalsIgnoreCase("prod-dr")){
|
| 66 | ROUTER_CONFIG="mysqlRouterConfig-prod.ini"
|
| 67 | }
|
| 68 |
|
| 69 | withEnv(["PATH=${env.PATH}:${tool 'mvn352'}/bin:${tool 'jdk180'}/bin:${env.WORKSPACE}/linux-amd64", "JAVA_HOME=${tool 'jdk180'}", "MAVEN_HOME=${tool 'mvn352'}", "HELM_HOME=${env.WORKSPACE}"]) {
|
| 70 |
|
| 71 | echo "JAVA_HOME=${env.JAVA_HOME}"
|
| 72 | echo "MAVEN_HOME=${env.MAVEN_HOME}"
|
| 73 | echo "PATH=${env.PATH}"
|
| 74 | echo "HELM_HOME=${env.HELM_HOME}"
|
| 75 |
|
| 76 | wrap([$class: 'ConfigFileBuildWrapper', managedFiles: [
|
| 77 | [fileId: 'maven-settings.xml', variable: 'MAVEN_SETTINGS'],
|
| 78 | [fileId: 'maven-settings-security.xml', variable: 'MAVEN_SETTINGS_SECURITY']
|
| 79 | ]]) {
|
| 80 |
|
| 81 |
|
| 82 | if (PHASES.contains("BUILD")) {
|
| 83 | stage 'Compile'
|
| 84 | sh 'mvn -s $MAVEN_SETTINGS -Dsettings.security=$MAVEN_SETTINGS_SECURITY clean compile'
|
| 85 |
|
| 86 | stage 'Unit Test'
|
| 87 | sh 'mvn -s $MAVEN_SETTINGS -Dsettings.security=$MAVEN_SETTINGS_SECURITY test'
|
| 88 |
|
| 89 | stage 'Package'
|
| 90 | sh 'mvn -s $MAVEN_SETTINGS -Dsettings.security=$MAVEN_SETTINGS_SECURITY package'
|
| 91 | //sh 'mvn -DskipTests -Dmaven.test.skip=true -s $MAVEN_SETTINGS package'
|
| 92 |
|
| 93 | stage 'Verify'
|
| 94 | sh 'mvn -s $MAVEN_SETTINGS -Dsettings.security=$MAVEN_SETTINGS_SECURITY verify'
|
| 95 |
|
| 96 | stage 'Publish Artifact'
|
| 97 |
|
| 98 | withCredentials([usernamePassword(credentialsId: MECHID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
|
| 99 |
|
| 100 | echo "Artifact: " + DOCKER_REGISTRY
|
| 101 |
|
| 102 | sh """
|
| 103 | docker login $DOCKER_REGISTRY --username $USERNAME --password $PASSWORD
|
| 104 | docker build -t $IMAGE_NAME -f target/Dockerfile target
|
| 105 | docker push $IMAGE_NAME
|
| 106 | """
|
| 107 | }
|
| 108 |
|
| 109 | }
|
| 110 | if (PHASES.contains("DEPLOY") || PHASES.contains("UNDEPLOY") || PHASES.contains("UNDEPLOYFORCE")) {
|
| 111 |
|
| 112 | stage 'Init Helm'
|
| 113 |
|
| 114 | //check if helm exists if not install
|
| 115 | if (fileExists('linux-amd64/helm')) {
|
| 116 | sh """
|
| 117 | echo "helm is already installed"
|
| 118 | """
|
| 119 | } else {
|
| 120 | //download helm
|
| 121 | sh """
|
| 122 | echo "installing helm"
|
| 123 | wget https://storage.googleapis.com/kubernetes-helm/helm-v2.8.2-linux-amd64.tar.gz
|
| 124 | tar -xf helm-v2.8.2-linux-amd64.tar.gz
|
| 125 | rm helm-v2.8.2-linux-amd64.tar.gz
|
| 126 | """
|
| 127 | }
|
| 128 |
|
| 129 | withCredentials([file(credentialsId: KUBE_CONFIG, variable: 'KUBECONFIG')]) {
|
| 130 |
|
| 131 | dir('helm') {
|
| 132 | //check if charts are valid, and then perform dry run, if successful then upgrade/install charts
|
| 133 |
|
| 134 | if (PHASES.contains("UNDEPLOY")) {
|
| 135 | stage 'Undeploy'
|
| 136 |
|
| 137 | sh """
|
| 138 | helm delete --tiller-namespace=$TILLER_NAMESPACE --purge $ARTIFACT_ID
|
| 139 | """
|
| 140 | }
|
| 141 | if (PHASES.contains("UNDEPLOYFORCE")) {
|
| 142 | stage 'Undeploy Force'
|
| 143 |
|
| 144 | sh """
|
| 145 |
|
| 146 | helm delete --tiller-namespace=$TILLER_NAMESPACE --purge $ARTIFACT_ID &
|
| 147 | chmod 755 forceDelete.sh
|
| 148 | ./forceDelete.sh $ARTIFACT_ID
|
| 149 | """
|
| 150 |
|
| 151 | }
|
| 152 |
|
| 153 | //NOTE Double quotes are used below to access groovy variables like artifact_id and tiller_namespace
|
| 154 | if (PHASES.contains("DEPLOY")) {
|
| 155 | stage 'Deploy'
|
| 156 | withCredentials([
|
| 157 | usernamePassword(credentialsId: OTF_MONGO_DB, usernameVariable: 'USERNAME_MONGO', passwordVariable: 'PASSWORD_MONGO'),
|
| 158 | usernamePassword(credentialsId: OTF_CAMUNDA_DB, usernameVariable: 'USERNAME_CAMUNDA', passwordVariable: 'PASSWORD_CAMUNDA')
|
| 159 | ]) {
|
| 160 |
|
| 161 | sh """
|
| 162 |
|
| 163 | echo "Validate Yaml"
|
| 164 | helm lint $ARTIFACT_ID
|
| 165 |
|
| 166 | echo "View Helm Templates"
|
| 167 | helm template $ARTIFACT_ID \
|
| 168 | --set appName=$ARTIFACT_ID \
|
| 169 | --set version=$VERSION \
|
| 170 | --set image=$IMAGE_NAME \
|
| 171 | --set namespace=$TILLER_NAMESPACE \
|
| 172 | --set env=$ENV \
|
| 173 | --set otf.mongo.username=$USERNAME_MONGO \
|
| 174 | --set otf.mongo.password=$PASSWORD_MONGO \
|
| 175 | --set otf.camunda.db.username=$USERNAME_CAMUNDA \
|
| 176 | --set otf.camunda.db.password=$PASSWORD_CAMUNDA \
|
| 177 |
|
| 178 |
|
| 179 | echo "Perform Dry Run Of Install"
|
| 180 | helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install --dry-run $ARTIFACT_ID $ARTIFACT_ID \
|
| 181 | --set version=$VERSION \
|
| 182 | --set image=$IMAGE_NAME \
|
| 183 | --set namespace=$TILLER_NAMESPACE \
|
| 184 | --set env=$ENV \
|
| 185 | --set otf.mongo.username=$USERNAME_MONGO \
|
| 186 | --set otf.mongo.password=$PASSWORD_MONGO \
|
| 187 | --set otf.camunda.db.username=$USERNAME_CAMUNDA \
|
| 188 | --set otf.camunda.db.password=$PASSWORD_CAMUNDA \
|
| 189 |
|
| 190 |
|
| 191 | echo "Helm Install/Upgrade"
|
| 192 | helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install $ARTIFACT_ID $ARTIFACT_ID \
|
| 193 | --set version=$VERSION \
|
| 194 | --set image=$IMAGE_NAME \
|
| 195 | --set namespace=$TILLER_NAMESPACE \
|
| 196 | --set env=$ENV \
|
| 197 | --set otf.mongo.username=$USERNAME_MONGO \
|
| 198 | --set otf.mongo.password=$PASSWORD_MONGO \
|
| 199 | --set otf.camunda.db.username=$USERNAME_CAMUNDA \
|
| 200 | --set otf.camunda.db.password=$PASSWORD_CAMUNDA \
|
| 201 |
|
| 202 | """
|
| 203 | }
|
| 204 | }
|
| 205 |
|
| 206 | }
|
| 207 | }
|
| 208 | }
|
| 209 | }
|
| 210 | }
|
| 211 | } |