Merge "jjb: global: releng-macros: Add timeout wrapper"
[releng.git] / jjb / global / releng-macros.yml
1 # Releng macros
2 #
3 # NOTE: make sure macros are listed in execution ordered.
4 #
5 # 1. parameters/properties
6 # 2. scm
7 # 3. triggers
8 # 4. wrappers
9 # 5. prebuilders (maven only, configured like Builders)
10 # 6. builders (maven, freestyle, matrix, etc..)
11 # 7. postbuilders (maven only, configured like Builders)
12 # 8. publishers/reporters/notifications
13
14 - parameter:
15     name: project-parameter
16     parameters:
17         - string:
18             name: PROJECT
19             default: '{project}'
20             description: "JJB configured PROJECT parameter to identify an opnfv Gerrit project"
21         - string:
22             name: GS_BASE
23             default: artifacts.opnfv.org/$PROJECT
24             description: "URL to Google Storage."
25         - string:
26             name: GS_BASE_PROXY
27             default: build.opnfv.org/artifacts.opnfv.org/$PROJECT
28             description: "URL to Google Storage proxy"
29         - string:
30             name: BRANCH
31             default: '{branch}'
32             description: "JJB configured BRANCH parameter (e.g. master, stable/danube)"
33         - string:
34             name: GERRIT_BRANCH
35             default: '{branch}'
36             description: "JJB configured GERRIT_BRANCH parameter (deprecated)"
37
38 - property:
39     name: logrotate-default
40     properties:
41         - build-discarder:
42             days-to-keep: 60
43             num-to-keep: 200
44             artifact-days-to-keep: 60
45             artifact-num-to-keep: 200
46
47 - scm:
48     name: git-scm
49     scm:
50         - git: &git-scm-defaults
51             credentials-id: '$SSH_CREDENTIAL_ID'
52             url: '$GIT_BASE'
53             branches:
54                 - 'origin/$BRANCH'
55             timeout: 15
56
57 - scm:
58     name: git-scm-gerrit
59     scm:
60         - git:
61             choosing-strategy: 'gerrit'
62             refspec: '$GERRIT_REFSPEC'
63             <<: *git-scm-defaults
64 - scm:
65     name: git-scm-with-submodules
66     scm:
67         - git:
68             credentials-id: '$SSH_CREDENTIAL_ID'
69             url: '$GIT_BASE'
70             refspec: ''
71             branches:
72                 - 'refs/heads/{branch}'
73             skip-tag: true
74             wipe-workspace: true
75             submodule:
76                 recursive: true
77                 timeout: 20
78 - trigger:
79     name: 'daily-trigger-disabled'
80     triggers:
81         - timed: ''
82
83 - trigger:
84     name: 'weekly-trigger-disabled'
85     triggers:
86         - timed: ''
87
88 - trigger:
89     name: gerrit-trigger-patchset-created
90     triggers:
91         - gerrit:
92             server-name: 'gerrit.opnfv.org'
93             trigger-on:
94                 - patchset-created-event:
95                     exclude-drafts: 'false'
96                     exclude-trivial-rebase: 'false'
97                     exclude-no-code-change: 'false'
98                 - draft-published-event
99                 - comment-added-contains-event:
100                     comment-contains-value: 'recheck'
101                 - comment-added-contains-event:
102                     comment-contains-value: 'reverify'
103             projects:
104               - project-compare-type: 'ANT'
105                 project-pattern: '{project}'
106                 branches:
107                   - branch-compare-type: 'ANT'
108                     branch-pattern: '**/{branch}'
109                 file-paths:
110                   - compare-type: 'ANT'
111                     pattern: '{files}'
112             skip-vote:
113                 successful: false
114                 failed: false
115                 unstable: false
116                 notbuilt: false
117
118 - trigger:
119     name: gerrit-trigger-change-merged
120     triggers:
121         - gerrit:
122             server-name: 'gerrit.opnfv.org'
123             trigger-on:
124                 - change-merged-event
125                 - comment-added-contains-event:
126                     comment-contains-value: 'remerge'
127             projects:
128               - project-compare-type: 'ANT'
129                 project-pattern: '{project}'
130                 branches:
131                   - branch-compare-type: 'ANT'
132                     branch-pattern: '**/{branch}'
133                 file-paths:
134                   - compare-type: 'ANT'
135                     pattern: '{files}'
136
137 - trigger:
138     name: 'experimental'
139     triggers:
140         - gerrit:
141             server-name: 'gerrit.opnfv.org'
142             trigger-on:
143                 - comment-added-contains-event:
144                     comment-contains-value: 'check-experimental'
145             projects:
146                 - project-compare-type: 'ANT'
147                   project-pattern: '{project}'
148                   branches:
149                       - branch-compare-type: 'ANT'
150                         branch-pattern: '**/{branch}'
151                   file-paths:
152                       - compare-type: 'ANT'
153                         pattern: '{files}'
154             skip-vote:
155                 successful: true
156                 failed: true
157                 unstable: true
158                 notbuilt: true
159
160 - wrapper:
161     name: ssh-agent-wrapper
162     wrappers:
163         - ssh-agent-credentials:
164             users:
165                 - 'd42411ac011ad6f3dd2e1fa34eaa5d87f910eb2e'
166
167 - wrapper:
168     name: build-timeout
169     wrappers:
170       - timeout:
171           timeout: '{timeout}'
172           timeout-var: 'BUILD_TIMEOUT'
173           fail: true
174
175 - wrapper:
176     name: fix-workspace-permissions
177     wrappers:
178         - pre-scm-buildstep:
179           - shell: |
180                 #!/bin/bash
181                 sudo chown -R $USER:$USER $WORKSPACE || exit 1
182
183 - builder:
184     name: build-html-and-pdf-docs-output
185     builders:
186         - shell: |
187             #!/bin/bash
188             set -o errexit
189             set -o xtrace
190             export PATH=$PATH:/usr/local/bin/
191             git clone ssh://gerrit.opnfv.org:29418/opnfvdocs docs_build/_opnfvdocs
192             GERRIT_COMMENT=gerrit_comment.txt ./docs_build/_opnfvdocs/scripts/docs-build.sh
193
194 - builder:
195     name: upload-under-review-docs-to-opnfv-artifacts
196     builders:
197         - shell: |
198             #!/bin/bash
199             set -o errexit
200             set -o pipefail
201             set -o xtrace
202             export PATH=$PATH:/usr/local/bin/
203
204             [[ $GERRIT_CHANGE_NUMBER =~ .+ ]]
205             [[ -d docs_output ]] || exit 0
206
207             echo
208             echo "###########################"
209             echo "UPLOADING DOCS UNDER REVIEW"
210             echo "###########################"
211             echo
212
213             gs_base="artifacts.opnfv.org/$PROJECT/review"
214             gs_path="$gs_base/$GERRIT_CHANGE_NUMBER"
215             local_path="upload/$GERRIT_CHANGE_NUMBER"
216
217             mkdir -p upload
218             mv docs_output "$local_path"
219             gsutil -m cp -r "$local_path" "gs://$gs_base"
220
221             gsutil -m setmeta \
222                 -h "Content-Type:text/html" \
223                 -h "Cache-Control:private, max-age=0, no-transform" \
224                 "gs://$gs_path"/**.html > /dev/null 2>&1
225
226             echo "Document link(s):" >> gerrit_comment.txt
227             find "$local_path" | grep -e 'index.html$' -e 'pdf$' | \
228                 sed -e "s|^$local_path|    http://$gs_path|" >> gerrit_comment.txt
229
230 - builder:
231     name: upload-generated-docs-to-opnfv-artifacts
232     builders:
233         - shell: |
234             #!/bin/bash
235             set -o errexit
236             set -o pipefail
237             set -o xtrace
238             export PATH=$PATH:/usr/local/bin/
239
240             [[ -d docs_output ]] || exit 0
241
242             echo
243             echo "########################"
244             echo "UPLOADING GENERATED DOCS"
245             echo "########################"
246             echo
247
248             echo "gs_path="$GS_URL/docs""
249             echo "local_path="upload/docs""
250
251             gs_path="$GS_URL/docs"
252             local_path="upload/docs"
253
254             mkdir -p upload
255             mv docs_output "$local_path"
256             ls "$local_path"
257
258             echo "gsutil -m cp -r "$local_path"/* "gs://$gs_path""
259             gsutil -m cp -r "$local_path"/* "gs://$gs_path"
260
261             gsutil -m setmeta \
262                 -h "Content-Type:text/html" \
263                 -h "Cache-Control:private, max-age=0, no-transform" \
264                 "gs://$gs_path"/**.html > /dev/null 2>&1
265
266             echo "Document link(s):" >> gerrit_comment.txt
267             find "$local_path" | grep -e 'index.html$' -e 'pdf$' | \
268                 sed -e "s|^$local_path|    http://$gs_path|" >> gerrit_comment.txt
269
270 # To take advantage of this macro, have your build write
271 # out the file 'gerrit_comment.txt' with information to post
272 # back to gerrit and include this macro in the list of builders.
273 - builder:
274     name: report-build-result-to-gerrit
275     builders:
276         - shell: |
277             #!/bin/bash
278             set -o errexit
279             set -o pipefail
280             set -o xtrace
281             export PATH=$PATH:/usr/local/bin/
282             if [[ -e gerrit_comment.txt ]] ; then
283                 echo
284                 echo "posting review comment to gerrit..."
285                 echo
286                 cat gerrit_comment.txt
287                 echo
288                 ssh -p 29418 gerrit.opnfv.org \
289                     "gerrit review -p $GERRIT_PROJECT \
290                      -m '$(cat gerrit_comment.txt)' \
291                      $GERRIT_PATCHSET_REVISION \
292                      --notify NONE"
293             fi
294
295 - builder:
296     name: remove-old-docs-from-opnfv-artifacts
297     builders:
298         - shell: |
299             #!/bin/bash
300             set -o errexit
301             set -o pipefail
302             set -o xtrace
303             export PATH=$PATH:/usr/local/bin/
304
305             [[ $GERRIT_CHANGE_NUMBER =~ .+ ]]
306
307             gs_path="artifacts.opnfv.org/$PROJECT/review/$GERRIT_CHANGE_NUMBER"
308
309             if gsutil ls "gs://$gs_path" > /dev/null 2>&1 ; then
310                 echo
311                 echo "Deleting Out-of-dated Documents..."
312                 gsutil -m rm -r "gs://$gs_path"
313             fi
314             gs_path="artifacts.opnfv.org/review/$GERRIT_CHANGE_NUMBER"
315
316             if gsutil ls "gs://$gs_path" > /dev/null 2>&1 ; then
317                 echo
318                 echo "Deleting Out-of-dated Documents..."
319                 gsutil -m rm -r "gs://$gs_path"
320             fi
321
322 - builder:
323     name: build-and-upload-artifacts-json-api
324     builders:
325         - shell: |
326             #!/bin/bash
327             set -o errexit
328             set -o pipefail
329             export PATH=$PATH:/usr/local/bin/
330
331             virtualenv -p python2.7 $WORKSPACE/releng_artifacts
332             source $WORKSPACE/releng_artifacts/bin/activate
333
334             # install python packages
335             pip install google-api-python-client
336
337             # generate and upload index file
338             echo "Generating Artifacts API ..."
339             python $WORKSPACE/utils/opnfv-artifacts.py > index.json
340             gsutil cp index.json gs://artifacts.opnfv.org/index.json
341
342             deactivate
343
344 - builder:
345     name: lint-python-code
346     builders:
347         - shell: |
348             #!/bin/bash
349             set -o errexit
350             set -o pipefail
351             set -o xtrace
352             export PATH=$PATH:/usr/local/bin/
353
354             virtualenv -p python2.7 $WORKSPACE/releng_flake8
355             source $WORKSPACE/releng_flake8/bin/activate
356
357             # install python packages
358             pip install "flake8==2.6.2"
359
360             # generate and upload lint log
361             echo "Running flake8 code on $PROJECT ..."
362
363             # Get number of flake8 violations. If none, this will be an
364             # empty string: ""
365             FLAKE_COUNT="$(find . \
366                 -path './releng_flake8' -prune -o \
367                 -path './.tox' -prune -o \
368                 -type f -name "*.py" -print | \
369                 xargs flake8 --exit-zero -qq --count 2>&1)"
370
371             # Ensure we start with a clean environment
372             rm -f lint.log
373
374             if [ ! -z $FLAKE_COUNT ]; then
375               echo "Flake8 Violations: $FLAKE_COUNT" > lint.log
376               find . \
377                   -path './releng_flake8' -prune -o \
378                   -path './.tox' -prune -o \
379                   -type f -name "*.py" -print | \
380                   xargs flake8 --exit-zero --first >> violation.log
381               SHOWN=$(wc -l violation.log | cut -d' ' -f1)
382               echo -e "First $SHOWN shown\n---" >> lint.log
383               cat violation.log >> lint.log
384               sed -r -i '4,$s/^/ /g' lint.log
385               rm violation.log
386             fi
387
388             deactivate
389
390 - builder:
391     name: report-lint-result-to-gerrit
392     builders:
393         - shell: |
394             #!/bin/bash
395             set -o errexit
396             set -o pipefail
397             set -o xtrace
398             export PATH=$PATH:/usr/local/bin/
399
400             # If no violations were found, no lint log will exist.
401             if [[ -e lint.log ]] ; then
402                 echo -e "\nposting linting report to gerrit...\n"
403
404                 cat lint.log
405                 echo
406
407                 ssh -p 29418 gerrit.opnfv.org \
408                     "gerrit review -p $GERRIT_PROJECT \
409                      -m \"$(cat lint.log)\" \
410                      $GERRIT_PATCHSET_REVISION \
411                      --notify NONE"
412
413                 exit 1
414             fi
415
416 - builder:
417     name: upload-review-docs
418     builders:
419         - build-html-and-pdf-docs-output
420         - upload-under-review-docs-to-opnfv-artifacts
421         - report-build-result-to-gerrit
422
423 - builder:
424     name: upload-merged-docs
425     builders:
426         - build-html-and-pdf-docs-output
427         - upload-generated-docs-to-opnfv-artifacts
428         - report-build-result-to-gerrit
429         - remove-old-docs-from-opnfv-artifacts
430
431 - builder:
432     name: check-bash-syntax
433     builders:
434         - shell: "find . -name '*.sh' | xargs bash -n"
435
436 - builder:
437     name: lint-yaml-code
438     builders:
439         - shell: |
440             #!/bin/bash
441             set -o errexit
442             set -o pipefail
443             set -o xtrace
444             export PATH=$PATH:/usr/local/bin/
445
446             # install python packages
447             pip install "yamllint==1.6.0"
448
449             # generate and upload lint log
450             echo "Running yaml code on $PROJECT ..."
451
452             # Ensure we start with a clean environment
453             rm -f yaml-violation.log lint.log
454
455             # Get number of yaml violations. If none, this will be an
456             # empty string: ""
457             find . \
458                 -type f -name "*.yml" -print \
459                 -o -name "*.yaml" -print | \
460                 xargs yamllint > yaml-violation.log || true
461
462             if [ -s "yaml-violation.log" ]; then
463               SHOWN=$(cat yaml-violation.log| grep -v "^$" |wc -l)
464               echo -e "First $SHOWN shown\n---" > lint.log
465               cat yaml-violation.log >> lint.log
466               sed -r -i '4,$s/^/ /g' lint.log
467             fi
468
469 - builder:
470     name: clean-workspace-log
471     builders:
472         - shell: |
473             find $WORKSPACE -type f -name '*.log' | xargs rm -f
474
475 - publisher:
476     name: archive-artifacts
477     publishers:
478         - archive:
479             artifacts: '{artifacts}'
480             allow-empty: true
481             fingerprint: true
482             latest-only: true
483
484 - publisher:
485     name: publish-coverage
486     publishers:
487       - cobertura:
488           report-file: "coverage.xml"
489           only-stable: "true"
490           health-auto-update: "false"
491           stability-auto-update: "false"
492           zoom-coverage-chart: "true"
493           targets:
494             - files:
495                 healthy: 10
496                 unhealthy: 20
497                 failing: 30
498             - method:
499                 healthy: 50
500                 unhealthy: 40
501                 failing: 30
502
503 # The majority of the email-ext plugin options are set to the default
504 # for this macro so they can be managed through Jenkins' global
505 # settings.
506 - publisher:
507     name: email-jenkins-admins-on-failure
508     publishers:
509       - email-ext:
510           content-type: text
511           attach-build-log: true
512           compress-log: true
513           always: false
514           failure: true
515           send-to:
516             - recipients