Jenkins: New CLI

Hello. It’s been ages since my last post, sorry for that. Looks like my work and studies took over most of my time. But let’s not dwell in the past and move to the purpose of this post!

I have quite a lot of interaction with Jenkins lately and to be honest, I really don’t like using  the Jenkins web interface. I’m always in favor of using good working CLI.

Unfortunately I couldn’t find any client that was good enough for what I’ve been doing with Jenkins. My requirements are pretty basic – 1. it should work, 2. it should cover enough of the different tasks I’m doing on Jenkins. I have been trying couple of clients, but each was either too basic, missing a lot of commands or either not working at some point.

Finally, as a side project, I decided to create my own version. For that I used the great python-jenkins module, which is a wrapper for Jenkins REST API. The rest was quite easy, just needed to create parser and some functions to parse and output more readable output of the information I got from the API calls.

I called it jcli (unfortunately the names jeni and jenkins-client were already in use 😦 )

You can find it on PyPi  or on GitHub. Next I’ll provide quick tutorial on how to install and use it.

Installation

One simple command:

sudo pip install jcli

Setup configuartion

Create the configuration file: /etc/jcli/config.ini

[jenkins]
user=mario
password=its_a_me
url=https://my_jenkins.com

You can also set it up in your current working directory.

How to use Jcli

*Note that you can find all the examples on GitHub  or PyPi pages.

Jcli contains several ‘main’ commands such as: ‘job, node, view’

To view which sub-commands each ‘main’ command contains, run:

jcli job -h

action:
  {count,list,delete,build,copy,disable,enable,last_build}
    count               print number of jobs
    list                list job(s)
    delete              delete job
    build               build job
    copy                copy job
    disable             disable job
    enable              enables job
    last_build          Print information on last build

List all jobs

jcli job list

This command will print list of all the jobs on your Jenkins server. Beware, it might be very long, depends on number of jobs on your Jenkins.

Start parameterized build

jcli job build my_job -p '{"GERRIT_REFSPEC": "my_refspec", "GERRIT_BRANCH": "my_branch"}'

This will start parameterized build with the parameters GERRIT_REFSPEC and GERRIT_BRANCH

Print information on the last build of specific job

jcli job last_build pep8-testing

This command will output summary of the last build of the job: ‘pep8-testing’. It would look like:

=================== Last build summary ===================
Build Number: 4

SCMs:

 Url: https://github.com/redhat-openstack/ansible-ovb
 Refspec: +refs/heads/*:refs/remotes/origin/*
 -----
 Url: https://github.com/bregman-arie/ansible-rhosp
 Refspec: +refs/heads/*:refs/remotes/origin/*
 -----
 Url: https://github.com/redhat-openstack/octario
 Refspec: +refs/heads/*:refs/remotes/origin/*
 -----

Parameters:

 Parameter: GERRIT_REFSPEC
 Value: the_refspec
 -----
 Parameter: GERRIT_BRANCH
 Value: master
 -----

Build Duration: 2037407
Built on slave: my_slave
URL: https://my-jenkins-server/pep8-testing/4/

Result: FAILURE

List all jobs with certain string in their name

jcli job list mario

pep8-mario
its-a-me-mario
unit-mario
tempest-mario-all-tests

List all views

jcli view list

test-jobs
packaging-jobs
poc-jobs

Delete node

jcli node delete my_node

This will remove the node ‘my_node’ from Jenkins

List all the installed plugins

jcli plugin list


github
windows-slaves
ircbot
buildresult-trigger
translation
console-column-plugin
scm-api
naginator
sectioned-view
copyartifact
ghprb
swarm
disk-usag

Print information on specific plugin(s)

jcli plugin list groovy

Name: Groovy Postbuild
Enabled?: True
Has update?: True
URL: http://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin
Version: 1.10

Name: Hudson Groovy builder
Enabled?: True
Has update?: True
URL: http://wiki.jenkins-ci.org/display/JENKINS/Groovy+plugin
Version: 1.22

The above command listed the plugins related to ‘groovy’ with additional information as version, url and whether it’s enabled on Jenkins.

Contribution

I plan to keep improving it whenever I need to do something that requires from me to use the web interface. If you used it and you have some ideas of your own, make sure to share those with me or contribute  to the project using GitHub PR 🙂

3 thoughts on “Jenkins: New CLI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s