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.
Scroll down until find the Manage Plugins option.
Then click in the available tab and search the Filter text box and type the text Bitbucket.
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.
When the plugin is installed create a Job and connect with the repository in New Item.
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.
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.
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.
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.
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.
Here a sample of Jenkinsfile for a maven project, the goal is compile the project and use maven for compile declared in tool section.
I’ve configured the maven tool in Jenkins > Global Tool Configuration > Maven.
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.
Done, test the connection by start to build the job in Build Now.
Check the project connects correctly with the repository.
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.
Github Webhooks
In this part the left menu has a option for Webhooks click there and add the webhook configuration.
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/ .
Now in Jenkins create a new Job and in the section of New Item and select the same configuration for pipeline.
In the section for build check the box for Github hook trigger for GitScm polling.
Build the project and see the project compiles.
GitLab
Gitlab dashboard also keep the same configuration base on Webhooks, in the Settings > Webhooks.
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.
Add the plugin of Gitlab.
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: …
In Advanced… generate the Secret token and provide in the GitLab Webhook.
Set the secret token in GitLab.
Now check the connection by build the project.
Azure Repos
For configure Azure Repos go ahead to Project Settings > Service Hooks in the repository.
Then create a Service hook.
Select the Jenkins service.
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.
Now have to provide the Jenkins configuration.
Create new Job.
Set the configure of notifications by check the Poll SCM option.
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.
Build the project.
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:
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.