14/01/2017 - GIT, JENKINS, LINUX
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.
/var/lib/jenkins/workspace/Nation-PullRequest/
or /var/lib/jenkins/workspace/Nation-Merge/
directory.Nation-PullRequest
item.Jenkins runs builds when:
test this please
, add to whitelist
etc.Nation-Merge
item.14.04
2.4.7
1.9.1
2.32.1
Nation-PullRequest
.Nation-Merge
.Nation-PullRequest
.Nation-Merge
.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
I assume that you've already installed it and it is up an running.
I assume that you've already installed it and http://localhost
is working.
$ 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
Full information can be found here.
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.
$ 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)
$ 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
http://localhost:8080
.$ sudo cat /var/lib/jenkins/secrets/initialAdminPassword
.admin
:ab56a379e3154a38acfc46b91bc0c25a
credentials.http://localhost:8080
.$ sudo service jenkins restart
.http://localhost:8080
.http://12345e55.ngrok.io/
into it. If I didn't:Inanzzz
into "Admin list" textbox.http://localhost:8080
.http://your-jenkins-domain.com:8080/github-webhook/
somewhere then untick it again.http://your-jenkins-domain.com/ghprbhook/
into "Payload URL" field. Pay attention to the difference with the original web hook URL we obtained above!!!x-www-form-urlencoded
in "Content Type" field.Issue comment
, Pull request
and Push
options.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.
Jenkins (GitHub plugin)
service.http://your-jenkins-domain.com/github-webhook/
into "Payload URL" field. Pay attention to the difference with the original web hook URL we obtained above!!!http://localhost:8080
.http://localhost:8080
.http://localhost:8080
.20
in "Max # of builds to keep" field.https://github.com/Inanzzz/nation
in "Project URL" field.sha1
in "Name" field.https://github.com/Inanzzz/nation.git
in "Repository URL" field.origin
in "Name" and +refs/pull/*:refs/remotes/origin/pr/*
in "Refspec" field.${sha1}
in "Branch Specifier" field.http://localhost:8080
.20
in "Max # of builds to keep" field.https://github.com/Inanzzz/nation.git
in "Repository URL" field.origin/develop
in "Branch Specifier" field. In the case of hotfix and release, it will help to run tests.origin/master
in "Branch Specifier" field. In the case of hotfix and release, it will help to run tests.http://localhost:8080
.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.
There has to be a build in "Build History" list to carry out this test.
http://localhost:8080
.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.
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.
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
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.