Trying the Jenkins View Builder

As you add more jobs to Jenkins you’ll often want to start breaking them out in to smaller, more logically grouped, views. While the UI itself makes this simple it’s a manual task, and as automation loving admins we can do better than clicking around. In this post we’ll take a brief look at the jenkins-view-builder and see if it can make our lives any easier.

My test case will be a simple Jenkins view that should include any jobs whose names match the test-puppet-.*-function pattern. These will be grouped together under the ‘Puppet Functions (auto)’ view. We’ll start by installing jenkins-view-builder inside a python virtualenv using the commands below -

# create the virtualenv
$ virtualenv jenkins-views
New python executable in jenkins-views/bin/python2

$ cd jenkins-views/

# set paths based on the virtualenv
$ source bin/activate
(jenkins-views)[jenkins-views]$

# install the python code
$ pip install jenkins-view-builder

# and check it worked
$ jenkins-view-builder --version
jenkins-view-builder 0.1

We’ll now write our example using YAML. There are two useful starting points when writing your own views. First are the jenkins-view-builder examples/. These present a few different types of jenkins views and can be a useful starting point to crib from. A second approach is to use existing Jenkins views for inspiration. You can read through the Jenkins config file, /var/lib/jenkins/config.xml on CentOS, and extract the XML, which can then be easily converted to YAML.

Using a combination of these sources we end up with a YAML view config that looks like this -

# cat puppet-view.yaml
---
  - view:
      type: list
      name: Puppet Functions (auto)
      description: Puppet Functions via jenkins-view-builder
      includeRegex: "test-puppet-.*-function"
      columns:
        - status
        - weather
        - job
        - last_success
        - last_failure
        - last_duration
        - build_button
      recurse: False

The trickiest part was figuring out the column name formats. We’ll now run this through jenkins-view-builder and take a look at the generated (and xmllint re-formatted) XML.

$ jenkins-view-builder test puppet-view.yaml
$ cat out/Puppet\ Functions\ \(auto\).xml | xmllint --format -
<?xml version="1.0"?>
<listView>
  <owner class="hudson" reference="../../.."/>
  <name>Puppet Functions (auto)</name>
  <filterExecutors>false</filterExecutors>
  <filterQueue>false</filterQueue>
  <properties class="hudson.model.View$PropertyList"/>
  <jobNames>
    <comparator class="hudson.util.CaseInsensitiveComparator"/>
  </jobNames>
  <jobFilters/>
  <columns>
    <hudson.views.StatusColumn/>
    <hudson.views.WeatherColumn/>
    <hudson.views.JobColumn/>
    <hudson.views.LastSuccessColumn/>
    <hudson.views.LastFailureColumn/>
    <hudson.views.LastDurationColumn/>
    <hudson.views.BuildButtonColumn/>
  </columns>
  <recurse>false</recurse>
  <description>Puppet Functions View (via jenkins-view-builder)</description>
  <includeRegex>test-puppet-.*-function</includeRegex>
</listView>

This looks both faithful to our YAML source config and very similar to UI created views we already have in Jenkins. Happy with our example we need to configure credentials jenkins-view-builder can use to access Jenkins and create our new view.

    $ cat jenkins.conf
    [jenkins]
    user=admin
    password=admin_pass
    url=http://jenkins/

Then we run the command to create the view -

    $ jenkins-view-builder update --conf jenkins.conf puppet-view.yaml
    Updating view data in Jenkins
    Starting new HTTP connection (1): jenkins
    Creating view Puppet Functions (auto)
    Starting new HTTP connection (1): jenkins

When this finishes you should be able to refresh your web browser tab and see the newly added view and all the jobs its regex matches.