Step-by-Step Guide for CI/CD with Maven, Jenkins, and GitLab Docker
Lately, CI/CD mechanisms have become the most commonly used process in development. Here, I will try to share a simple CI/CD pipeline using Jenkins to perform a git push to an online repository.
The scenario is that when a developer performs a git push to the testbed server, a Maven install check will be carried out in Jenkins. If the Maven install is successful, it will proceed with a git push to the online GitLab. In the future, we can add more actions in Jenkins, such as deploying to a Docker server or automatic testing, but here we will try a simple integration.
First, we need Jenkins and GitLab offline images, which we will deploy on the testbed server.
A. Create a Docker Compose file for Jenkins, here I will listen on port 8080.
B. Also, deploy the GitLab image. Here, I am using the default settings where it will run on port 80 with the container name “gitlab”.
After the GitLab offline container is successfully running in Docker, we can access it directly at the URL http://localhost. When accessing the website for the first time, a form will appear to set a new password.
After setting up the GitLab password, we need to configure network settings:
1. Access GitLab:
• Open your browser and navigate to http://localhost.
• Log in with the root user and the password you set during the initial setup.
2. Navigate to Admin Area:
• After logging in, click on the profile icon in the top-right corner.
• Select Admin Area from the dropdown menu.
3. Go to Network Settings:
• In the Admin Area, look for the Settings menu in the left sidebar.
• Scroll down and select Network.
You should now be in the Network settings area where you can configure various network-related settings for your GitLab instance.
This will allow you to customize network settings such as outbound requests, inbound email settings, and more according to your requirements.
Check “Allow requests to the local network from hooks and services” to enable access for services running on the local network. In this case, the Jenkins container we set up earlier will perform the webhooks.
Log out, then register a new account which we will use as the developer user.
After the user is successfully created, log out and log back in with the root user. Then select “Create new group”. Here, I will create an example group named “Jenkins Group”; you can adjust the name based on your team.
Select the user that was previously registered and make them the owner.
Log out, then log in using the user we registered (ahmedyusuf). Enter the Jenkins Group. After that, create a project named “belajar-jenkins”; you can adjust the name based on the project you are working on.
After that, go to the profile page (top right corner) > Settings > Access Tokens, then create a token.
Here we will obtain an access token that will be used for the webhook. Copy the access token and note it down.
C. GitLab Online
Open the GitLab website at http://gitlab.com, select Account > Settings, then select the Access Tokens menu on the left. Create a token and copy it.
Click “Create personal access token”, and immediately save the token key in a note. This way, we have saved the tokens from both GitLab offline (docker) and GitLab online in our note.D. Setting Jenkins
Go back to the Jenkins container, open the Jenkins container logs to get the initial password for login.
Open the Jenkins website at http://localhost:8080 and enter the password.
After logging in, proceed by installing the suggested plugins. Here, we will need several plugins to be installed.
Wait for the loading to complete.
Next, install the Maven Integration plugin from the Plugin Manager menu to integrate our Maven project with Jenkins.
Also install the GitLab Hook plugin to enable the GitLab webhooks feature.
E. Create credential
Here we will need credentials for both GitLab online and offline (docker). Enter the two API tokens for GitLab Offline and GitLab Online that we generated earlier.
Also create a GitLab username and password.
So we have 2 credentials for GitLab online and 2 credentials for GitLab docker, like this:
F. Setup connection
Next, we need to ensure that the API tokens we entered can connect to both GitLab Online and GitLab Offline (Docker). For GitLab Docker, I am using the host URL http://gitlab because the container runs with the name gitlab, so we can access it based on the container name. Make sure both connections are successful.
G. Open gitlab docker
Copy the project URL from the top right corner by clicking the “Clone” button, then choose the “Git” option (not the SSH one).
Create a project in Jenkins, and select Git for source code management. Enter both repository URLs based on the URLs from GitLab online and offline. Here, in the first form, I set GitLab offline as the origin.
In the Build Trigger section, set “Build when a change is pushed to GitLab” so that if there is a push to the GitLab offline repository, a push event trigger will occur to GitLab online.
In the Build section, set Maven as follows. This ensures that before pushing to GitLab online, a Maven install will be performed to verify that the source code can be installed successfully and the unit tests run normally.
In the Post-build Actions section, add Git Publisher. Here, we will configure it to push to the remote GitLab online repository only if the source code is successfully built.
H. Setting Webhook in GitLab docker
Go back to GitLab offline, navigate to Settings > Integration.
Enter the URL as specified in the build trigger section in the previous Jenkins menu. Here, we will set up the webhook URL configuration in Jenkins. For the URL format, enter it as follows: http://[username_developer]:[password]@[jenkins_container_name]:[jenkins_port]/[project_name].
Test the webhook and ensure you get a 200 status code.
I. Setup Git in the Jenkins Docker container
To allow Jenkins to push to GitLab online, we need to generate an RSA key as a credential to enable connectivity.
Enter the Jenkins container.
Generate an RSA key using your GitLab online username and password.
Add the public key to SSH keys in GitLab online.
Perform a git clone test.
Create a README file and push it to the master branch. Ensure the master branch is successfully created.
Change the setting to unprotect the master branch in GitLab Online.
II. Setup project on the host PC
Here we will test pushing a Maven project to GitLab offline. The expectation is that Jenkins will receive an event from GitLab offline through webhooks and then perform a git push to GitLab online. I already have the source code on my local laptop, and it has been initialized with the GitLab offline project.
Perform a git push.
Check in the Jenkins console. There will be a message indicating that the Maven project was successfully built and pushed to GitLab online.