forked from cfpb/jenkins-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkins-automation.groovy
237 lines (197 loc) · 4.71 KB
/
jenkins-automation.groovy
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// ** constants **
// ***************
def CREATE_AND_ACTIVATE_VIRTUAL_ENV = """
if [ -d ".env" ]; then
echo "**> virtualenv exists"
else
echo "**> creating virtualenv"
virtualenv -p /usr/local/bin/python2.7 .env
fi
. .env/bin/activate
"""
// ** templates **
// ***************
job('template-base') {
wrappers {
colorizeOutput()
}
// handle failure
}
job('template-base-js') {
using 'template-base'
wrappers {
nodejs('Node 0.12')
}
}
job('template-build-js') {
using 'template-base-js'
steps {
shell(
'''
cd $DIR_TO_BUILD
./frontendbuild.sh
'''
)
}
publishers {
archiveArtifacts('dist/')
}
}
job('template-test-js') {
using 'template-base-js'
steps {
shell(
'''
cd $DIR_UNDER_TEST
./frontendtest.sh
'''
)
}
}
job('template-django-unit-test') {
using 'template-base'
steps {
shell("""\
$CREATE_AND_ACTIVATE_VIRTUAL_ENV
pip install -r requirements.txt
python manage.py test
""")
}
publishers {
archiveXUnit {
jUnit {
pattern("**/reports/junit.xml")
}
}
allowBrokenBuildClaiming()
cobertura("**/reports/coverage.xml") {
failNoReports(true)
}
}
}
job('template-deploy') {
using 'template-base'
steps {
shell(
'''
# enable python virtualenv that has ansible already installed
source $JENKINS_HOME/ANSIBLE_VENV/bin/activate
# specify $PROJ_NAME and $INVENTORY_FILE in your jenkins.groovy:
# job() {
# environmentVariables(['PROJ_NAME': 'xxx', 'INVENTORY_FILE': 'xxx'])
# }
ansible-playbook -i $PROJ_NAME/ansible/inventories/$INVENTORY_FILE $PROJ_NAME/ansible/playbook.yml --vault-password-file=$PROJ_NAME/ansible/get_vault_password --private-key=/var/lib/jenkins/.ssh/id_rsa_deploy
'''
)
}
wrappers {
injectPasswords()
}
// Add mask passwords, since there's no option for it yet
configure { node ->
node / "buildWrappers" / EnvInjectPasswordWrapper << maskPasswordParameters("true")
}
}
// Requires:
// - CONFIG_FILE: name of the config file to use
// - BEHAVE_TAGS: any tags you want to use
//
// Example: environmentVariables(['CONFIG_FILE': 'environment_dev.cfg', 'BEHAVE_TAGS': '-t=~ignore -t=smoke_testing'])
job('template-browser-test') {
using 'template-base'
wrappers {
injectPasswords()
sauceOnDemandConfig {
enableSauceConnect(true)
webDriverBrowsers("Linuxchrome44")
}
}
// Add mask passwords, since there's no option for it yet
configure { node ->
node / "buildWrappers" / EnvInjectPasswordWrapper << maskPasswordParameters("true")
}
steps {
shell("""
$CREATE_AND_ACTIVATE_VIRTUAL_ENV
cd test/browser_testing
cp features/\${CONFIG_FILE} features/environment.cfg
rm -rf test-results
mkdir test-results
export SELENIUM_TUNNEL=\${sauce_tunnel_name}
export SELENIUM_VIDEO=True
export SELENIUM_CMD_TIMEOUT=600
export SELENIUM_IDLE_TIMEOUT=90
export SELENIUM_VIDEO_UPLOAD_ON_PASS=True
export SELENIUM_NAME=\${JOB_NAME}
export SELENIUM_LIB="2.45.0"
export SELENIUM_RESOLUTION="1024x768"
pip install -r requirements.txt
behave -k -f=plain --logging-level=INFO --junit --junit-directory=test-results \${BEHAVE_TAGS}
""")
}
publishers {
archiveXUnit {
jUnit {
pattern("**/test-results/TESTS-*.xml")
}
}
allowBrokenBuildClaiming()
}
}
// ** build flows **
//
// Requires:
// - FAILURE_EMAILS: a string of email addresses separated by comma,
// notification will be sent to these email addresses when a failure occurs
//
// *****************
buildFlowJob('template-base-build-flow') {
triggers {
scm('H/3 * * * *')
// pullrequest()
}
configure { node ->
node / buildNeedsWorkspace('true')
}
publishers {
aggregateBuildFlowTests()
allowBrokenBuildClaiming()
extendedEmail('$FAILURE_EMAILS') {
trigger('Failure')
configure { node ->
node / replyTo('$DEFAULT_REPLYTO')
}
}
}
}
// ** DSL Project Builder **
// ********************
projectRepos = readFileFromWorkspace("project_repos.txt").split('\n')
job('dsl-project-builder') {
using 'template-base'
description('This job rebuilds all jobs defined in all jenkins.groovy files whenever any project repository changes.')
multiscm {
git {
remote {
url('https://github.com/Ooblioob/jenkins-automation')
}
branch('*/master')
}
projectRepos.each { repo ->
git {
remote {
url(repo)
}
branch('*/master')
relativeTargetDir(repo.split('/')[4])
}
}
}
steps {
dsl {
external(['**/jenkins.groovy'])
additionalClasspath('lib/*.jar')
removeAction('DISABLE')
}
}
}