Automating Github releases via Jenkins

Recently at work we were discussing releasing internal python artifacts to Github. Github has the concept of releases1.

Releases are a workflow for shipping software to end users. Releases are first-class objects with changelogs and binary assets that present a full project history beyond Git artifacts. They're accessible from a repository's homepage.

Thus anybody within the company who has access to the repository would be able to access the release artifact, view the release history and know what went into each release by looking at the release notes/comments. It's pretty nifty!

You can of course create each release manually, but we wanted to automate the releases via Jenkins! Now, Jenkins has Github Publisher plugin but it does not have the ability to perform releases. Not as of yet. But, Github provides an api2 which is super easy to use. And it is pretty straightforward to create a simple Python script that would integrate with the API to create releases, upload artifacts etc.

So I came up with a simple python script that allows us to:
a. Get all the releases for a given repo,
b. Create a release,
c. Delete a release given the release name,
d. Download release artifacts given the release name,
e. List artifacts for a release.

I have included the full code below:

You can use the script like so:
python github\_release\_management.py -c -t <Github_Token> -o <Github Organization> -r <repo name>

Creating a release is now a simple two step process:

export BUILD_NUMBER=1  
python github_release_management.py -c -t  -o Spiderman -r awesome-api
Creating the Github token for Jenkins:

In order to create and upload releases you need access to your github. If you have 2FA (Two Factored Authentication) ON then you need to generate a token that would allow you to create/upload release artifacts. You can create a token by going to Account Settings -> Application and generating a new token:

Once you have created the token, copy it some place so you can easily access it for the next step.

Integrating with Jenkins:

The reason why I am using BUILD_NUMBER environment variable in the script above is because Jenkins automatically sets the BUILD_NUMBER variable for each build. So I can use that as part of the release number.

To setup Jenkins build to run the python script above you need to install the Post Build Task3 plugin. Once you have the plugin installed, you can then go to your actual build configuration and setup the Post Build Action like so:

Notice the grayed out area after -t-, that's where you should put the token you generated above.

Save. And voila! You are done!


Have you automated releases via Jenkins on GitHub? How did you do it?

Also, please feel free to comment below any questions/comments/ideas you might have? Your comments help me understand what I need to tweak on my blog so as to make the content more explanatory!


References: