Debian Codegraph @

README

raw
1 NAME
2
3 Jenkins::API - A wrapper around the Jenkins API
4
5 VERSION
6
7 version 0.18
8
9 SYNOPSIS
10
11 This is a wrapper around the Jenkins API.
12
13 use Jenkins::API;
14
15 my $jenkins = Jenkins::API->new({
16 base_url => 'http://jenkins:8080',
17 api_key => 'username',
18 api_pass => 'apitoken',
19 });
20 my $status = $jenkins->current_status();
21 my @not_succeeded = grep { $_->{color} ne 'blue' } @{$status->{jobs}};
22 # {
23 # 'color' => 'red',
24 # 'name' => 'Test-Project',
25 # 'url' => 'http://jenkins:8080/job/Test-Project/',
26 # }
27
28 my $success = $jenkins->create_job($project_name, $config_xml);
29 ...
30
31 ATTRIBUTES
32
33 Specify these attributes to the constructor of the Jenkins::API object
34 if necessary.
35
36 base_url
37
38 This is the base url for your Jenkins installation. This is commonly
39 running on port 8080 so it's often something like http://jenkins:8080
40
41 api_key
42
43 This is the username for the basic authentication if you have it turned
44 on.
45
46 If you don't, don't specify it.
47
48 Note that Jenkins returns 403 error codes if authentication is required
49 but hasn't been specified. A common setup is to allow build statuses to
50 be read but triggering builds and making configuration changes to
51 require authentication. Check "response_code" after making a call that
52 fails to see if it is an authentication failure.
53
54 my $success = $jenkins->trigger_build($job_name);
55 unless($success)
56 {
57 if($jenkins->response_code == 403)
58 {
59 print "Auth failure\n";
60 }
61 else
62 {
63 print $jenkins->response_content;
64 }
65 }
66
67 api_pass
68
69 The API token for basic auth. Go to the Jenkins wiki page on
70 authenticating scripted clients
71 <https://wiki.jenkins-ci.org/display/JENKINS/Authenticating+scripted+clients>
72 for information on getting an API token for your user to use for
73 authentication.
74
75 METHODS
76
77 check_jenkins_url
78
79 Checks the url provided to the API has a Jenkins server running on it.
80 It returns the version number of the Jenkins server if it is running.
81
82 $jenkins->check_jenkins_url;
83 # 1.460
84
85 current_status
86
87 Returns the current status of the server as returned by the API. This
88 is a hash containing a fairly comprehensive list of what's going on.
89
90 $jenkins->current_status();
91 # {
92 # 'assignedLabels' => [
93 # {}
94 # ],
95 # 'description' => undef,
96 # 'jobs' => [
97 # {
98 # 'color' => 'blue',
99 # 'name' => 'Jenkins-API',
100 # 'url' => 'http://jenkins:8080/job/Jenkins-API/'
101 # },
102 # 'mode' => 'NORMAL',
103 # 'nodeDescription' => 'the master Jenkins node',
104 # 'nodeName' => '',
105 # 'numExecutors' => 2,
106 # 'overallLoad' => {},
107 # 'primaryView' => {
108 # 'name' => 'All',
109 # 'url' => 'http://jenkins:8080/'
110 # },
111 # 'quietingDown' => bless( do{\(my $o = 0)}, 'JSON::XS::Boolean' ),
112 # 'slaveAgentPort' => 0,
113 # 'useCrumbs' => $VAR1->{'quietingDown'},
114 # 'useSecurity' => $VAR1->{'quietingDown'},
115 # 'views' => [
116 # {
117 # 'name' => 'All',
118 # 'url' => 'http://jenkins:8080/'
119 # }
120 # ]
121 # }
122
123 It is also possible to pass two parameters to the query to refine or
124 expand the data you get back. The tree parameter allows you to select
125 specific elements. The example from the Jenkins documentation , tree=>
126 'jobs[name],views[name,jobs[name]]' demonstrates the syntax nicely.
127
128 The other parameter you can pass is depth, by default it's 0, if you
129 set it higher it dumps a ton of data.
130
131 $jenkins->current_status({ extra_params => { tree => 'jobs[name,color]' }});;
132 # {
133 # 'jobs' => [
134 # {
135 # 'color' => 'blue',
136 # 'name' => 'Jenkins-API',
137 # },
138 # ]
139 # }
140
141 $jenkins->current_status({ extra_params => { depth => 1 }});
142 # returns everything and the kitchen sink.
143
144 It is also possible to only look at a subset of the data. Most urls you
145 can see on the website in Jenkins can be accessed. If you have a job
146 named Test-Project for example with the url /job/Test-Project you can
147 specify the path_parts => ['job', 'Test-Project'] to look at the data
148 for that job alone.
149
150 $jenkins->current_status({
151 path_parts => [qw/job Test-Project/],
152 extra_params => { depth => 1 },
153 });
154 # just returns the data relating to job Test-Project.
155 # returning it in detail.
156
157 The method will die saying 'Invalid response' if the server doesn't
158 respond as it expects, or die with a JSON decoding error if the JSON
159 parsing fails.
160
161 get_job_details
162
163 Returns detail about the job specified.
164
165 $job_details = $jenkins->get_job_details('Test-Project');
166 # {
167 # 'actions' => [],
168 # 'buildable' => bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' ),
169 # 'builds' => [],
170 # 'color' => 'disabled',
171 # 'concurrentBuild' => $VAR1->{'buildable'},
172 # 'description' => '',
173 # 'displayName' => 'Test-Project',
174 # 'displayNameOrNull' => undef,
175 # 'downstreamProjects' => [],
176 # 'firstBuild' => undef,
177 # 'healthReport' => [],
178 # 'inQueue' => $VAR1->{'buildable'},
179 # 'keepDependencies' => $VAR1->{'buildable'},
180 # 'lastBuild' => undef,
181 # 'lastCompletedBuild' => undef,
182 # 'lastFailedBuild' => undef,
183 # 'lastStableBuild' => undef,
184 # 'lastSuccessfulBuild' => undef,
185 # 'lastUnstableBuild' => undef,
186 # 'lastUnsuccessfulBuild' => undef,
187 # 'name' => 'Test-Project',
188 # 'nextBuildNumber' => 1,
189 # 'property' => [],
190 # 'queueItem' => undef,
191 # 'scm' => {},
192 # 'upstreamProjects' => [],
193 # 'url' => 'http://jenkins-t2:8080/job/Test-Project/'
194 # }
195
196 The information can be refined in the same way as "current_status"
197 using extra_params.
198
199 view_status
200
201 Provides the status of the specified view. The list of views is
202 provided in the general status report.
203
204 $jenkins->view_status('MyView');
205 # {
206 # 'busyExecutors' => {},
207 # 'queueLength' => {},
208 # 'totalExecutors' => {},
209 # 'totalQueueLength' => {}
210 # }
211 # {
212 # 'description' => undef,
213 # 'jobs' => [
214 # {
215 # 'color' => 'blue',
216 # 'name' => 'Test',
217 # 'url' => 'http://jenkins-t2:8080/job/Test/'
218 # }
219 # ],
220 # 'name' => 'Test',
221 # 'property' => [],
222 # 'url' => 'http://jenkins-t2:8080/view/Test/'
223 # }
224
225 This method allows the same sort of refinement as the "current_status"
226 method. To just get the job info from the view for example you can do
227 essentially the same,
228
229 use Data::Dumper;
230 my $view_list = $api->current_status({ extra_params => { tree => 'views[name]' }});
231 my @views = grep { $_ ne 'All' } map { $_->{name} } @{$view_list->{views}};
232 for my $view (@views)
233 {
234 my $view_jobs = $api->view_status($view, { extra_params => { tree => 'jobs[name,color]' }});
235 print Dumper($view_jobs);
236 }
237 # {
238 # 'jobs' => [
239 # {
240 # 'color' => 'blue',
241 # 'name' => 'Test'
242 # }
243 # ]
244 # }
245
246 trigger_build
247
248 Trigger a build,
249
250 $success = $jenkins->trigger_build('Test-Project');
251
252 If you need to specify a token you can pass that like this,
253
254 $jenkins->trigger_build('Test-Project', { token => $token });
255
256 Note that the success response is simply to indicate that the build has
257 been scheduled, not that the build has succeeded.
258
259 trigger_build_with_parameters
260
261 Trigger a build with parameters,
262
263 $success = $jenkins->trigger_build_with_parameters('Test-Project', { Parameter => 'Value' } );
264
265 The method behaves the same way as trigger_build.
266
267 build_queue
268
269 This returns the items in the build queue.
270
271 $jenkins->build_queue();
272
273 This allows the same extra_params as the "current_status" call. The
274 depth and tree parameters work in the same way. See the Jenkins API
275 documentation for more details.
276
277 The method will die saying 'Invalid response' if the server doesn't
278 respond as it expects, or die with a JSON decoding error if the JSON
279 parsing fails.
280
281 load_statistics
282
283 This returns the load statistics for the server.
284
285 $jenkins->load_statistics();
286 # {
287 # 'busyExecutors' => {},
288 # 'queueLength' => {},
289 # 'totalExecutors' => {},
290 # 'totalQueueLength' => {}
291 # }
292
293 This also allows the same extra_params as the "current_status" call.
294 The depth and tree parameters work in the same way. See the Jenkins API
295 documentation for more details.
296
297 The method will die saying 'Invalid response' if the server doesn't
298 respond as it expects, or die with a JSON decoding error if the JSON
299 parsing fails.
300
301 create_job
302
303 Takes the project name and the XML for a config file and gets Jenkins
304 to create the job.
305
306 my $success = $api->create_job($project_name, $config_xml);
307
308 project_config
309
310 This method returns the configuration for the project in XML.
311
312 my $config = $api->project_config($project_name);
313
314 set_project_config
315
316 This method allows you to set the configuration for the project using
317 XML.
318
319 my $success = $api->set_project_config($project_name, $config);
320
321 delete_project
322
323 Delete the project from Jenkins.
324
325 my $success = $api->delete_project($project_name);
326
327 general_call
328
329 This is a catch all method for making a call to the API. Jenkins is
330 extensible with plugins which can add new API end points. We can not
331 predict all of these so this method allows you to call those functions
332 without needing a specific method.
333
334 general_call($url_parts, $args);
335
336 my $response = $api->general_call(
337 ['job', $job, 'api', 'json'],
338 {
339 method => 'GET',
340 extra_params => { tree => 'color,description' },
341 decode_json => 1,
342 expected_response_code => 200,
343 });
344
345 # does a GET /job/$job/api/json?tree=color%2Cdescription
346 # decodes the response as json
347 # dies if a 200 response isn't returned.
348
349 The arguments hash can contain these elements,
350
351 * method
352
353 Valid options are the HTTP verbs, make sure they are in caps.
354
355 * extra_params
356
357 Pass in extra parameters the method expects.
358
359 * decode_json
360
361 Defaulted to true.
362
363 * expected_response_code
364
365 Defaulted to 200
366
367 response_code
368
369 This method returns the HTTP response code from our last request to the
370 Jenkins server. This may be useful when an error occurred.
371
372 response_content
373
374 This method returns the content of the HTTP response from our last
375 request to the Jenkins server. This may be useful when an error occurs.
376
377 response_header
378
379 This method returns the specified header of the HTTP response from our
380 last request to the Jenkins server.
381
382 The following example triggers a parameterized build, extracts the
383 'Location' HTTP response header, and selects certain elements of the
384 queue item information
385
386 $success = $jenkins->trigger_build_with_parameters('Test-Project', { Parameter => 'Value' } );
387 if ($success) {
388 my $location = $jenkins->response_header('Location');
389 my $queue_item = $jenkins->general_call(
390 [ URI->new($location)->path_segments, 'api', 'json' ],
391 {
392 extra_params => { tree => 'url,why,executable[url]' }
393 }
394 );
395 # {
396 # 'executable' => {
397 # 'url' => 'http://jenkins:8080/job/Test-Project/136/',
398 # '_class' => 'org.jenkinsci.plugins.workflow.job.WorkflowRun'
399 # },
400 # 'url' => 'queue/item/555125/',
401 # 'why' => undef,
402 # '_class' => 'hudson.model.Queue$LeftItem'
403 # };
404 } else {
405 print $jenkins->response_code;
406 }
407
408 BUGS
409
410 The API wrapper doesn't deal with Jenkins installations not running
411 from the root path. I don't actually know if that's an install option,
412 but the internal url building just doesn't deal with that situation
413 properly. If you want that fixing a patch is welcome.
414
415 Please report any bugs or feature requests to through the web interface
416 at https://github.com/colinnewell/Jenkins-API/issues/new. I will be
417 notified, and then you'll automatically be notified of progress on your
418 bug as I make changes.
419
420 SUPPORT
421
422 You can find documentation for this module with the perldoc command.
423
424 perldoc Jenkins::API
425
426 You can also look for information at:
427
428 * github issue list
429
430 https://github.com/colinnewell/Jenkins-API/issues
431
432 * AnnoCPAN: Annotated CPAN documentation
433
434 http://annocpan.org/dist/Jenkins-API
435
436 * CPAN Ratings
437
438 http://cpanratings.perl.org/d/Jenkins-API
439
440 * Search CPAN
441
442 http://search.cpan.org/dist/Jenkins-API/
443
444 SEE ALSO
445
446 * Jenkins CI server
447
448 http://jenkins-ci.org/
449
450 * Net::Jenkins
451
452 An alternative to this library.
453
454 https://metacpan.org/module/Net::Jenkins
455
456 * Task::Jenkins
457
458 Libraries to help testing modules on a Jenkins server.
459
460 https://metacpan.org/module/Task::Jenkins
461
462 ACKNOWLEDGEMENTS
463
464 Birmingham Perl Mongers for feedback before I released this to CPAN.
465
466 With thanks to Nick Hu for adding the trigger_build_with_parameters
467 method.
468
469 Alex Kulbiy for the auth support and David Steinbrunner for some
470 Makefile love.
471
472 CONTRIBUTORS
473
474 * Nick Hu
475
476 * David Steinbrunner
477
478 * Alex Kulbiy
479
480 * Piers Cawley
481
482 * Arthur Axel 'fREW' Schmidt
483
484 * Dave Horner https://dave.thehorners.com
485
486 * Sven Willenbuecher
487
488 AUTHOR
489
490 Colin Newell <[email protected]>
491
492 COPYRIGHT AND LICENSE
493
494 This software is copyright (c) 2012-2021 by Colin Newell.
495
496 This is free software; you can redistribute it and/or modify it under
497 the same terms as the Perl 5 programming language system itself.
498