In this example, we are going to let GitHub trigger Jenkins pipeline build only when we push code to a feature branch in GitHub repository. That's all!


Apply steps defined under "Jenkins server GitHub SSH integration" and "Allow Jenkins user to execute docker commands" sections in Integrating Jenkins with GitHub to run dockerised application pipeline page.

Ngrok setup

Download ngrok to your host OS's desktop and run $ ./ngrok http -host-header=rewrite in terminal. The is your Jenkins GUI address. This will give you an URL that can be accessible from the Internet. We will use this to integrate with GitHub later on. Assume that it gave us as URL.

GitHub Integration Plugin

Find and install "GitHub Integration Plugin" plugin in Jenkins "Manage Plugins" page.

GitHub webhook URL

Do the following in Jenkins GUI.

  1. Go to "Configure System" page.

  2. Click "Advanced" button under "GitHub" section.

  3. Tick "Specify another hook url for GitHub configuration" tickbox, note URL somewhere and untick it again.

  4. Exit from page without saving.

Prepare Github repository

Go to GitHub repository on and do the following.

  1. Go to "Settings" and then "Webhooks" page.

  2. Paste to "Payload URL" box. As you can see we are not using the local IP address.

  3. Leave "Just the push event" option selected under "Which events would you like to trigger this webhook?" section. Note: This should normally be enough but optionally select "Let me select individual events." then tick "Pushes" and "Pull requests" options.

  4. Click "Add webhook" button.

If you echo Jenkins logs at /var/log/jenkins/jenkins.log file, you should see line below at the bottom.

Feb 16, 2019 5:59:32 PM org.jenkinsci.plugins.github.webhook.subscriber.PingGHEventSubscriber onEvent
INFO: PING webhook received from repo !

Also, if you check the details of the webhook in GitHub page, you will see the communication information between GitHub and Jenkins. It can be found under "Recent Deliveries" section of the added webhook.

Create Jenkins project

Go to Jenkins and do the following to add a new project.

  1. Name the project as "game-pr-builder", select "Pipeline" and save it.

  2. Under "General" tab, tick "GitHub project" option and paste into the box.

  3. Under "Build Triggers" tab, tick "GitHub hook trigger for GITScm polling" option.

  4. Select "Pipeline script from SCM" option under "Pipeline" section.

  5. Select "Git" as SCM.

  6. Use for "Repository URL".

  7. Select GitHub-inanzzz from "Credentials".

  8. Change */master to **/feature/* (or feature/*) because we only want "feature" branches to build for git push actions.

  9. Add ci/pipeline/branch/feature/Jenkinsfile to "Script path".

  10. Save and exit.

Test with git push event

$ git status
$ git add .
$ git commit -m 'Update readme file'
$ git push origin feature/test
139991f..25f345e feature/test -> feature/test

Your Jenkins log should print lines below.

Feb 16, 2019 9:22:47 PM org.jenkinsci.plugins.github.webhook.subscriber.DefaultPushGHEventSubscriber onEvent
INFO: Received PushEvent for from ⇒
Feb 16, 2019 9:22:47 PM org.jenkinsci.plugins.github.webhook.subscriber.DefaultPushGHEventSubscriber$1 run
INFO: Poked game-pr-builder
Feb 16, 2019 9:22:49 PM com.cloudbees.jenkins.GitHubPushTrigger$1 run
INFO: SCM changes detected in game-pr-builder. Triggering #15
Feb 16, 2019 9:23:12 PM org.jenkinsci.plugins.workflow.job.WorkflowRun finish
INFO: game-pr-builder #15 completed: SUCCESS



pipeline {
agent any

options {

stages {
stage('Checkout SCM') {
steps {
echo '> Checking out the source control ...'
checkout scm
stage('Docker Up') {
steps {
echo '> Building the docker containers ...'
sh 'cd docker && cd ci && make build'
stage('Composer Install') {
steps {
echo '> Building the application within the container ...'
sh 'cd docker && cd ci && make composer'
stage('Test') {
steps {
echo '> Running the application tests ...'
sh 'cd docker && cd ci && make test'


CONTAINER := game_php

@docker-compose up -d

@docker exec -i $(CONTAINER) composer install

@docker exec -i $(CONTAINER) vendor/bin/php-cs-fixer fix src --rules=@PSR2 --using-cache=no --dry-run --verbose --diff
@docker exec -i $(CONTAINER) vendor/bin/phpunit tests
@docker exec -i $(CONTAINER) vendor/bin/phpstan analyse src tests --no-progress --level=max
@make -s down

@docker-compose down --volumes
@make -s clean

@docker system prune --volumes --force