Sudo pip install.
[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: upload-under-review-docs-to-opnfv-artifacts
200     builders:
201       - shell: |
202           #!/bin/bash
203           set -o errexit
204           set -o pipefail
205           set -o xtrace
206           export PATH=$PATH:/usr/local/bin/
207
208           [[ $GERRIT_CHANGE_NUMBER =~ .+ ]]
209           [[ -d docs/_build/ ]] || exit 0
210
211           echo
212           echo "###########################"
213           echo "UPLOADING DOCS UNDER REVIEW"
214           echo "###########################"
215           echo
216
217           gs_base="artifacts.opnfv.org/$PROJECT/review"
218           gs_path="$gs_base/$GERRIT_CHANGE_NUMBER"
219           local_path="upload/$GERRIT_CHANGE_NUMBER"
220
221           mkdir -p upload
222           mv docs/_build/html/ "$local_path"
223           gsutil -m cp -r "$local_path" "gs://$gs_base"
224
225           gsutil -m setmeta \
226               -h "Content-Type:text/html" \
227               -h "Cache-Control:private, max-age=0, no-transform" \
228               "gs://$gs_path"/**.html > /dev/null 2>&1
229
230           echo "Document link(s):" >> gerrit_comment.txt
231           find "$local_path" | grep -e 'index.html$' -e 'pdf$' | \
232               sed -e "s|^$local_path|    http://$gs_path|" >> gerrit_comment.txt
233
234 # To take advantage of this macro, have your build write
235 # out the file 'gerrit_comment.txt' with information to post
236 # back to gerrit and include this macro in the list of builders.
237 - builder:
238     name: report-build-result-to-gerrit
239     builders:
240       - shell: |
241           #!/bin/bash
242           set -o errexit
243           set -o pipefail
244           set -o xtrace
245           export PATH=$PATH:/usr/local/bin/
246           if [[ -e gerrit_comment.txt ]] ; then
247               echo
248               echo "posting review comment to gerrit..."
249               echo
250               cat gerrit_comment.txt
251               echo
252               ssh -p 29418 gerrit.opnfv.org \
253                   "gerrit review -p $GERRIT_PROJECT \
254                    -m '$(cat gerrit_comment.txt)' \
255                    $GERRIT_PATCHSET_REVISION \
256                    --notify NONE"
257           fi
258
259 - builder:
260     name: remove-old-docs-from-opnfv-artifacts
261     builders:
262       - shell: |
263           #!/bin/bash
264           set -o errexit
265           set -o pipefail
266           set -o xtrace
267           export PATH=$PATH:/usr/local/bin/
268
269           [[ $GERRIT_CHANGE_NUMBER =~ .+ ]]
270
271           gs_path="artifacts.opnfv.org/$PROJECT/review/$GERRIT_CHANGE_NUMBER"
272
273           if gsutil ls "gs://$gs_path" > /dev/null 2>&1 ; then
274               echo
275               echo "Deleting Out-of-dated Documents..."
276               gsutil -m rm -r "gs://$gs_path"
277           fi
278           gs_path="artifacts.opnfv.org/review/$GERRIT_CHANGE_NUMBER"
279
280           if gsutil ls "gs://$gs_path" > /dev/null 2>&1 ; then
281               echo
282               echo "Deleting Out-of-dated Documents..."
283               gsutil -m rm -r "gs://$gs_path"
284           fi
285
286 - builder:
287     name: build-and-upload-artifacts-json-api
288     builders:
289       - shell: |
290           #!/bin/bash
291           set -o errexit
292           set -o pipefail
293           export PATH=$PATH:/usr/local/bin/
294
295           virtualenv -p python2.7 $WORKSPACE/releng_artifacts
296           source $WORKSPACE/releng_artifacts/bin/activate
297
298           # install python packages
299           pip install google-api-python-client
300
301           # generate and upload index file
302           echo "Generating Artifacts API ..."
303           python $WORKSPACE/utils/opnfv-artifacts.py > index.json
304           gsutil cp index.json gs://artifacts.opnfv.org/index.json
305
306           deactivate
307
308 - builder:
309     name: lint-python-code
310     builders:
311       - shell: |
312           #!/bin/bash
313           set -o errexit
314           set -o pipefail
315           set -o xtrace
316           export PATH=$PATH:/usr/local/bin/
317
318           virtualenv -p python2.7 $WORKSPACE/releng_flake8
319           source $WORKSPACE/releng_flake8/bin/activate
320
321           # install python packages
322           pip install "flake8==2.6.2"
323
324           # generate and upload lint log
325           echo "Running flake8 code on $PROJECT ..."
326
327           # Get number of flake8 violations. If none, this will be an
328           # empty string: ""
329           FLAKE_COUNT="$(find . \
330               -path './releng_flake8' -prune -o \
331               -path './.tox' -prune -o \
332               -type f -name "*.py" -print | \
333               xargs flake8 --exit-zero -qq --count 2>&1)"
334
335           # Ensure we start with a clean environment
336           rm -f lint.log
337
338           if [ ! -z $FLAKE_COUNT ]; then
339             echo "Flake8 Violations: $FLAKE_COUNT" > lint.log
340             find . \
341                 -path './releng_flake8' -prune -o \
342                 -path './.tox' -prune -o \
343                 -type f -name "*.py" -print | \
344                 xargs flake8 --exit-zero --first >> violation.log
345             SHOWN=$(wc -l violation.log | cut -d' ' -f1)
346             echo -e "First $SHOWN shown\n---" >> lint.log
347             cat violation.log >> lint.log
348             sed -r -i '4,$s/^/ /g' lint.log
349             rm violation.log
350           fi
351
352           deactivate
353
354 - builder:
355     name: report-lint-result-to-gerrit
356     builders:
357       - shell: |
358           #!/bin/bash
359           set -o errexit
360           set -o pipefail
361           set -o xtrace
362           export PATH=$PATH:/usr/local/bin/
363
364           # If no violations were found, no lint log will exist.
365           if [[ -e lint.log ]] ; then
366               echo -e "\nposting linting report to gerrit...\n"
367
368               cat lint.log
369               echo
370
371               ssh -p 29418 gerrit.opnfv.org \
372                   "gerrit review -p $GERRIT_PROJECT \
373                    -m \"$(cat lint.log)\" \
374                    $GERRIT_PATCHSET_REVISION \
375                    --notify NONE"
376
377               exit 1
378           fi
379
380 - builder:
381     name: upload-review-docs
382     builders:
383       - upload-under-review-docs-to-opnfv-artifacts
384       - report-build-result-to-gerrit
385
386 - builder:
387     name: check-bash-syntax
388     builders:
389       - shell: "find . -name '*.sh' | xargs bash -n"
390
391 - builder:
392     name: lint-yaml-code
393     builders:
394       - shell: |
395           #!/bin/bash
396           set -o errexit
397           set -o pipefail
398           set -o xtrace
399           export PATH=$PATH:/usr/local/bin/
400
401           # install python packages
402           sudo pip install "yamllint==1.8.2"
403
404           # generate and upload lint log
405           echo "Running yaml code on $PROJECT ..."
406
407           # Get list of yaml files
408           YAML_FILES=$(git --no-pager diff --diff-filter=MCRAT --name-only HEAD^1 | egrep "ya?ml$")
409
410           # Ensure we start with a clean environment
411           rm -f yaml-violation.log lint.log
412
413           # Yamllint files only in patchset
414           for yamlfile in $YAML_FILES; do
415             yamllint $yamlfile >> yaml-violation.log || true
416           done
417
418           if [ -s "yaml-violation.log" ]; then
419             SHOWN=$(grep -c -v "^$" yaml-violation.log)
420             echo -e "First $SHOWN shown\n---" > lint.log
421             cat yaml-violation.log >> lint.log
422             sed -r -i '4,$s/^/ /g' lint.log
423           fi
424
425 - builder:
426     name: clean-workspace
427     builders:
428       - shell: |
429           #!/bin/bash
430           set -o errexit
431           set -o nounset
432           set -o pipefail
433           sudo /bin/rm -rf "$WORKSPACE"
434
435 - builder:
436     name: clean-workspace-log
437     builders:
438       - shell: |
439           find $WORKSPACE -type f -name '*.log' | xargs rm -f
440
441 - publisher:
442     name: archive-artifacts
443     publishers:
444       - archive:
445           artifacts: '{artifacts}'
446           allow-empty: true
447           fingerprint: true
448           latest-only: true
449
450 - publisher:
451     name: publish-coverage
452     publishers:
453       - cobertura:
454           report-file: "coverage.xml"
455           only-stable: "true"
456           health-auto-update: "false"
457           stability-auto-update: "false"
458           zoom-coverage-chart: "true"
459           targets:
460             - files:
461                 healthy: 10
462                 unhealthy: 20
463                 failing: 30
464             - method:
465                 healthy: 50
466                 unhealthy: 40
467                 failing: 30
468
469 # The majority of the email-ext plugin options are set to the default
470 # for this macro so they can be managed through Jenkins' global
471 # settings.
472 - publisher:
473     name: email-jenkins-admins-on-failure
474     publishers:
475       - email-ext:
476           content-type: text
477           attach-build-log: true
478           compress-log: true
479           always: false
480           failure: true
481           send-to:
482             - recipients