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