#!/usr/bin/env groovy env.APPLICATION_NAME = 'spam-filter' env.APPLICATION_LABEL = 'utilities' env.GIT_BRANCH = 'master' env.GIT_REPOSITORY_PATH = "github.com/andyjk15/${env.APPLICATION_NAME}.git" env.GIT_REPOSITORY_URL = "https://${env.GIT_REPOSITORY_PATH}" env.GITHUB_CREDENTIALS_ID = 'Github' env.DOCKER_REPOSITORY = 'registry.cryptosky.me' env.DOCKER_REPOSITORY_URL = "https://${env.DOCKER_REPOSITORY}" env.DOCKER_REPOSITORY_TCP = "tcp://${env.DOCKER_REPOSITORY}:4243" env.NAMESPACE = 'production' env.SLAVE_LABEL = "cryptosky-aio-build" def mvn( String gloals ) { sh "mvn -s configuration/settings.xml --show-version --batch-mode ${gloals}" } String get_application_version() { "1.0.0-b${env.BUILD_NUMBER}" } String executeShellScript( String shellPath, String arg1 = '', String arg2 = '', String arg3 = '', String arg4 = '' ) { sh "./${shellPath} ${arg1} ${arg2} ${arg3} ${arg4}" } try { timestamps { node ("${env.SLAVE_LABEL}") { stage('Initialise') { checkout([$class: 'GitSCM', branches: [[name: 'master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'Github', url: ${env.GIT_REPOSITORY_URL}]]]) env.APPLICATION_VERSION = get_application_version() withCredentials( [usernamePassword( credentialsId: 'doctl', passwordVariable: 'DOCTL_TOKEN', usernameVariable: 'DOCTL_USERNAME' )] ) { sh "doctl auth init --access-token ${DOCTL_TOKEN}" sh "doctl kubernetes cluster kubeconfig save cryptosky-kubernetes-cluster-production" } } stage('Test Artifact') { try { // mvn 'verify -DskipUTs -DskipTests' } finally { // mvn 'test' } } stage('Build Image') { // mvn 'clean package -DskipTests' executeShellScript("configuration/scripts/mapVarsToConfigs.sh", env.DOCKER_REPOSITORY, env.APPLICATION_NAME, env.APPLICATION_VERSION, env.APPLICATION_LABEL) } stage('Tag Repository') { withDockerServer([uri: "${env.DOCKER_REPOSITORY_TCP}"]) { withDockerRegistry([credentialsId: 'Registry', url: "${env.DOCKER_REPOSITORY_URL}"]) { def image = docker.build("${env.DOCKER_REPOSITORY}/${env.APPLICATION_NAME}:${env.APPLICATION_VERSION}") image.push() def latest = docker.build("${env.DOCKER_REPOSITORY}/${env.APPLICATION_NAME}:latest") latest.push() } } withCredentials( [usernamePassword( credentialsId: env.GITHUB_CREDENTIALS_ID, passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME' )] ) { sh "git tag ${env.APPLICATION_VERSION}" sh "git push https://${GIT_USERNAME}:${GIT_PASSWORD}@${env.GIT_REPOSITORY_PATH} ${env.APPLICATION_VERSION}" } } stage('Deploy') { executeShellScript("configuration/scripts/deployToKubernetes.sh", env.APPLICATION_NAME) } } } } catch ( exception ) { currentBuild.result = 'FAILURE' throw exception } finally { currentBuild.result = 'SUCCESS' }