Hi everyone and welcome back to the “Get started with Jenkins” series.
In the previous video I showed you how you can get your source code out of a git repository
and into Jenkins. However, this also required us to manually
start the Jenkins build process.
In this video we're going to set up Jenkins so that a build is started after each commit
to our repository on BitBucket. To do this, we're going to create a webhook on the BitBucket
website. This webhook will notify Jenkins of all new commits and will send some information
along with it. This information includes the branch that the commit was made on, the name
of the repository and so on.
Just a quick note before we continue. Previously in this series I told you that I ran Jenkins
in a virtual machine on my Mac. For this video however, I moved my Jenkins
installation to DigitalOcean.
Why? Well BitBucket needs to be able to access my Jenkins installation to tell it about new
commits. Obviously my home network is not directly
accessible from the open internet so I opted to move my Jenkins installation to a DigitalOcean
droplet.
So what’s next for this video?
I’ll start by installing the BitBucket plugin for Jenkins. This plugins makes sure that
Jenkins is listening for messages that are coming from BitBucket.
Than I’m going to create a new job and configure it. After that, I’ll test and verify that
everything is set up correctly!
So let’s get going!
So here I am at my Jenkins installation. Let's start by installing the BitBucket plugin.
I'm going to go to "Manage Jenkins" and I'm going to click on "Manage plugins" to install
the plugin. On the tab "Available" I'm going to use the filter option and I'm going to
type "BitBucket". For some reason my computer is very slow when I'm searching here, as you
can see, the letters are sort of catching up to me. Once the search results are finished
loading, it's the last plugin here on this page, it's called "BitBucket plugin, integrate
Jenkins with BitBucket". So I'm going to click "Download now and install after restart" to
install the plugin. And this is going to install all the dependencies of the plugin. It's going
to install Mercurial, SSH, a git client, Junit, etc. So now that it's done I'm going to tick
this box to restart Jenkins and wait until it has restarted.
After about half a minute, Jenkins redirected us back to the homepage. Let's just see if
the plugin was installed correctly. I'm going to click on "Create new job" and I'm going
to create a freestyle project and call it "my-bitbucket-project". And on the next step
I should be able to enable the BitBucket integration. So let's see if that is the case. So yes,
here at the end in the section "Build triggers" I see "Build when a change is pushed to BitBucket".
That's exactly what we want. Now for the purpose of this video I also created a repository
on BitBucket, a test repository to show you how you can integrate it. And it has just
one file in it called "this is my file!" and it's got the same contents.
So let's now go back to Jenkins and configure our project or job. I've already given it
a name and now I'm going to say that I'm using the git source control management system.
And it asks me the location of my repository. So let's go back to BitBucket here and you
click "Clone". BitBucket reveals the clone URL for your repository. I'm just going to
copy this URL here and I'm going to paste it back into Jenkins. This is a public repository
so I don't need credentials. I want to build everything in the master branch so this is
ok. I don't want additional behaviour so I'm going to leave the rest to default. Now in
the "Build triggers" section I'm going to enable "Build when a change is pushed to BitBucket".
This enables without any further more or do and I'm going to click "Save". And that's
pretty much all we have to do in Jenkins. The next thing is to add a webhook to our
BitBucket repository. I'm going to go back to BitBucket here and I'm going to the settings
of my repository. Under integrations there is a feature called "Webhooks". So if I click
on "Webhooks" I have the possibility of adding one. So i'm going to add one now. I'm going
to call my webhook Jenkins and now BitBucket needs to know the URL or the location of my
Jenkins installation. This URL will be notified every time we push new commits to BitBucket.
I'm going to paste in the URL of my Jenkins installation but you have to append "bitbucket-hook".
That's pretty much all that we have to do. We keep the status active because obviously we want
the webhook to trigger. We don't need SSL or TLS and we want to trigger every time we
push to our repository. I'm going to click save. And now we are ready to test our webhook.
So to effectively test our webhook I'm going to change the contents of the test-jenkins
repository, make a new commit and then push that commit to BitBucket to see if it notifies
my Jenkins installation of the change. So I'm going to open this file with Sublime Text
here and I'm going to add something here. I'm going to save it and now I'm going to create a new
commit. Now I use SourceTree as my preferred git client but you can use any other client
obviously. I'm going to stage this file and say "This is an awesome commit that will be
build by Jenkins". And I'm going to tick this box here "Push changes immediately to origin/master"
and this will not only commit, it will also push the changes to BitBucket. That should
trigger our webhook.
Ok so that's done, let's go back to Safari and go to the Commits section of our repository
to verify that our commit is there. Our commit is there! So now let's check Jenkins to see
if it has actually triggered a build. So if I go back to Jenkins, the build is already
there! Jenkins has auto refreshed the page and there you go! Our first build is here.
It's triggered by our webhook. We can open it to see there are obvously no changes since
this is the first build that Jenkins does of our project. And it even states: "Started
by Bitbucket push by Savjee". So there you have it: our very first automated build with
Jenkins!
So that’s it for this video. Curious to learn more about Jenkins?
In the next video I’ll show you how you can run PHPUnit tests after each commit.
Thank your for watching this video! If you like my video’s make sure to follow
me on Twitter or subscribe to my channel!