TL;DR
This post will explain how to trigger a job on Jenkins that will test changes on Github repository when Jenkins is behind a firewall. Github has service integration with Amazon SQS and Jenkins has a plugin that triggers a job on SQS event. To make it work you will need to do the following:
- Create Amazon SQS for your Github repository and configure it
- Add SQS to Github and configure it to notify on push and pull requests
- Install SQS plugin and configure it in Jenkins
- Write pipeline job that will handle the different events
- Post job results to Github branch or pull request
Amazon SQS
“Amazon Simple Queue Service (SQS) is a fully managed message queuing service”, in other words SQS it will be our message bus, where we send messages from Github and listen to them on Jenkins. Jenkins will have to connect to SQS (outbond connection) and not listen to inbound connections. To get started, you need an AWS account and then you can create a new SQS service like this:
- Go to AWS SQS console
- Click on
Create New Queue
, and then: - Click on
Quick-Create-Queue
3 - Select the queue you’ve just created and at the bottom copy for later:
ARN
URL
Next we will create a new IAM user for Jenkins:
- Go to IAM console
- Click on
Add user
, and then:- User name: jenkins4
- Access type: Programmatic access
- Click on
Next:Permissions
, and then:- Select
Attach existing policies directly
- In the Filter search for
AmazonSQSFullAccess
- Select
- Click on
Next:Preview
and then Click onCreate user
- Copy the
Access Key ID
andSecret access key
5
That’s it, you have SQS configured.
Github
Github has a great support for webhooks and services, and there is even a one for Jenkins. But all of them assume Github can reach your Jenkins. Fortunately Github also has a service for Amazon SQS:
- Go to your repository services:
https://github.com/<user>/<repo>/settings/installations
- Click on
Add service
, then find and select Amazon SQS - Click on Amazon SQS service to configure it, and then:
- Aws access key - IAM user
Access Key ID
- Aws sqs arn - SQS queue
ARN
- Aws secret key - IAM user
Secret access key
- Aws access key - IAM user
- Update service
Open your terminal:
Find the service id:
curl -u <user> https://api.github.com/repos/<user>/<repo>/hooks
Modify on which events the service will trigger:
curl -X PATCH --data '{ "events": ["push", "pull_request"] }' -u <user> 'https://api.github.com/repos/<user>/<root>/hooks/<id>'
Github will now send messages on push and pull request to your Amazon SQS queue. You can find what other events can be added and the messages content at Github documentation on web-hooks and events.
Jenkins
Jenkins has a vast collection of open source plugins and even two for Amazon SQS. You will need only one of them and a pipeline support:
- Go to Jenkins Plugin manager:
http://<jenkins FQDN>/pluginManager/available
- Install
AWS SQS Build Trigger Plugin
6 - Go to Jenkins configuration:
http://<jenkins FQDN>/configure
In section Configuration of Amazon SQS queues click on
Add
, then:- Credentials: Click on
Add
and select Jenkins, then:- Kind: Secret Text
- Scope: Global
- Secret: IAM user’s
Secret access key
- ID: IAM user’s
Access Key ID
- Queue name: Queue
URL
- Click on
Test access
, you should see: “Access to <queue> successful”
- Credentials: Click on
Click on
Save
Create a new Pipeline job7 with the following setting:
- In section Build Triggers:
- mark: ‘Trigger build when a message is published to an Amazon SQS queue’
- SQS queue to monitor: <queue>
- In section Pipeline, use
Pipeline script
and you can use this as template for your job:Pipeline- Lines 11-36 function for commenting on Github:
- Install python library
PyGithub
on the executing system - Create Access token in Github for your user (or a bot user)
- Set global parameter in Jenkins
GITHUB_ACCESS_TOKEN
- Install python library
- Lines 38-61 function for cloning and checking out the right repository based on the SQS message from Github.
- Lines 69-73 will handle parameters from SQS trigger, the most important is
sqs_body
- Lines 76-87 will prepare everything you need for the job to work
- Lines 11-36 function for commenting on Github: