Configure and Trigger a Jenkins CI Server using Github Webhook

Configure and Trigger a Jenkins CI Server using Github Webhook

In this simple hands-on lab, we've been asked to configure a Jenkins project to build the train schedule app. The source code for the application is hosted in the GitHub Repo. The app already has build automation set up using gradle wrapper, and can be built with ./gradlew build. The team wants Jenkins to execute this automated build every time changes are pushed to the GitHub repository.

We will need to:

  • Configure Jenkins to authenticate with GitHub

  • Create a freestyle project in Jenkins

  • Configure the project to build the train schedule app

  • Set up a webhook to trigger the build whenever changes are made to the repository in GitHub

  • Configure the build to archive trainSchedule.zip as a build artifact

To confirm, we will make a change in GitHub and commit it. This should trigger a build in Jenkins automatically

Steps:

  1. First, we'll create a personal fork of the sample application.

  2. Create an API key in GitHub that will allow Jenkins to interact with GitHub. Navigate to settings > developer settings > personal access tokens > generate new token.

    We'll assign the token the "admin:repo_hook" permission. This will enable Jenkins to utilise github's API to create webhooks for jenkins.

    Copy the generated token
    Webhooks are event notifications made from one application to another over HTTP.

  3. In Jenkins, setup the connection to github using the API token just created. navigate to Manage Jenkins > Configure Systems > scroll down to GitHub and Add GitHub Server

    Add the credentials (select secret text as the kind and paste the api key generated in github)

    Leave "Manage hooks" box checked to allow jenkins automatically manage webhooks from github

    Click "Test Connections" to verify everything is setup correctly

    The Jenkins instance is successfully configured to talk to github.com

  4. Configure the project.

    Copy the URL for the project's github repository and use it to setup a "New Item" in jenkins.

    select the build trigger to setup a webhook so that everytime changes are commmited to the github repo, github sends webhook to jenkins, this will automatically trigger a build in jenkins

    setup build steps. this is a gradle build that uses the gradle wrapper. the task is "build" and this will call the ./gradlew build.

    create a post build action to archive the artifact. this will archive the artifact that was produced as a result of this build. the artifact is a zip file that contains all the code that will need to be deployed to a production web server for example.

click on "Build Now" to check and see if the build is working

artifact of the manually triggered build

  1. Test the webhook automatic trigger.

    commit changes to github to trigger automatic build. The "README.md" file was edited

build automatically triggered by commit

build successful

artifact of the automatically triggered build

Voila! QED!