GITLAB JENKINS PROPERTY GENERATOR

We use Gitlab at work and I've recently been working on improving our deployments. We have several old monolithic applications which are a challenge to deploy for QA especially as we are getting into the habit of using feature branches to manage our code.

What happens is Joe will deploy feature/ABC-253 to our QA server. Then Sally will need to get feature/ABC-764 tested as well. The developers can certainly hop on the server and easily switch branches but I wanted our QA person (who isn't that technical) to be able to do this as well.

Digging through the Gitlab API I figured out I could grab a list of current branches for a particular project. Tie that into a parameterized build on Jenkins and I thought I could setup a simple "GUI" for our QA person to be able to switch branches on a project.

Note: At the time I wrote this I was also interested in learning ColdBox so I wrote the app using that framework. If I were to do it over again I'd probably do it using CommandBox.

The players:

Install

Configuration

Open the /jgpg/config/Coldbox.cfc file and enter in your Gitlab URL, API key and the path to where you want your property file stored.

Note the property file location needs to be readable from your Jenkins server!

 settings = {
  apiKey = 'enter-your-key-here',
  propertyPath = 'enter-path-to-where-you-want-properties-file',
  gitlaburl = 'enter-the-root-url-to-your-gitlab-instance'
 }

Usage

This assumes you have a working Lucee / Railo installation. If you don't, the quickest way to get this up an running is via CommandBox. Follow the installation instructions for CommandBox, download the JGPG code, open a command prompt in the JGPG directory and type 'box server start'. This will start a server. Note the port # in the prompt: 127.0.0.1:58374. Open that URL in your browser and hopefully you will see something like:

JGPG Output

Now you can setup a scheduled task to run this code and generate a new .properties file as needed. I run this once a day. We don't create feature branches that often and I find this is an acceptable schedule. YMMV.

Jenkins Configuration

Install the Extended Choice Parameter Script plugin in Jenkins. In my case I am simply using this to set a property in my job which I can pass on to Ant / Git to deploy the appropriate branch to my QA server.

Configuring the plugin is straightforward.

Jenkins Config

Enter a name of the parameter you want to define, also provide a description i.e.: 'branch'

For the parameter type I just want to display a dropdown of all the available branches to my QA user. So I select "Single Select".

Next you define where your property file lives. This is the file generated by our JGPG code. I'm on Windows so I simply setup a share somewhere and point both my code and Jenkins at the same shared folder.

The "Property Key" ties to the list of projects in your properties file:

[group1]
project-abc=develop,feature/ABC-4369,feature/ABC-4606,feature/ABC-4700,feature/ABC-4717
project-xyz=feature/XYZ-4637,develop,feature/XYZ-4541
[group2]
project-foo=feature/FOO-2597,feature/FOO-3296,feature/FOO-4242,develop,feature/FOO-1500

So if I want to display a list of project-foo's branches I will set my "Property Key" to "project-foo". In the screenshot above this is set to 'sto' the name of one our Gitlab projects.

And finally you can pick a default - normally we want to be on the 'develop' branch so this is defined as the default.

Now our QA user will open our Jenkin's job, click Build With Parameters and be presented with a list of branches they can deploy

Jenkins Config

Once selected the branch name is available in our 'branch' property we defined which can be passed on to our build/deployment script.

How you deploy is up to you :)

Published on Friday, March 06 2015     Tags: git tools jenkins

COMMENT / SHARE

About

Jim is a Senior Applications Engineer working at Red Hat. He slso likes motorcycles and keeping his quadcopter out of trees.