Created
May 19, 2018 00:17
-
-
Save woody3000/f80092031c90a35d7751d57a5f084a99 to your computer and use it in GitHub Desktop.
Advanced Jenkinsfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO_GIT_PUSH = false | |
GIT_PUSH_CRED_ID = | |
GIT_PUSH_URL = | |
DO_ARCHIVE = false | |
ARCHIVE_GIT_CRED_ID = | |
ARCHIVE_AWS_CRED_ID = | |
S3_UPLOAD_LOCATION = | |
SEND_NOTIFICATIONS = false | |
deploy.DO_CF_DEPLOY = false | |
deploy.CF_APP_NAME = | |
deploy.CF_ENDPOINT = | |
deploy.CF_CRED_ID = | |
deploy.CF_ORG = | |
deploy.CF_SPACE = | |
deploy.service.db.name = | |
deploy.service.db.type = | |
deploy.service.db.plan = | |
deploy.service.db.reset = false | |
runtime.CF_ENDPOINT = | |
runtime.CF_ORG = | |
runtime.CF_SPACE = | |
runtime.CF_USERNAME = | |
runtime.CF_PASSWORD = | |
runtime.POSTGRES_VCAP_CONFIG_KEY = | |
runtime.FLYWAY_COMMAND = migrate | |
runtime.AWS_ACCESS_KEY = | |
runtime.AWS_SECRET_KEY = | |
runtime.AWS_REGION = | |
runtime.KMS_KEY_ARN = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!groovy | |
pipeline { | |
agent any | |
tools { | |
maven 'Maven-3' | |
jdk 'JDK 1.8uLATEST' | |
} | |
options { | |
timeout(time: 15, unit: 'MINUTES') | |
disableConcurrentBuilds() | |
buildDiscarder(logRotator(numToKeepStr:'10')) | |
skipDefaultCheckout true | |
} | |
triggers { | |
pollSCM('H/5 * * * *') | |
} | |
environment { | |
DEFAULT_CONFIG_FILE="${env.WORKSPACE}/default_jenkins_config.props" | |
OVERRIDE_PROJECT_CONFIG_ID="${env.JOB_NAME}".replaceAll('/.+' + '$', '') | |
OVERRIDE_BRANCH_CONFIG_ID="${env.JOB_NAME}" | |
TMP_DIR="${env.WORKSPACE}/tmp" | |
EXPORT_GIT_REPO_DIRNAME="${env.JOB_BASE_NAME}-git_export" | |
} | |
parameters { | |
choice(name: 'DO_GIT_PUSH', | |
description: 'Whether or not to push code to the upstream repo after a successful build', | |
choices: 'true\nfalse') | |
choice(name: 'DO_ARCHIVE', | |
description: 'Whether or not to archive the build artifacts, export the git repo, and upload both to S3', | |
choices: 'true\nfalse') | |
} | |
stages { | |
stage('Pre-Clean') { | |
steps { | |
deleteDir() | |
checkout scm | |
} | |
} | |
stage('Load Configs') { | |
steps { | |
script { | |
// load the default config from the repo | |
def allConfigs = readProperties file: "${env.DEFAULT_CONFIG_FILE}" | |
// merge project-specific settings | |
try { | |
configFileProvider([configFile(fileId: "${env.OVERRIDE_PROJECT_CONFIG_ID}", targetLocation: "${env.OVERRIDE_PROJECT_CONFIG_ID}.props")]) {} | |
def projectConfigs = readProperties defaults: allConfigs, file: "${env.OVERRIDE_PROJECT_CONFIG_ID}.props" | |
allConfigs = projectConfigs | |
} | |
catch(IllegalArgumentException e) { | |
echo "Unable to find override config ids for project: ${env.OVERRIDE_PROJECT_CONFIG_ID}" | |
} | |
// merge branch-specific settings | |
try { | |
configFileProvider([configFile(fileId: "${env.OVERRIDE_BRANCH_CONFIG_ID}", targetLocation: "${env.OVERRIDE_BRANCH_CONFIG_ID}.props")]) {} | |
def branchConfigs = readProperties defaults: allConfigs, file: "${env.OVERRIDE_BRANCH_CONFIG_ID}.props" | |
allConfigs = branchConfigs | |
} | |
catch(IllegalArgumentException e) { | |
echo "Unable to find override config ids for branch: ${env.OVERRIDE_BRANCH_CONFIG_ID}" | |
} | |
// extract config values from combined configs for use in other steps | |
_DO_GIT_PUSH = allConfigs['DO_GIT_PUSH'] | |
_GIT_PUSH_CRED_ID = allConfigs['GIT_PUSH_CRED_ID'] | |
_GIT_PUSH_URL = allConfigs['GIT_PUSH_URL'] | |
_DO_ARCHIVE = allConfigs['DO_ARCHIVE'] | |
_S3_UPLOAD_LOCATION = allConfigs['S3_UPLOAD_LOCATION'] | |
_ARCHIVE_AWS_CRED_ID = allConfigs['ARCHIVE_AWS_CRED_ID'] | |
_ARCHIVE_GIT_CRED_ID = allConfigs['ARCHIVE_GIT_CRED_ID'] | |
_SEND_NOTIFICATIONS = allConfigs['SEND_NOTIFICATIONS'] | |
} | |
} | |
} | |
stage('Print Env Vars') { | |
steps { | |
echo "PATH = ${PATH}" | |
echo "M2_HOME = ${M2_HOME}" | |
echo "DO_GIT_PUSH = ${_DO_GIT_PUSH}" | |
echo "GIT_PUSH_CRED_ID = ${_GIT_PUSH_CRED_ID}" | |
echo "GIT_PUSH_URL = ${_GIT_PUSH_URL}" | |
echo "DO_ARCHIVE = ${_DO_ARCHIVE}" | |
echo "ARCHIVE_GIT_CRED_ID = ${_ARCHIVE_GIT_CRED_ID}" | |
echo "ARCHIVE_AWS_CRED_ID = ${_ARCHIVE_AWS_CRED_ID}" | |
echo "S3_UPLOAD_LOCATION = ${_S3_UPLOAD_LOCATION}" | |
echo "SEND_NOTIFICATIONS = ${_SEND_NOTIFICATIONS}" | |
} | |
} | |
stage('Build') { | |
steps { | |
sh 'mvn clean compile -DskipTests=true' | |
} | |
} | |
stage('Test') { | |
parallel { | |
stage('Unit Tests') { | |
steps { | |
sh 'mvn test' | |
} | |
} | |
stage('Integration Tests') { | |
steps { | |
sh 'mvn integration-test' | |
} | |
} | |
} | |
} | |
stage('Package') { | |
steps { | |
sh 'mvn install -DskipTests=true' | |
} | |
} | |
stage('Archive build artifacts') { | |
when { | |
expression { _DO_ARCHIVE == 'true' && params.DO_ARCHIVE == 'true' } | |
} | |
steps { | |
sh """ | |
mkdir -p ${TMP_DIR} | |
rm -rf ${TMP_DIR}/${env.JOB_BASE_NAME}.zip | |
zip ${TMP_DIR}/${env.JOB_BASE_NAME}.zip ods-cf-service-broker/target/ods-cf-service-broker.jar ods-cf-service-broker/manifest.yml | |
""" | |
} | |
} | |
stage('Export git repo') { | |
when { | |
expression { _DO_ARCHIVE == 'true' && params.DO_ARCHIVE == 'true' } | |
} | |
steps { | |
script { | |
_SCM_URL = sh(returnStdout: true, script: 'git config remote.origin.url').trim() | |
echo _SCM_URL | |
} | |
checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: '**']], | |
doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', | |
relativeTargetDir: "${env.EXPORT_GIT_REPO_DIRNAME}"], [$class: 'CleanBeforeCheckout']], submoduleCfg: [], | |
userRemoteConfigs: [[ | |
credentialsId: "${_ARCHIVE_GIT_CRED_ID}", | |
url: _SCM_URL | |
]]] | |
sh """ | |
mkdir -p ${TMP_DIR} | |
rm -rf ${TMP_DIR}/${env.EXPORT_GIT_REPO_DIRNAME}.zip | |
zip -r ${TMP_DIR}/${env.EXPORT_GIT_REPO_DIRNAME}.zip ${env.EXPORT_GIT_REPO_DIRNAME} | |
""" | |
} | |
} | |
stage('Upload to S3') { | |
when { | |
expression { _DO_ARCHIVE == 'true' && params.DO_ARCHIVE == 'true' } | |
} | |
environment { | |
AWS_CREDS = credentials("${_ARCHIVE_AWS_CRED_ID}") | |
AWS_ACCESS_KEY_ID = AWS_CREDS_USR | |
AWS_SECRET_ACCESS_KEY = AWS_CREDS_PSW | |
} | |
steps { | |
sh """ | |
aws s3 cp ${TMP_DIR}/${env.JOB_BASE_NAME}.zip s3://${_S3_UPLOAD_LOCATION} | |
aws s3 cp ${TMP_DIR}/${env.EXPORT_GIT_REPO_DIRNAME}.zip s3://${_S3_UPLOAD_LOCATION} | |
""" | |
} | |
} | |
stage('Push Code Up') { | |
when { | |
expression { _DO_GIT_PUSH == 'true' && params.DO_GIT_PUSH == 'true' } | |
} | |
steps { | |
echo "Pushin code to url: ${_GIT_PUSH_URL} using credential id: ${_GIT_PUSH_CRED_ID}" | |
sshagent(["${_GIT_PUSH_CRED_ID}"]) { | |
sh """ | |
git remote remove next_repo || true | |
git remote add next_repo ${_GIT_PUSH_URL} | |
git checkout ${env.BRANCH_NAME} | |
git merge --ff-only origin/${env.BRANCH_NAME} | |
git push next_repo ${env.BRANCH_NAME} | |
""" | |
} | |
} | |
} | |
} | |
post { | |
failure { | |
script { | |
if(_SEND_NOTIFICATIONS) { | |
emailext body: '${JELLY_SCRIPT, template="html_gmail.jelly"}', | |
mimeType: 'text/html', | |
recipientProviders: [developers(), culprits(), requestor(), brokenTestsSuspects(), brokenBuildSuspects(), upstreamDevelopers()], | |
subject: '$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS!', | |
to: '$DEFAULT_RECIPIENTS' | |
} | |
} | |
} | |
regression { | |
script { | |
if(_SEND_NOTIFICATIONS) { | |
emailext body: '${JELLY_SCRIPT, template="html_gmail.jelly"}', | |
mimeType: 'text/html', | |
recipientProviders: [developers(), culprits(), requestor(), brokenTestsSuspects(), brokenBuildSuspects(), upstreamDevelopers()], | |
subject: '$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS!', | |
to: '$DEFAULT_RECIPIENTS' | |
} | |
} | |
} | |
fixed { | |
script { | |
if(_SEND_NOTIFICATIONS) { | |
emailext body: '${JELLY_SCRIPT, template="html_gmail.jelly"}', | |
mimeType: 'text/html', | |
recipientProviders: [developers(), culprits(), requestor(), brokenTestsSuspects(), brokenBuildSuspects(), upstreamDevelopers()], | |
subject: '$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS!', | |
to: '$DEFAULT_RECIPIENTS' | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment