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