Rework the examples

Create additional examples to help users get started.

Change-Id: I609dd06ee33033ae5efc18fa39ce920721998412
This commit is contained in:
Khai Do 2015-07-30 12:37:25 -07:00
parent c83e9b1dc1
commit f0989dd2ba
3 changed files with 128 additions and 47 deletions

View File

@ -1,30 +0,0 @@
Example usage
=============
Example usage::
import jenkins
j = jenkins.Jenkins('http://your_url_here', 'username', 'password')
j.get_jobs()
j.create_job('empty', jenkins.EMPTY_CONFIG_XML)
j.disable_job('empty')
j.copy_job('empty', 'empty_copy')
j.enable_job('empty_copy')
j.reconfig_job('empty_copy', jenkins.RECONFIG_XML)
j.delete_job('empty')
j.delete_job('empty_copy')
j.get_views()
j.create_view('EMPTY', jenkins.EMPTY_VIEW_CONFIG_XML)
j.view_exists('EMPTY')
j.delete_view('EMPTY')
# build a parameterized job
# requires setting up api-test job to accept 'param1' & 'param2'
j.build_job('api-test', {'param1': 'test value 1', 'param2': 'test value 2'})
last_build_number = j.get_job_info('api-test')['lastCompletedBuild']['number']
build_info = j.get_job_info('api-test', last_build_number)
print(build_info)
Look at the :doc:`api` for more details.

118
doc/source/examples.rst Normal file
View File

@ -0,0 +1,118 @@
Using Python-Jenkins
====================
The python-jenkins library allows management of a Jenkins server through
the Jenkins REST endpoints. Below are examples to get you started using
the library. If you need further help take a look at the :doc:`api`
docs for more details.
Example 1: Get version of Jenkins
---------------------------------
This is an example showing how to connect to a Jenkins instance and
retrieve the Jenkins server version.
::
import jenkins
server = jenkins.Jenkins('localhost:8080', username='myuser', password='mypassword')
version = server.get_version()
print version
The above code prints the version of the Jenkins master running on 'localhost:8080'
From Jenkins vesion 1.426 onward you can specify an API token instead of your
real password while authenticating the user against the Jenkins instance.
Refer to the `Jenkins Authentication`_ wiki for details about how you
can generate an API token. Once you have an API token you can pass the API token
instead of a real password while creating a Jenkins instance.
.. _Jenkins Authentication: https://wiki.jenkins-ci.org/display/JENKINS/Authenticating+scripted+clients
Example 2: Working with Jenkins Jobs
------------------------------------
This is an example showing how to create, configure and delete Jenkins jobs.
::
server.create_job('empty', jenkins.EMPTY_CONFIG_XML)
jobs = server.get_jobs()
print jobs
server.build_job('empty')
server.disable_job('empty')
server.copy_job('empty', 'empty_copy')
server.enable_job('empty_copy')
server.reconfig_job('empty_copy', jenkins.RECONFIG_XML)
server.delete_job('empty')
server.delete_job('empty_copy')
# build a parameterized job
# requires creating and configuring the api-test job to accept 'param1' & 'param2'
server.build_job('api-test', {'param1': 'test value 1', 'param2': 'test value 2'})
last_build_number = server.get_job_info('api-test')['lastCompletedBuild']['number']
build_info = server.get_job_info('api-test', last_build_number)
print build_info
Example 3: Working with Jenkins Views
-------------------------------------
This is an example showing how to create, configure and delete Jenkins views.
::
server.create_view('EMPTY', jenkins.EMPTY_VIEW_CONFIG_XML)
view_config = server.get_view_config('EMPTY')
views = server.get_views()
server.delete_view('EMPTY')
print views
Example 4: Working with Jenkins Plugins
---------------------------------------
This is an example showing how to retrieve Jenkins plugins information.
::
plugins = server.get_plugins_info()
print plugins
The above example will print a dictionary containing all the plugins that
are installed on the Jenkins server. An example of what you can expect
from the :func:`get_plugins_info` method is documented in the :doc:`api`
doc.
Example 5: Working with Jenkins Nodes
-------------------------------------
This is an example showing how to add, configure, enable and delete Jenkins nodes.
::
server.create_node('slave1')
nodes = get_nodes()
print nodes
node_config = server.get_node_info('slave1')
print node_config
server.disable_node('slave1')
server.enable_node('slave1')
Example 6: Working with Jenkins Build Queue
-------------------------------------------
This is an example showing how to retrieve information on the Jenkins queue.
::
server.build_job('foo')
queue_info = server.get_queue_info()
id = queue_info[0].get('id')
server.cancel_queue(id)

View File

@ -42,7 +42,7 @@
:platform: Unix, Windows
:synopsis: Python API to interact with Jenkins
See examples at :doc:`example`
See examples at :doc:`examples`
'''
import base64
@ -346,11 +346,10 @@ class Jenkins(object):
Example::
>>> j = Jenkins()
>>> next_build_number = j.get_job_info('build_name')['nextBuildNumber']
>>> output = j.build_job('build_name')
>>> next_build_number = server.get_job_info('build_name')['nextBuildNumber']
>>> output = server.build_job('build_name')
>>> from time import sleep; sleep(10)
>>> build_info = j.get_build_info('build_name', next_build_number)
>>> build_info = server.get_build_info('build_name', next_build_number)
>>> print(build_info)
{u'building': False, u'changeSet': {u'items': [{u'date': u'2011-12-19T18:01:52.540557Z', u'msg': u'test', u'revision': 66, u'user': u'unknown', u'paths': [{u'editType': u'edit', u'file': u'/branches/demo/index.html'}]}], u'kind': u'svn', u'revisions': [{u'module': u'http://eaas-svn01.i3.level3.com/eaas', u'revision': 66}]}, u'builtOn': u'', u'description': None, u'artifacts': [{u'relativePath': u'dist/eaas-87-2011-12-19_18-01-57.war', u'displayPath': u'eaas-87-2011-12-19_18-01-57.war', u'fileName': u'eaas-87-2011-12-19_18-01-57.war'}, {u'relativePath': u'dist/eaas-87-2011-12-19_18-01-57.war.zip', u'displayPath': u'eaas-87-2011-12-19_18-01-57.war.zip', u'fileName': u'eaas-87-2011-12-19_18-01-57.war.zip'}], u'timestamp': 1324317717000, u'number': 87, u'actions': [{u'parameters': [{u'name': u'SERVICE_NAME', u'value': u'eaas'}, {u'name': u'PROJECT_NAME', u'value': u'demo'}]}, {u'causes': [{u'userName': u'anonymous', u'shortDescription': u'Started by user anonymous'}]}, {}, {}, {}], u'id': u'2011-12-19_18-01-57', u'keepLog': False, u'url': u'http://eaas-jenkins01.i3.level3.com:9080/job/build_war/87/', u'culprits': [{u'absoluteUrl': u'http://eaas-jenkins01.i3.level3.com:9080/user/unknown', u'fullName': u'unknown'}], u'result': u'SUCCESS', u'duration': 8826, u'fullDisplayName': u'build_war #87'}
'''
@ -375,8 +374,7 @@ class Jenkins(object):
''':returns: list of job dictionaries, ``[dict]``
Example::
>>> j = Jenkins()
>>> queue_info = j.get_queue_info()
>>> queue_info = server.get_queue_info()
>>> print(queue_info[0])
{u'task': {u'url': u'http://your_url/job/my_job/', u'color': u'aborted_anime', u'name': u'my_job'}, u'stuck': False, u'actions': [{u'causes': [{u'shortDescription': u'Started by timer'}]}], u'buildable': False, u'params': u'', u'buildableStartMilliseconds': 1315087293316, u'why': u'Build #2,532 is already in progress (ETA:10 min)', u'blocked': True}
'''
@ -409,8 +407,7 @@ class Jenkins(object):
Example::
>>> j = Jenkins()
>>> info = j.get_info()
>>> info = server.get_info()
>>> jobs = info['jobs']
>>> print(jobs[0])
{u'url': u'http://your_url_here/job/my_job/', u'color': u'blue',
@ -434,8 +431,7 @@ class Jenkins(object):
Example::
>>> j = Jenkins()
>>> info = j.get_version()
>>> info = server.get_version()
>>> print info
>>> 1.541
@ -470,8 +466,7 @@ class Jenkins(object):
Example::
>>> j = Jenkins()
>>> info = j.get_plugins_info()
>>> info = server.get_plugins_info()
>>> print(info)
[{u'backupVersion': None, u'version': u'0.0.4', u'deleted': False,
u'supportsDynamicLoad': u'MAYBE', u'hasUpdate': True,
@ -505,8 +500,7 @@ class Jenkins(object):
Example::
>>> j = Jenkins()
>>> info = j.get_plugin_info("Gearman Plugin")
>>> info = server.get_plugin_info("Gearman Plugin")
>>> print(info)
{u'backupVersion': None, u'version': u'0.0.4', u'deleted': False,
u'supportsDynamicLoad': u'MAYBE', u'hasUpdate': True,
@ -693,8 +687,7 @@ class Jenkins(object):
:returns: The result of the script run.
Example::
>>> j = Jenkins()
>>> info = j.run_script("println(Jenkins.instance.pluginManager.plugins)")
>>> info = server.run_script("println(Jenkins.instance.pluginManager.plugins)")
>>> print(info)
u'[Plugin:windows-slaves, Plugin:ssh-slaves, Plugin:translation,
Plugin:cvs, Plugin:nodelabelparameter, Plugin:external-monitor-job,