71511abed5bc597b6177c4646ea466c723f9dafc
[octopus.git] / docs / opnfv-jjb-usage.rst
1 How to write and use JJB?
2 ============================================
3
4 What is Jenkins Job Builder
5 ----------------------------
6
7 Jenkins Job Builder(JJB) takes simple descriptions of Jenkins jobs in YAML format, and uses them to configure Jenkins jobs. JJB keeps your job descriptions in human readable format and job template system simplifies the configuration of Jenkins jobs. Upstream documentation is available at http://ci.openstack.org/jenkins-job-builder/.
8
9 How to Write/Use Jenkins Job Builder
10 ------------------------------------
11
12 Job template is widely used in JJBs and makes the configuration of Jenkins jobs simple, if you need to define several jobs which are nearly identical, except perhaps in their names, SCP targets, etc., then you may use a job template to specify the particulars of the job, and then use a project to realize the job with appropriate variable substitution.
13
14 To illustrate how to configure Jenkins jobs by using job template, we can start with a simple example used in our OPNFV releng project octopus directory, which is just used to print "Hello world from Octopus", shown as below::
15
16  -job-template:
17     name: octopus-test
18
19     node: master
20
21     project-type: freestyle
22
23     logrotate:
24         daysToKeep: 30
25         numToKeep: 10
26         artifactDaysToKeep: -1
27         artifactNumToKeep: -1
28
29     builders:
30         - shell: |
31             echo "Hello world from octopus"
32
33 the value "-1" here means keep forever, you can add this job template into the project jobs to run in jenkins::
34
35  - project:
36      name: octopus
37      jobs:
38          - 'octopus-test'
39
40 then this job works!
41
42 Further, if you are a developer who wants to set up a job template to be used in jenkins, we should dive into much more, taking one job template in releng project used for octopus project, the 'octopus-daily-{stream}', as an example::
43
44  -job-template:
45     name: 'octopus-daily-{stream}'
46
47     node: master
48
49     # Job template for daily builders
50     #
51     # Required Variables:
52     #     stream:    branch with - in place of / (eg. stable)
53     #     branch:    branch (eg. stable)
54
55     project-type: freestyle
56     varsetabove: '{somevar}'
57
58     logrotate:
59         daysToKeep: '{build-days-to-keep}'
60         numToKeep: '{build-num-to-keep}'
61         artifactDaysToKeep: '{build-artifact-days-to-keep}'
62         artifactNumToKeep: '{build-artifact-num-to-keep}'
63
64     parameters:
65         - project-parameter:
66             project: '{project}'
67
68     scm:
69         - git-scm:
70             credentials-id: '{ssh-credentials}'
71             refspec: ''
72             branch: '{branch}'
73
74     wrappers:
75         - ssh-agent-credentials:
76             user: '{ssh-credentials}'
77
78     triggers:
79         - timed: 'H H * * *'
80
81     prebuilders:
82         - test-macro
83
84     builders:
85         - shell:
86             !include-raw build-upload-docu.sh
87
88     postbulders:
89         - test-macro
90
91 the {stream} here means when you add this into jobs, you can replace {stream} with what you want, such as::
92
93  stream:
94         - master:
95             branch: 'master'
96
97 the::
98
99  node: master
100
101 means to restrict this job to run in Jenkins master node. Next, several important procedures are illustrated here,
102
103 - scm, this mudule allows you to specify the source code location for the project, and it allows referencing multiple repositories in a Jenkins job.
104 - triggers, this module defines what causes a Jenkins job to start building.
105 - prebuilders and postbuilders, which define job need done pre and post the builders.
106 - builders, which defines actions that the Jenkins job should execute, usually the shell scripts or maven targets are existed there, e.g., build-upload-docu.sh used in our example.
107
108 Generally, the modules used in a job template is sequenced as
109
110 1. parameters, properties
111 2. scm
112 3. triggers
113 4. wrappers
114 5. prebuilders
115 6. builders
116 7. postbuilders
117 8. publishers, reporters, notifications
118
119 Working with OPNFV Jenkins Jobs
120 -------------------------------
121
122 By now, the releng project of OPNFV is the release engineering project for JJBs, you can clone the repo::
123
124  git clone ssh://YOU@gerrit.opnfv.org:29418/releng
125
126 make changes::
127
128  git commit -sv
129  git review
130  remote: Resolving deltas: 100% (3/3)
131  remote: Processing changes: new: 1, refs: 1, done
132  remote:
133  remote: New Changes:
134  remote:   https://gerrit.opnfv.org/gerrit/51
135  remote:
136  To ssh://agardner@gerrit.opnfv.org:29418/releng.git
137   "* [new branch]      HEAD -> refs/publish/master
138
139 Follow the link to gerrit https://gerrit.opnfv.org/gerrit/51 in a few moments the verify job will have completed and you will see Verified +1 jenkins-ci in the gerrit ui.
140
141 If the changes pass the verify job https://build.opnfv.org/ci/view/builder/job/builder-verify-jjb/ The patch can be submitted by a committer.
142
143 The verify and merge jobs are retriggerable in Gerrit by simply leaving a comment with one of the keywords listed below. This is useful in case you need to re-run one of those jobs in case if build issues or something changed with the environment.
144
145 * Verify Job: Trigger: **recheck** or **reverify**
146
147 * Merge Job: Trigger: **remerge**
148
149 You can add below persons as reviewers to your patch in order to get it reviewed and submitted.
150
151 * Ulrich Kleber (Ulrich.Kleber@huawei.com)
152 * Fatih Degirmenci (fatih.degirmenci@ericsson.com)
153 * Xinyu Zhao(Jerry) (zhaoxinyu@huawei.com)
154 * Aric Gardner (agardner@linuxfoundation.org)
155
156 The Current merge and verify jobs for jenkins job builder for releng project, shown in https://git.opnfv.org/cgit/releng/tree/jjb.
157
158 Assuming that you have set up some job templates and put them into a project, then the question is that how they work? Taking the jobs 'builder-verify-jjb', 'builder-merge' used in releng project as examples, 'builder-verify-jjb' is to verify jobs you commited, you will see verified '+1' jenkins-ci in gerrit if it succeed, 'builder-merge' is to set up a merge job and update all the JJBs. If you have some new jobs need to be run, you can set up your own job templates and add them into the project.
159
160 **Documentation tracking**
161
162 Revision: _sha1_
163
164 Build date:  _date_