#!/usr/bin/env groovy | |
/* Copyright (c) 2019 AT&T Intellectual Property. # | |
# # | |
# Licensed under the Apache License, Version 2.0 (the "License"); # | |
# you may not use this file except in compliance with the License. # | |
# You may obtain a copy of the License at # | |
# # | |
# http://www.apache.org/licenses/LICENSE-2.0 # | |
# # | |
# Unless required by applicable law or agreed to in writing, software # | |
# distributed under the License is distributed on an "AS IS" BASIS, # | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # | |
# See the License for the specific language governing permissions and # | |
# limitations under the License. # | |
##############################################################################*/ | |
properties([[$class: 'ParametersDefinitionProperty', parameterDefinitions: [ | |
[$class: 'hudson.model.StringParameterDefinition', name: 'PHASE', defaultValue: "BUILD"], | |
[$class: 'hudson.model.StringParameterDefinition', name: 'ENV', defaultValue: "dev"], | |
[$class: 'hudson.model.StringParameterDefinition', name: 'MECHID', defaultValue: "username"], | |
[$class: 'hudson.model.StringParameterDefinition', name: 'KUBE_CONFIG', defaultValue: "kubeConfig-dev"], | |
[$class: 'hudson.model.StringParameterDefinition', name: 'OTF_MONGO_DB', defaultValue: "otf_mongo_dev_db"], | |
[$class: 'hudson.model.StringParameterDefinition', name: 'OTF_CAMUNDA_DB', defaultValue: "otf_camunda_dev_db"], | |
[$class: 'hudson.model.StringParameterDefinition', name: 'TILLER_NAMESPACE', defaultValue: "org.oran.otf"] | |
]]]) | |
echo "Build branch: ${env.BRANCH_NAME}" | |
node("docker") { | |
stage 'Checkout' | |
checkout scm | |
PHASES = PHASE.tokenize('_'); | |
echo "PHASES : " + PHASES | |
pom = readMavenPom file: 'pom.xml' | |
ARTIFACT_ID = pom.artifactId; | |
VERSION = pom.version; | |
LABEL_VERSION = pom.version.replaceAll("\\.", "-"); | |
echo "LabelVerion: " + LABEL_VERSION | |
NAMESPACE = pom.groupId | |
echo "Tiller Namespace: " + TILLER_NAMESPACE | |
DOCKER_REGISTRY = pom.properties['docker.registry'] | |
if( ENV.equalsIgnoreCase("dev") ){ | |
IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE + "/" + ARTIFACT_ID + ":" + VERSION | |
} | |
if( ENV.equalsIgnoreCase("prod") || ENV.equalsIgnoreCase("prod-dr") ){ | |
IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE + ".prod" + "/" + ARTIFACT_ID + ":" + VERSION | |
} | |
if( ENV.equalsIgnoreCase("st") ){ | |
IMAGE_NAME = pom.properties['docker.registry'] + "/" + NAMESPACE + ".st" + "/" + ARTIFACT_ID + ":" + VERSION | |
} | |
echo "Artifact: " + IMAGE_NAME | |
if( ENV.equalsIgnoreCase("dev") ){ | |
ROUTER_CONFIG="mysqlRouterConfig-dev.ini" | |
} | |
if( ENV.equalsIgnoreCase("st") ){ | |
ROUTER_CONFIG="mysqlRouterConfig-st.ini" | |
} | |
if( ENV.equalsIgnoreCase("prod") || ENV.equalsIgnoreCase("prod-dr")){ | |
ROUTER_CONFIG="mysqlRouterConfig-prod.ini" | |
} | |
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}"]) { | |
echo "JAVA_HOME=${env.JAVA_HOME}" | |
echo "MAVEN_HOME=${env.MAVEN_HOME}" | |
echo "PATH=${env.PATH}" | |
echo "HELM_HOME=${env.HELM_HOME}" | |
wrap([$class: 'ConfigFileBuildWrapper', managedFiles: [ | |
[fileId: 'maven-settings.xml', variable: 'MAVEN_SETTINGS'], | |
[fileId: 'maven-settings-security.xml', variable: 'MAVEN_SETTINGS_SECURITY'] | |
]]) { | |
if (PHASES.contains("BUILD")) { | |
stage 'Compile' | |
sh 'mvn -s $MAVEN_SETTINGS -Dsettings.security=$MAVEN_SETTINGS_SECURITY clean compile' | |
stage 'Unit Test' | |
sh 'mvn -s $MAVEN_SETTINGS -Dsettings.security=$MAVEN_SETTINGS_SECURITY test' | |
stage 'Package' | |
sh 'mvn -s $MAVEN_SETTINGS -Dsettings.security=$MAVEN_SETTINGS_SECURITY package' | |
//sh 'mvn -DskipTests -Dmaven.test.skip=true -s $MAVEN_SETTINGS package' | |
stage 'Verify' | |
sh 'mvn -s $MAVEN_SETTINGS -Dsettings.security=$MAVEN_SETTINGS_SECURITY verify' | |
stage 'Publish Artifact' | |
withCredentials([usernamePassword(credentialsId: MECHID, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { | |
echo "Artifact: " + DOCKER_REGISTRY | |
sh """ | |
docker login $DOCKER_REGISTRY --username $USERNAME --password $PASSWORD | |
docker build -t $IMAGE_NAME -f target/Dockerfile target | |
docker push $IMAGE_NAME | |
""" | |
} | |
} | |
if (PHASES.contains("DEPLOY") || PHASES.contains("UNDEPLOY") || PHASES.contains("UNDEPLOYFORCE")) { | |
stage 'Init Helm' | |
//check if helm exists if not install | |
if (fileExists('linux-amd64/helm')) { | |
sh """ | |
echo "helm is already installed" | |
""" | |
} else { | |
//download helm | |
sh """ | |
echo "installing helm" | |
wget https://storage.googleapis.com/kubernetes-helm/helm-v2.8.2-linux-amd64.tar.gz | |
tar -xf helm-v2.8.2-linux-amd64.tar.gz | |
rm helm-v2.8.2-linux-amd64.tar.gz | |
""" | |
} | |
withCredentials([file(credentialsId: KUBE_CONFIG, variable: 'KUBECONFIG')]) { | |
dir('helm') { | |
//check if charts are valid, and then perform dry run, if successful then upgrade/install charts | |
if (PHASES.contains("UNDEPLOY")) { | |
stage 'Undeploy' | |
sh """ | |
helm delete --tiller-namespace=$TILLER_NAMESPACE --purge $ARTIFACT_ID | |
""" | |
} | |
if (PHASES.contains("UNDEPLOYFORCE")) { | |
stage 'Undeploy Force' | |
sh """ | |
helm delete --tiller-namespace=$TILLER_NAMESPACE --purge $ARTIFACT_ID & | |
chmod 755 forceDelete.sh | |
./forceDelete.sh $ARTIFACT_ID | |
""" | |
} | |
//NOTE Double quotes are used below to access groovy variables like artifact_id and tiller_namespace | |
if (PHASES.contains("DEPLOY")) { | |
stage 'Deploy' | |
withCredentials([ | |
usernamePassword(credentialsId: OTF_MONGO_DB, usernameVariable: 'USERNAME_MONGO', passwordVariable: 'PASSWORD_MONGO'), | |
usernamePassword(credentialsId: OTF_CAMUNDA_DB, usernameVariable: 'USERNAME_CAMUNDA', passwordVariable: 'PASSWORD_CAMUNDA') | |
]) { | |
sh """ | |
echo "Validate Yaml" | |
helm lint $ARTIFACT_ID | |
echo "View Helm Templates" | |
helm template $ARTIFACT_ID \ | |
--set appName=$ARTIFACT_ID \ | |
--set version=$VERSION \ | |
--set image=$IMAGE_NAME \ | |
--set namespace=$TILLER_NAMESPACE \ | |
--set env=$ENV \ | |
--set otf.mongo.username=$USERNAME_MONGO \ | |
--set otf.mongo.password=$PASSWORD_MONGO \ | |
--set otf.camunda.db.username=$USERNAME_CAMUNDA \ | |
--set otf.camunda.db.password=$PASSWORD_CAMUNDA \ | |
echo "Perform Dry Run Of Install" | |
helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install --dry-run $ARTIFACT_ID $ARTIFACT_ID \ | |
--set version=$VERSION \ | |
--set image=$IMAGE_NAME \ | |
--set namespace=$TILLER_NAMESPACE \ | |
--set env=$ENV \ | |
--set otf.mongo.username=$USERNAME_MONGO \ | |
--set otf.mongo.password=$PASSWORD_MONGO \ | |
--set otf.camunda.db.username=$USERNAME_CAMUNDA \ | |
--set otf.camunda.db.password=$PASSWORD_CAMUNDA \ | |
echo "Helm Install/Upgrade" | |
helm upgrade --tiller-namespace=$TILLER_NAMESPACE --install $ARTIFACT_ID $ARTIFACT_ID \ | |
--set version=$VERSION \ | |
--set image=$IMAGE_NAME \ | |
--set namespace=$TILLER_NAMESPACE \ | |
--set env=$ENV \ | |
--set otf.mongo.username=$USERNAME_MONGO \ | |
--set otf.mongo.password=$PASSWORD_MONGO \ | |
--set otf.camunda.db.username=$USERNAME_CAMUNDA \ | |
--set otf.camunda.db.password=$PASSWORD_CAMUNDA \ | |
""" | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} |