How to connect Jenkins for SCM with (Bitbucket/GitHub/GitLab/Azure Repos)

RicDev2
7 min readApr 7, 2020

When design a pipeline of CI/CD for any application, one of the first steps is use a SCM (Source Code Management) to extract the code for build, test and deploy, but sometimes the configuration for the remote repositories change according with the plugin or options. I’ve used the next git repositories: Bitbucket, GitHub, GitLab and Azure Repos in many projects, so here I show you what configuration you need for each one.

First, we’re going to need a Jenkins up and running. I’m running Jenkins on a docker container, using the image provided by Docker hub https://hub.docker.com/_/jenkins also you can use the jenkins.war file from the Jenkins website if you don’t have Docker.

So, let’s start to configure Jenkins.

Bitbucket

Bitbucket is one of the most popular SCM and provides a GUI easy to navegate and manage the repository and colaborate. With Bitbucket we’re going to need to download the plugin available in Manage Jenkins section.

Access to Jenkins, in my local machine is the url http://localhost:8080. Navigate to section Manage Jenkins -> Manage Plugins.

Lateral menu and display Manage Jenkins link.

Scroll down until find the Manage Plugins option.

With manage plugins we can download any plugin we need.

Then click in the available tab and search the Filter text box and type the text Bitbucket.

Available tab and Filter box.
You’ll see the “Bitbucket plugin” appear. Choose exactly what says Bitbucket ignore the other plugins.

We have to check the Bitbucket option and download the plugin. Here there are two options (install without restart or Download now and install after restart) I’ll chose the second and check box Restart Jenkins… In this moment Jenkins start to download the plugin then the server will be restart. Depends on your internet connection the download takes a while.

Installing the Bitbucket plugin, wait until finish download.

When the plugin is installed create a Job and connect with the repository in New Item.

New item for create the Job.

Choose a name and select the pipeline then click in Ok button. In this post I’ll use the configuration by Pipeline for all examples.

Pipeline Job project.

In Build Trigger section check Build when a change is pushed to Bitbucket for trigger the Job when a push code event come from Bitbucket.

Build Trigger section.

In Pipeline section select the repository from Bitbucket and select the credentials for Bitbucket. You can configure the credential in Jenkins in Jenkins > Credentials > System > Global Credentials (unrestricted) .Create a Credentials for each of remote repositories.

Repository connection with credentials.

This Pipeline section configured used from SCM and Git. The Repository URL all SCMs provide you when you clone the repository.

Bitbucket Webhooks

For send notification to Jenkins about events (push code) from Bitbucket , we need a mechanism called Webhooks, so in the project of Bitbucket go to Settings > Webhooks and Add webhoook.

Add a new webhook in the settings of Bitbucket project.

And now put the IP_JENKINS server and port or domain. The url looks like: http://<IP_JENKINS>:8080/bitbucket-hook/ and keep the values by default or change the Trigger by receive another type of notification from the repository.

Webhook configuration.

Here a sample of Jenkinsfile for a maven project, the goal is compile the project and use maven for compile declared in tool section.

Jenkinsfile sample.

I’ve configured the maven tool in Jenkins > Global Tool Configuration > Maven.

Maven configuration used the version 3.5.4.

And for generate a simple maven project use the command: mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4

Push your project to repository, don’t forget add Jenkins file in root project.

The state of Bitbucket repository.

Done, test the connection by start to build the job in Build Now.

Build Now option for start the Job.

Check the project connects correctly with the repository.

View of build finish.

GitHub

Github configuration is similar to all git servers base on webhooks, sending events in the repository always configure by Webhooks. I created a project in Github. The Settings tab is where should configure the webhook.

Repository dashboard.

Github Webhooks

In this part the left menu has a option for Webhooks click there and add the webhook configuration.

Add new Webhook for connect with Jenkins.

In the Payload URL we have to provide the Jenkins ip or domain and concat the context github-webhook http://IP_JENKINS:8080/github-webhook/ .

Webhook configuration.

Now in Jenkins create a new Job and in the section of New Item and select the same configuration for pipeline.

New Job for Github.

In the section for build check the box for Github hook trigger for GitScm polling.

Build Trigger section.

Build the project and see the project compiles.

Result of Build Job.

GitLab

Gitlab dashboard also keep the same configuration base on Webhooks, in the Settings > Webhooks.

Gitlab Dashboard.

Set the data of webwook of Jenkins as the url : http://IP_JENKINS:8070/project/cicd-project-gitlab and check the box for receive the push events. I changed the port of Jenkins because a have some problems with the port 8080.

Webhook configuration.

Add the plugin of Gitlab.

Downloading the Gitlab plugin.

Wait to download and create a new job in Jenkins in the section for Build Triggers check the box Build when a change is pushed to Gitlab webhook URL: …

Build Trigger section for GitLab.

In Advanced… generate the Secret token and provide in the GitLab Webhook.

Click in Generate to get the Secret Token.

Set the secret token in GitLab.

Secret Token in Webhook configuration.

Now check the connection by build the project.

Result build Job.

Azure Repos

For configure Azure Repos go ahead to Project Settings > Service Hooks in the repository.

Azure Repos dashboard.

Then create a Service hook.

Service Hooks option.

Select the Jenkins service.

Assistant for create new Hook.

Now select the Trigger for send notification Jenkins, I choose Code pushed for receive the notification when code is pushed but you can choose another event.

Type of Trigger event.

Now have to provide the Jenkins configuration.

Jenkins connection data.

Create new Job.

New Job Jenkins.

Set the configure of notifications by check the Poll SCM option.

Build Triggers section.

For connect with Azure, make sure generate Git credentials for used as authenticate in user.

NOTE: Do not use your user account of Azure Repos, in your repository you need generate the credentials.

Credentials to connect Jenkins.

Build the project.

Build Job result.

Branch Building

In some cases the branch to build the pipeline must be specific, by default Jenkins defines the master branch to build, but in the cycle of CI you need build on develop, release o feature branch.

Use the next configuration in your job:

In branch to build add the branches to execute the job.

Conclusion

When you start to discover the Devops world there are many options, tools and ways to create pipelines that proof the application through stages. In the market exists many git servers in cloud that help us to save out code and provide management, connectors to CI servers. Jenkins is a good CI server, easy to use and configure, scalable and secure. Plugins allow us connect Jenkins to git servers for all kind of events in the repository. I hope this post help you and save you many hours of searching.

--

--