This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Jenkinsfile.cicd
148 lines (131 loc) · 4.71 KB
/
Jenkinsfile.cicd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!groovy
// --------------------
// Declarative Pipeline
// --------------------
pipeline {
agent any
environment {
// Enable pipeline verbose debug output if greater than 0
DEBUG_OUTPUT = 'false'
// Get projects/namespaces from config maps
DEV_PROJECT = new File('/var/run/configs/ns/project.dev').getText('UTF-8').trim()
TEST_PROJECT = new File('/var/run/configs/ns/project.test').getText('UTF-8').trim()
PROD_PROJECT = new File('/var/run/configs/ns/project.prod').getText('UTF-8').trim()
TOOLS_PROJECT = new File('/var/run/configs/ns/project.tools').getText('UTF-8').trim()
// Get application config from config maps
REPO_OWNER = new File('/var/run/configs/jobs/repo.owner').getText('UTF-8').trim()
REPO_NAME = new File('/var/run/configs/jobs/repo.name').getText('UTF-8').trim()
APP_NAME = new File('/var/run/configs/jobs/app.name').getText('UTF-8').trim()
APP_DOMAIN = new File('/var/run/configs/jobs/app.domain').getText('UTF-8').trim()
// JOB_NAME should be the pull request/branch identifier (i.e. 'pr-5')
JOB_NAME = JOB_BASE_NAME.toLowerCase()
// SOURCE_REPO_* references git repository resources
SOURCE_REPO_RAW = "https://raw.githubusercontent.com/${REPO_OWNER}/${REPO_NAME}/master"
SOURCE_REPO_REF = "pull/${CHANGE_ID}/head"
SOURCE_REPO_URL = "https://github.com/${REPO_OWNER}/${REPO_NAME}.git"
// ENV_HOST is the full domain without the path (ie. 'appname-dev.pathfinder.gov.bc.ca')
DEV_HOST = "${REPO_NAME}-dev.${APP_DOMAIN}"
TEST_HOST = "${REPO_NAME}-test.${APP_DOMAIN}"
PROD_HOST = "${REPO_NAME}.${APP_DOMAIN}"
// PATH_ROOT will be appended to ENV_HOST
PATH_ROOT = "/${JOB_NAME.equalsIgnoreCase('master') ? 'app' : JOB_NAME}"
}
options {
parallelsAlwaysFailFast()
}
stages {
stage('Initialize') {
agent any
steps {
// Cancel any running builds in progress
timeout(time: 10, unit: 'MINUTES') {
echo "Cancelling previous ${APP_NAME}-${JOB_NAME} builds in progress..."
abortAllPreviousBuildInProgress(currentBuild)
}
script {
if(DEBUG_OUTPUT.equalsIgnoreCase('true')) {
// Force OpenShift Plugin directives to be verbose
openshift.logLevel(1)
// Print all environment variables
echo 'DEBUG - All pipeline environment variables:'
echo sh(returnStdout: true, script: 'env')
}
// Load Common Code as Global Variable
commonPipeline = load "${WORKSPACE}/openshift/commonPipeline.groovy"
}
}
}
stage('Build') {
agent any
steps {
script {
loadCommonPipeline()
commonPipeline.runStageBuild()
}
}
post {
success {
echo 'Cleanup App BuildConfigs...'
script {
openshift.withCluster() {
openshift.withProject(TOOLS_PROJECT) {
if(DEBUG_OUTPUT.equalsIgnoreCase('true')) {
echo "DEBUG - Using project: ${openshift.project()}"
} else {
def bcApp = openshift.selector('bc', "${REPO_NAME}-app-${JOB_NAME}")
if(bcApp.exists()) {
echo "Removing BuildConfig ${REPO_NAME}-app-${JOB_NAME}..."
bcApp.delete()
}
}
}
}
}
}
}
}
stage('Deploy - Dev') {
agent any
steps {
script {
loadCommonPipeline()
commonPipeline.runStageDeploy('Dev', DEV_PROJECT, DEV_HOST, PATH_ROOT)
}
}
post {
success {
script {
commonPipeline.createDeploymentStatus(DEV_PROJECT, 'SUCCESS', JOB_NAME, DEV_HOST, PATH_ROOT)
commonPipeline.notifyStageStatus('Deploy - Dev', 'SUCCESS')
}
}
unsuccessful {
script {
commonPipeline.createDeploymentStatus(DEV_PROJECT, 'FAILURE', JOB_NAME, DEV_HOST, PATH_ROOT)
commonPipeline.notifyStageStatus('Deploy - Dev', 'FAILURE')
}
}
}
}
}
post {
changed {
script {
// Comment on Pull Request if build changes to successful
if(currentBuild.currentResult.equalsIgnoreCase('SUCCESS')) {
echo "Posting 'PR Deployed at: https://${DEV_HOST}${PATH_ROOT}' to Pull Request..."
commonPipeline.commentOnPR("PR Deployed at: https://${DEV_HOST}${PATH_ROOT}")
}
}
}
}
}
// --------------------
// Supporting Functions
// --------------------
// Load Common Code as Global Variable
def loadCommonPipeline() {
if (!binding.hasVariable('commonPipeline')) {
commonPipeline = load "${WORKSPACE}/openshift/commonPipeline.groovy"
}
}