In this example, we're going to run builds in Jenkins server based on events listed below. We'll set two Jenkins items/projects. First one Nation-PullRequest is to run builds for normal pull request events such as create, push, comment, close and reopen. The second one Nation-Merge is for just merging pull requests.


How it works


  1. User creates a local branch.

  2. User pushes local branch to GitHub.

  3. User creates pull request for pushed local branch in GitHub.

  4. GitHub tells Jenkins to run build.

  5. Jenkins copies repository from GitHub to /var/lib/jenkins/workspace/Nation-PullRequest/ or /var/lib/jenkins/workspace/Nation-Merge/ directory.

  6. Jenkins runs build tests.

  7. Jenkins tells GitHub about the build result (success/failure). Applies only to Nation-PullRequest item.

Jenkins runs builds when:



Overview


  1. Create a GitHub repository.

  2. Install Ubuntu server. 14.04

  3. Install Apache server. 2.4.7

  4. Install Git package. 1.9.1

  5. Setup GitHub with SSH.

  6. Install Jenkins server. 2.32.1

  7. Jenkins GUI setup.

  8. Install Jenkins GitHub plugins.

  9. Configure Jenkins globally.

  10. Configure GitHub web hooks.

  11. Create a Jenkins project Nation-PullRequest.

  12. Create a Jenkins project Nation-Merge.

  13. Configure Jenkins project Nation-PullRequest.

  14. Configure Jenkins project Nation-Merge.

  15. Manually testing Jenkins projects.

  16. Testing Jenkins GitHub pull request builder.

  17. Testing Jenkins GitHub merge builder.

1. Create a GitHub repository


Our project is called nation and its properties are as follows.


Browser URL: https://github.com/Inanzzz/nation
Clone URL: https://github.com/Inanzzz/nation.git

2. Install Ubuntu server


I assume that you've already installed it and it is up an running.


3. Install Apache server


I assume that you've already installed it and http://localhost is working.


4. Install Git package


$ sudo apt-get update
$ sudo apt-get install git
$ git config --global user.name "Inanzzz"
$ git config --global user.email "inanzzz@helloworld.com"

$ git --version
git version 1.9.1

5. Setup GitHub with SSH


Full information can be found here.


6. Install Jenkins server


You need to have a JDK and JRE installed. At the time of writing this post openjdk-7-jre and openjdk-7-jdk were suggested.


Install JDK and JRE


$ sudo apt-get install openjdk-7-jre
$ sudo apt-get install openjdk-7-jdk

$ java -version
java version "1.7.0_121"
OpenJDK Runtime Environment (IcedTea 2.6.8) (7u121-2.6.8-1ubuntu0.14.04.1)
OpenJDK 64-Bit Server VM (build 24.121-b00, mixed mode)

Install Jenkins


$ wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -
$ sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
$ sudo apt-get update
$ sudo apt-get install jenkins

$ sudo service jenkins status
Jenkins Continuous Integration Server is running with the pid 11444

Log file: var/log/jenkins/jenkins.log


7. Jenkins GUI setup


Installation



Configuration



8. Install Jenkins GitHub plugins



9. Configure Jenkins globally



10. Configure GitHub web hooks


Obtain web hook URL



Enable web hook URL in GitHub



If you see a green tick icon next to the webhook URL after refreshing the page means the link is working. If you click the link and scroll to the bottom of the page, you'll see GitHub and Jenkins communication details under "Recent Deliveries" section.


Enable Jenkins (GitHub) plugin service



11. Create a Jenkins project - Nation-PullRequest



12. Create a Jenkins project - Nation-Merge



13. Configure Jenkins project - Nation-PullRequest



14. Configure Jenkins project - Nation-Merge



15. Manually testing Jenkins project


Nation-PullRequest



Jenkins log will appear as below.


$ tail -f /var/log/jenkins/jenkins.log

Jan 14, 2017 8:02:59 PM org.jenkinsci.plugins.ghprb.GhprbTrigger stop
INFO: Stopping the ghprb trigger for project Nation-PullRequest
Jan 14, 2017 8:02:59 PM org.jenkinsci.plugins.ghprb.GhprbTrigger start
INFO: Starting the ghprb trigger for the Nation-PullRequest job; newInstance is true
Jan 14, 2017 8:03:06 PM hudson.model.Run execute
INFO: Nation-PullRequest #1 main build action completed: SUCCESS

If you open "Console Output" of build #1 shown below, you'll see useful information about build.



Nation-Merge


There has to be a build in "Build History" list to carry out this test.



Jenkins log will appear as below.


$ tail -f /var/log/jenkins/jenkins.log

