Created
July 16, 2019 18:36
-
-
Save kamikaz1k/7fe25b604c0edac552e6a71215d41ba9 to your computer and use it in GitHub Desktop.
declarative Jenkins pipeline sample
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
import groovy.json.JsonOutput | |
import groovy.json.JsonSlurper | |
import java.util.Date | |
datadog = new com.freshbooks.jenkinshelper.datadog() | |
github = new com.freshbooks.jenkinshelper.GitHub() | |
helper = new com.freshbooks.jenkinshelper.pipelinehelper() | |
slack = new com.freshbooks.jenkinshelper.slack() | |
def APP_NAME = 'service-name' | |
def APP_VERSION = '' | |
def BUILD_SERVER_NAME = 'service-name' | |
def CODE_COVERAGE_TARGET = '90' | |
def DEPLOY_COMMIT = '' | |
def STAGING_APP_VERSION_URL = "https://ourstagingenvironment/service/service-name/ping" | |
def STAGING_RUNDECK_JOB_ID = 'someuuidforourdeploymentjobforstaging' | |
def PRODUCTION_APP_VERSION_URL = "https://ourprodenvironment/service/service-name/ping" | |
pipeline { | |
agent { | |
label 'master' | |
} | |
options { | |
timeout(time: 1, unit: 'HOURS') | |
preserveStashes() | |
} | |
stages { | |
stage('Changeset Audit') { | |
when { | |
expression { | |
!isNewVersion() | |
} | |
beforeAgent true | |
} | |
steps { | |
script { | |
def audit = helper.auditChangeset(APP_NAME) | |
if (audit == "1") { | |
throw new Exception("Changeset Audit failed - code change and migration not allowed") | |
} | |
} | |
} | |
} | |
stage('Initialization') { | |
agent { | |
label 'master' | |
} | |
when { | |
expression { | |
isNewVersion() | |
} | |
beforeAgent true | |
} | |
steps { | |
script { | |
helper.withGithubCredentials { | |
if (abortOnReleaseBranchFirstCommit()) { | |
echo "Found tag ${tags}, aborting previously tested commit" | |
error("Aborting Release branch build for initial commit") | |
} | |
def tagOutput = sh(script: 'make tag', returnStdout: true).trim() | |
APP_VERSION = tagOutput.split("\n").last() | |
} | |
} | |
} | |
} | |
stage('Build') { | |
parallel { | |
stage('Build - service-name Docker Container') { | |
agent { | |
label 'unit-c5' | |
} | |
stages { | |
stage('Build - Build Application Image') { | |
steps { | |
script { | |
dir('service-name') { | |
DEPLOY_COMMIT = helper.checkoutGit() | |
writeFile file: "DEPLOY_COMMIT.sha", text: DEPLOY_COMMIT | |
stash includes: "DEPLOY_COMMIT.sha", name: 'deploy_commit' | |
if (isNewVersion()) { | |
sh "echo 'generateVersionFileForPythonApp ${APP_VERSION}'" | |
helper.generateVersionFileForPythonApp(APP_NAME, APP_VERSION) | |
} else { | |
sh "echo 'skipping generateVersionFileForPythonApp'" | |
} | |
helper.buildDockerContainer(APP_NAME, DEPLOY_COMMIT) | |
} | |
} | |
} | |
} | |
stage('Build - Push Docker Image') { | |
steps { | |
script { | |
pushDockerServerApps(APP_NAME, DEPLOY_COMMIT) | |
} | |
} | |
} | |
} | |
} | |
stage('Preload Agents') { | |
steps { | |
script { | |
helper.preloadCapybaraAgent() | |
helper.preloadCapybaraAgent() | |
} | |
sh "echo 'Bringing up capybara agents'" | |
} | |
} | |
} | |
} | |
stage('Testing') { | |
when { | |
expression { | |
!isHotfix() | |
} | |
beforeAgent true | |
} | |
parallel { | |
stage('Dependency Audit') { | |
agent { | |
label 'unit-c5' | |
} | |
steps { | |
script { | |
helper.buildDependencyScanner() | |
helper.runPythonDependencyAudit(APP_NAME, DEPLOY_COMMIT) | |
} | |
} | |
} | |
stage('Unit Tests') { | |
agent { | |
label 'unit-c5' | |
} | |
steps { | |
script { | |
runUnitTests(APP_NAME, DEPLOY_COMMIT) | |
} | |
} | |
} | |
stage('Capybara 1') { | |
agent { | |
label 'capybara-c5-large' | |
} | |
stages { | |
stage('Capybara - Cleanup') { | |
steps { | |
script { | |
helper.capybaraCleanup() | |
} | |
} | |
} | |
stage('Capybara - Restart docker') { | |
steps { | |
script { | |
restartDocker() | |
} | |
} | |
} | |
stage('Capybara - Start Backend Services') { | |
steps { | |
script { | |
unstash name: 'deploy_commit' | |
DEPLOY_COMMIT = readFile('DEPLOY_COMMIT.sha').trim() | |
startCapybaraEnvironment(DEPLOY_COMMIT, APP_NAME) | |
} | |
} | |
} | |
stage('Capybara - Test') { | |
steps { | |
script { | |
helper.capybaraRunTestsForBackend(APP_NAME, "", DEPLOY_COMMIT, 1, 2) | |
} | |
} | |
post { | |
always { | |
script { | |
helper.capybaraPostTest() | |
} | |
} | |
} | |
} | |
} | |
} | |
stage('Capybara 2') { | |
agent { | |
label 'capybara-c5-large' | |
} | |
stages { | |
stage('Capybara - Cleanup') { | |
steps { | |
script { | |
helper.capybaraCleanup() | |
} | |
} | |
} | |
stage('Capybara - Restart docker') { | |
steps { | |
script { | |
restartDocker() | |
} | |
} | |
} | |
stage('Capybara - Start Backend Services') { | |
steps { | |
script { | |
unstash name: 'deploy_commit' | |
DEPLOY_COMMIT = readFile('DEPLOY_COMMIT.sha').trim() | |
startCapybaraEnvironment(DEPLOY_COMMIT, APP_NAME) | |
} | |
} | |
} | |
stage('Capybara - Test') { | |
steps { | |
script { | |
helper.capybaraRunTestsForBackend(APP_NAME, "", DEPLOY_COMMIT, 2, 2) | |
} | |
} | |
post { | |
always { | |
script { | |
helper.capybaraPostTest() | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
stage('Deploy') { | |
when { | |
expression { | |
isNewVersion() | |
} | |
beforeAgent true | |
} | |
agent { | |
label 'unit-c5' | |
} | |
stages { | |
stage('Deploy - Push Docker Image') { | |
steps { | |
script { | |
helper.retagWithVersionAndPushDockerImage(APP_NAME, DEPLOY_COMMIT, APP_VERSION) | |
} | |
} | |
} | |
stage('Deploy - Push to Staging') { | |
steps { | |
script { | |
helper.deployApp( | |
APP_NAME, | |
BUILD_SERVER_NAME, | |
STAGING_RUNDECK_JOB_ID, | |
APP_VERSION, | |
'staging' | |
) { | |
getAppVersion(STAGING_APP_VERSION_URL) | |
} | |
} | |
} | |
} | |
stage('Deploy - Build Release Wiki Page') { | |
steps { | |
script { | |
helper.releaseWiki(APP_NAME, APP_VERSION) | |
} | |
} | |
} | |
} | |
} | |
} | |
post { | |
success { | |
sendStatusSlackUser(BUILD_SERVER_NAME, APP_NAME, 'SUCCESS') | |
} | |
failure { | |
sendStatusSlackUser(BUILD_SERVER_NAME, APP_NAME, 'FAILED') | |
} | |
aborted { | |
handleAbort(BUILD_SERVER_NAME, APP_NAME) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment