DIY Continous Integration with GitHub Webhooks, Azure, and Docker Containers Part 1

There are plenty of tools out there focused on CI (Continuous Integration), not all of which play nice with Microsoft or are for some reason inaccessible ($$, IT management, etc).  This is one approach I’ve used based of the tools available to me.  Maybe some part of it will be of value to you.

The Main components are Github, Azure Runbooks, and Docker.  The overall process is this.  You make a commit in github, then github will send a bunch of data over to Azure to be processed.  Azure will spin up a docker container and dump your code onto the container.  I chose Azure for two reasons.  The cost to run the runbooks is very small.  You get 500 minutes Free!  After that its still only $0.002 / minute.  The second reason is that all the work has been done for you by Azure.  You don’t have to set anything up or manage another server.  We all have better things to do.

I’m actually going to start with the Azure Runbook because without that you can’t really create the webhook.  Create a Powershell Runbook and edit it.  Copy and paste the following for the first few lines.

param ([object]$WebHookData)
$WebhookBody = $WebhookData.RequestBody
$Inputs = ConvertFrom-JSON $WebhookBody
#$Inputs# here for debug, it will output to the ouput section of the job in Azure
#$Inputs.head_commit.modified
$authorEmail = $Inputs.head_commit.author.email
$ref = (($Inputs.ref).Split('/'))[-1]
$user = $Inputs.head_commit.committer.name
$message = $Inputs.head_commit.message
$message += "`n"
$files = $Inputs.head_commit.modified
$files = $files + $Inputs.head_commit.added
"Files changed/added $files"
$repo = $Inputs.repository.name
$org = $Inputs.repository.owner.name

There is plenty of additional information in $WebhookData.RequestBody, but this list grabs some of the key elements you need: what changed, by who, and what org/repo the changes were made in.  This is also a good starting point.  From here we need to add a Listener, aka Webhook.  Go ahead and publish what you have so far.  Back on the main page for the runbook under “RESOURCES” is a link to Webhooks.  Click on that and click to Add a webhook.  Create a new Webhook.  Give it a name and this is key YOU MUST COPY THE URL NOW.  Once you create it you can NOT go back and see the URL again.   Paste it someplace handy so you don’t lose it.  Almost done.  Click on OK and then click on “Modify run settings”.  The default is to Run on Azure.  Now this is also a great alternative and you can build automation around Azure vms and apply most of what will follow.  However, since I have access to on-prem equipment its cheaper to run everything local.  You will need to set up a Hybrid runbook worker  (This process is outside the scope of this post).  Don’t worry you can finish creating the Webhook now, leaving it on Azure, and come back later to switch over to your Hybrid Worker.

Now on to github.  To set up a repository webhook on GitHub, head over to the Settings page of your repository, and click on Webhooks & services. After that, click on Add webhook.  Take the URL you copied earlier and paste it into “Payload URL”.  You can leave the rest as defaults, make sure Content type is application/json.  Click on “Add Webhook”

Now you’re ready to start some basic testing.  If you don’t already have a dev/test branch for your repo, go ahead and create one.  Make a commit to your test branch.  Head back over to the Azure runbook.  This time under “RESOURCES” click on “Jobs”.   You should see a completed job (possibly In Queue or Running if you are quick).  Click on the Job.

Now you can click on the Output button and you should see some useful information.  If you go back to your runbook editor and start removing comments you will get even more information.  Remove the comment from $Inputs to see everything.  Use this part to get familiar with the data coming from github to determine what you consider to be valuable.

Part II will cover some decision making and spinning up the Container.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s