Jan 14, 2017 8:02:59 PM org.jenkinsci.plugins.ghprb.GhprbTrigger stop
INFO: Stopping the ghprb trigger for project Nation-Merge
Jan 14, 2017 8:02:59 PM org.jenkinsci.plugins.ghprb.GhprbTrigger start
INFO: Starting the ghprb trigger for the Nation-Merge job; newInstance is true
Jan 14, 2017 8:03:06 PM hudson.model.Run execute
INFO: Nation-Merge #1 main build action completed: SUCCESS

If you open "Console Output" of build #1 shown below, you'll see useful information about build.



16. Testing Jenkins GitHub pull request builder


Creating a pull request


We create a new branch feature/update-gitignore in our local PC, commit to it and push it to remote GitHub repository. Jenkins starts building after we create the pull request in GitHub, not before. It waits for us!


Jenkins log will appear as below after pushing the the new branch feature/update-gitignore.


$ tail -f /var/log/jenkins/jenkins.log

Jan 14, 2017 8:20:38 PM org.jenkinsci.plugins.ghprb.GhprbRootAction handleAction
WARNING: Request not known for event: push
Jan 14, 2017 8:20:38 PM org.jenkinsci.plugins.github.webhook.subscriber.DefaultPushGHEventSubscriber onEvent
INFO: Received PushEvent for https://github.com/Inanzzz/nation

Jenkins log will appear as below after starting and finishing build for feature/update-gitignore.


$ tail -f /var/log/jenkins/jenkins.log

Jan 14, 2017 8:21:35 PM org.jenkinsci.plugins.ghprb.GhprbRootAction handleAction
INFO: Checking PR #3 for Inanzzz/nation
Jan 14, 2017 8:21:35 PM org.jenkinsci.plugins.ghprb.GhprbTrigger handlePR
INFO: Checking PR #3 for job Nation
Jan 14, 2017 8:21:35 PM org.jenkinsci.plugins.ghprb.GhprbPullRequest
INFO: Created Pull Request #3 on Inanzzz/nation by Inanzzz () updated at: 1/14/17 8:35 PM SHA: 8f81f2b57df92b7ba0886e9f4ba3c8431b352581
Jan 14, 2017 8:21:35 PM org.jenkinsci.plugins.ghprb.GhprbPullRequest updatePR
INFO: Pull request #3 was updated on repo Inanzzz/nation but there aren't any new comments nor commits; that may mean that commit status was updated.
Jan 14, 2017 8:21:51 PM hudson.model.Run execute
INFO: Nation #3 main build action completed: SUCCESS



If you go to Jenkins GUI, you'll see more details about the builds.



Test this please comment command


You can leave test this please comment to your PR to manually trigger Jenkins build which will be based on last commit. There are more "comment commands" which can be found in Jenkins system configuration settings under "GitHub Pull Request Builder" section or here.


$ tail -f /var/log/jenkins/jenkins.log

Jan 14, 2017 8:42:16 PM org.jenkinsci.plugins.ghprb.GhprbRootAction handleAction
INFO: Checking issue comment 'test this please' for repo Inanzzz/nation
Jan 14, 2017 8:42:16 PM org.jenkinsci.plugins.ghprb.GhprbTrigger handleComment
INFO: Checking comment on PR #3 for job Nation
Jan 14, 2017 8:42:17 PM org.jenkinsci.plugins.ghprb.GhprbPullRequest updatePR
INFO: Pull request #3 was updated/initialized on Inanzzz/nation at 1/14/17 8:55 PM by Inanzzz (comment)
Jan 14, 2017 8:42:27 PM hudson.model.Run execute
INFO: Nation #4 main build action completed: SUCCESS


17. Testing Jenkins GitHub merge builder


We already have a PR in GitHub for branch feature/update-gitignore so we just need to hit "Merge pull request" button.


Jenkins log will appear as below after hitting "Merge pull request" button.


$ tail -f /var/log/jenkins/jenkins.log

INFO: Received PushEvent for https://github.com/Inanzzz/nation
Jan 14, 2017 2:24:22 PM org.jenkinsci.plugins.github.webhook.subscriber.DefaultPushGHEventSubscriber$1 run
INFO: Poked Nation-Merge

Jenkins log will appear as below after hitting "Confirm merge" button.


$ tail -f /var/log/jenkins/jenkins.log

Jan 14, 2017 2:24:24 PM com.cloudbees.jenkins.GitHubPushTrigger$1 run
INFO: SCM changes detected in Nation-Merge. Triggering #9
Jan 14, 2017 2:24:35 PM hudson.model.Run execute
INFO: Nation-Merge#9 main build action completed: SUCCESS




If you go to Jenkins GUI, you'll see more details about the builds.



Readings