Thursday 6 October 2016

Parsing of Jenkins data with Python.

To know about Jenkins see my post:
http://pythoninterface.blogspot.in/2016/10/what-is-jenkins.html

In my present company i am working with Jenkins for automating lot of projects.
For most of the projects i will write the code in Python, Perl and Shell.

Then i just use my company Jenkins page and i will create a new project for it and i will make settings for the project such that Jenkins will run my script on a particular server in time which i mentioned in daily basis and also i set ed the mail triggering also.

Now as Jenkins runs my code daily and stores the content & result and relevant details, i want the data to be called in my other codes for this purpose i use python module Jenkins for handling Jenkins like a remote.

Installation of Jenkins module:

pip install jenkinsapi


Then open python console:

import jenkinsapi
from jenkinsapi.jenkins import Jenkins

Now you can test the functionalities of the module by using dir command:

dir(Jenkins)

You will get output as:

['RETRY_ATTEMPTS', '__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__len__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_clone', '_poll', '_poll_if_needed', 'base_server_url', 'build_job', 'copy_job', 'create_job', 'create_node', 'credentials', 'delete_job', 'delete_node', 'delete_view_by_url', 'get_artifact_data', 'get_create_url', 'get_credentials', 'get_data', 'get_executors', 'get_jenkins_obj', 'get_jenkins_obj_from_url', 'get_job', 'get_jobs', 'get_jobs_info', 'get_jobs_list', 'get_label', 'get_master_data', 'get_node', 'get_node_url', 'get_nodes', 'get_nodes_url', 'get_plugins', 'get_plugins_url', 'get_queue', 'get_queue_url', 'get_view_by_url', 'has_job', 'has_node', 'has_plugin', 'install_plugin', 'install_plugins', 'items', 'iteritems', 'iterkeys', 'jobs', 'keys', 'nodes', 'plugins', 'poll', 'pprint', 'process_job_folder', 'python_api_url', 'rename_job', 'resolve_job_folders', 'safe_restart', 'strip_trailing_slash', 'validate_fingerprint', 'validate_fingerprint_for_build', 'version', 'views']

Now we need to map the jenkinsapi to our Jenkins i.e, the local jenkins server:

j = Jenkins('<local jenkins server path>')

Example:
                   j = Jenkins('http://localhost:8080')

Now to check the version of Jenkins which we use,

J.version

which will give the version details.

To get all Projects present in Jenkins use:

J.keys()

This will give all the project keys and we can use a relevant project key which we want and we can extract the data of it.

For example lets consider output of above command as,

['fooo', 'test_1']

Then if u want the details of "test_1" project use,

j1 = J['test_1']

to check it functionalities use dir of it,

dir(j1)

which will give,

['RETRY_ATTEMPTS', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__len__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_add_missing_builds', '_buildid_for_type', '_config', '_data', '_element_tree', '_get_config_element_tree', '_get_hg_branch', '_mk_json_from_build_parameters', '_poll', '_revmap', '_scm_map', '_scmbranchmap', '_scmurlmap', 'baseurl', 'delete_from_queue', 'disable', 'enable', 'get_build', 'get_build_dict', 'get_build_ids', 'get_build_metadata', 'get_build_triggerurl', 'get_buildnumber_for_revision', 'get_config', 'get_config_xml_url', 'get_data', 'get_delete_url', 'get_description', 'get_downstream_job_names', 'get_downstream_jobs', 'get_first_build', 'get_first_buildnumber', 'get_jenkins_obj', 'get_last_build', 'get_last_build_or_none', 'get_last_buildnumber', 'get_last_completed_build', 'get_last_completed_buildnumber', 'get_last_failed_buildnumber', 'get_last_good_build', 'get_last_good_buildnumber', 'get_last_stable_build', 'get_last_stable_buildnumber', 'get_next_build_number', 'get_params', 'get_params_list', 'get_queue_item', 'get_rename_url', 'get_revision_dict', 'get_scm_branch', 'get_scm_type', 'get_scm_url', 'get_upstream_job_names', 'get_upstream_jobs', 'has_params', 'has_queued_build', 'invoke', 'is_enabled', 'is_queued', 'is_queued_or_running', 'is_running', 'jenkins', 'load_config', 'mk_json_from_build_parameters', 'modify_scm_branch', 'modify_scm_url', 'name', 'poll', 'pprint', 'process_job_folder', 'python_api_url', 'resolve_job_folders', 'strip_trailing_slash', 'update_config', 'url']

now you can check all the functionalities of it.

Example:

                if you want to get last good build use command as,

                j1.get_last_good_build()


Note:
Keep checking this site for further update of new codes. 

4 comments:

  1. I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful to learn python language.
    DevOps Training in Chennai

    DevOps Online Training in Chennai

    DevOps Training in Bangalore

    DevOps Training in Hyderabad

    DevOps Training in Coimbatore

    DevOps Training

    DevOps Online Training

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This was such an informative piece. I had heard about this topic before, but your article provided a deeper understanding and made me consider things I hadn't thought about before. Thank you
    Python Course in Nagpur

    ReplyDelete