-
Notifications
You must be signed in to change notification settings - Fork 2
/
Jenkinsfile
185 lines (168 loc) · 5.61 KB
/
Jenkinsfile
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
pipeline {
// Jenkins configuration dependencies
//
// Global Tool Configuration:
// Git
//
// This configuration utilizes the following Jenkins plugins:
//
// * Warnings Next Generation
// * Email Extension Plugin
//
// This configuration also expects the following environment variables
// to be set (typically in /apps/ci/config/env:
//
// JENKINS_EMAIL_SUBJECT_PREFIX
// The Email subject prefix identifying the server.
// Typically "[Jenkins - <HOSTNAME>]" where <HOSTNAME>
// is the name of the server, i.e. "[Jenkins - cidev]"
//
// JENKINS_DEFAULT_EMAIL_RECIPIENTS
// A comma-separated list of email addresses that should
// be the default recipients of Jenkins emails.
agent {
dockerfile {
filename 'Dockerfile.ci'
dir "server"
// Pass JENKINS_EMAIL_SUBJECT_PREFIX and JENKINS_DEFAULT_EMAIL_RECIPIENTS
// into container as "env" arguments, so they are available inside the
// Docker container
args '-u root --env JENKINS_DEFAULT_EMAIL_RECIPIENTS=$JENKINS_DEFAULT_EMAIL_RECIPIENTS --env JENKINS_EMAIL_SUBJECT_PREFIX=$JENKINS_EMAIL_SUBJECT_PREFIX'
}
}
options {
buildDiscarder(
logRotator(
artifactDaysToKeepStr: '',
artifactNumToKeepStr: '',
numToKeepStr: '20'))
}
environment {
DEFAULT_RECIPIENTS = "${ \
sh(returnStdout: true, \
script: 'echo $JENKINS_DEFAULT_EMAIL_RECIPIENTS').trim() \
}"
EMAIL_SUBJECT_PREFIX = "${ \
sh(returnStdout: true, script: 'echo $JENKINS_EMAIL_SUBJECT_PREFIX').trim() \
}"
EMAIL_SUBJECT = "$EMAIL_SUBJECT_PREFIX - " +
'$PROJECT_NAME - ' +
'GIT_BRANCH_PLACEHOLDER - ' +
'$BUILD_STATUS! - ' +
"Build # $BUILD_NUMBER"
EMAIL_CONTENT =
'''$PROJECT_NAME - GIT_BRANCH_PLACEHOLDER - $BUILD_STATUS! - Build # $BUILD_NUMBER:
|
|Check console output at $BUILD_URL to view the results.
|
|There were ${TEST_COUNTS,var="fail"} failed tests.
|
|There are ${ANALYSIS_ISSUES_COUNT} static analysis issues in this build.
|
|There were ${TEST_COUNTS,var="skip"} skipped tests.'''.stripMargin()
}
stages {
stage('initialize') {
steps {
script {
// Retrieve the actual Git branch being built for use in email.
//
// For pull requests, the actual Git branch will be in the
// CHANGE_BRANCH environment variable.
//
// For actual branch builds, the CHANGE_BRANCH variable won't exist
// (and an exception will be thrown) but the branch name will be
// part of the PROJECT_NAME variable, so it is not needed.
ACTUAL_GIT_BRANCH = ''
try {
ACTUAL_GIT_BRANCH = CHANGE_BRANCH + ' - '
} catch (groovy.lang.MissingPropertyException mpe) {
// Do nothing. A branch (as opposed to a pull request) is being
// built
}
// Replace the "GIT_BRANCH_PLACEHOLDER" in email variables
EMAIL_SUBJECT = EMAIL_SUBJECT.replaceAll('GIT_BRANCH_PLACEHOLDER - ', ACTUAL_GIT_BRANCH )
EMAIL_CONTENT = EMAIL_CONTENT.replaceAll('GIT_BRANCH_PLACEHOLDER - ', ACTUAL_GIT_BRANCH )
}
}
}
stage('build') {
steps {
sh '''
ruby -v
'''
}
}
stage('test') {
steps {
sh '''
# Switch to the rails application directory
cd server
# Yarn buld
yarn
# Disable Spring, as it should not be needed, and may interfere with tests
export DISABLE_SPRING=true
# Configure MiniTest to use JUnit-style reporter
export MINITEST_REPORTER=JUnitReporter
bundle exec rails db:reset
bundle exec rails test:system test
'''
}
post {
always {
junit '**/test/reports/*.xml'
}
}
}
stage('static-analysis') {
steps {
sh '''
cd server
# Run RuboCop
# Send output to standard out for "Record compiler warnings and static analysis results"
# post-build action
#
# Using "|| true" so that build will be considered successful, even if there are Rubocop
# violation.
bundle exec rubocop -D --format clang || true
'''
}
post {
always {
// Collect Rubocop reports
recordIssues(tools: [ruboCop(reportEncoding: 'UTF-8')],
qualityGates: [[threshold: 1, type: 'TOTAL', criticality: 'UNSTABLE']]
)
// Collect coverage reports
publishHTML([
allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: 'server/coverage/rcov',
reportFiles: 'index.html',
reportName: "RCov Report"
])
}
}
}
stage('clean-workspace') {
steps {
// Change permissions of the workspace directory to world-writeable
// so Jenkins can delete it. This is needed, because files may be
// written to the directory from the Docker container as the "root"
// user, which Jenkins would not otherwise be able to clean up.
sh '''
chmod --recursive 777 $WORKSPACE
'''
cleanWs()
}
}
}
post {
always {
emailext to: "$DEFAULT_RECIPIENTS",
subject: "$EMAIL_SUBJECT",
body: "$EMAIL_CONTENT"
}
}
}