Build & Delivery CI/CD Pipeline for your Maven project with the help of Jenkins and Ngrok
Hello everyone,
In this article, you can find a step-by-step guide on how to set up your Maven project with Jenkins. Once you push the code to your GitHub repository through Git, it automatically builds your project and provides you with the results. Similarly, you can create different jobs to set up your CI/CD pipeline. We'll go through it step by step.
Checked Project Setup
So, let's begin with a Maven project. In the screenshot, you can see I have a Selenium project in my Eclipse. It could be any project or framework; it doesn't depend on the archetype or language. However, I prefer Quickstart 1.4, so I'm using that.
You can use the Surefire plugin to run your Maven project using the command line, to check for successful builds and error-free code. Make sure you have downloaded Maven and set the main folder path and bin path in the environment variables, just like you did for Java. Now, Simply add the following plugin to your POM. For more details, visit this link: https://maven.apache.org/surefire/maven-surefire-plugin/usage.html.
For the first time, run the following commands from the project directory step by step:-
If everything is working as expected, you can expect logs like these.
Push the Code to GitHub
Since everyone is familiar with Git and GitHub, I won't explain much here. Just install Git Bash or use plugins from Eclipse to push the code. After that, run the following commands to push the code into your repository.
You can modify the commands based on the return logs from Git Bash if you encounter any setup issues or bugs.
Setup WebHook to Jenkins
Now download and install Jenkins by following this link: https://www.jenkins.io/doc/book/installing/windows/. Once you have successfully installed it, create a new job and select the type "Freestyle project."
Step1:-
In these screenshots, you can see I have a Freestyle project named "Autotrigger." Once you have created it, navigate to the "Configure" section. Then, scroll down to the "Source Code Management" section and select Git. Paste your GitHub repository URL. Next, move to the "Build Triggers" section. Under "Build Triggers," select "GitHub hook trigger for GITScm polling." Finally, click on "Apply" and then "Save."
Step2:-
You may have noticed that your Jenkins is running on localhost. When attempting to run Jenkins via auto triggers, it requires a valid hostname instead of 'localhost'. To address this issue, we are introducing ngrok. With ngrok, you can easily configure webhooks.
Just follow these steps:
Now, 90 percent of our work is done. We need to set up a webhook on GitHub. Follow these steps again:-
Now, once you modify your code and push it to GitHub, you will see an automatic build running under the Autotrigger FreeStyle project. From there, you can also check the console. If everything is working as expected it will return build success.
Pipeline
Build Pipeline:-
Now, what we've done so far is just a part of CI/CD. In a typical workspace, the scenario can be quite different. In DevOps, there could be multiple servers and environments, each with different names like Stage Server, Dev Server, Deployment Server, Release Server, Test Plans, Web Apps, Repositories, Commits, and more. There are no strict boundaries; any DevOps engineer can design pipelines in their own way or based on the requirements. The entire process is entitled as DevOps from operation to deployment, follow this link for more details:- https://www.browserstack.com/guide/devops-lifecycle. However, there are primarily four types of phases present in the pipeline.....(You can set different names as well)
Our main focus is to deliver high-quality software, tailored to meet business and user requirements.
Now, let's build the pipeline step by step in Jenkins. I will explain the settings for one job, and then you can replicate them for the rest. The most important concepts to remember here is Upstream and Downstream. Upstream refers to the initial job; after executing the upstream job, the downstream job will trigger. If there are any issues present in the Upstream job, the pipeline will automatically stop.
Before creating pipelines, add two or more plugins from the plugin manager:
Try to download pipeline-related plugins so you won't need to worry about any issues later on. Now, after installing the plugins, restart Jenkins and click on 'New Item', similar to the auto-trigger setup we created earlier. Create four jobs: Build Jobs, Deploy Jobs, Test Jobs, and Release Jobs. Now, configure each one of them one by one.
Build Job:- The Build job is the first job, so there shouldn't be any upstream job connected to it. However, after the Build job, we need to push the code to a different server for Deployment. Therefore, the downstream job from the Build job will be the Deployment job.
Now, to configure the build steps, choose 'Execute Windows batch command' and make sure to include 'cd %WORKSPACE%' for validation between the two jobs. Here, the DevOps engineer is using some commands to track their process. The other settings will remain the same, such as source code management and Build Triggers (refer to the configuration of the Auto Trigger FreeStyle project). Now hit apply and save the build job.
Deploy Job:- In the Deploy job, the rest of the features remain the same. You just need to choose under Build Triggers, "Build after other projects are built" and select "Trigger only if build is stable" and in the "Projects to watch" field, simply type "Build_Job" and select it.
The rest of the jobs are the same; just mention the following upstream jobs in the "Projects to watch" field.
Now our jobs are ready, we need to convert them into a pipeline. To do that, simply go to the view, type a suitable name for your pipeline, choose the "Build Pipeline radio button," and create it.
Now there are no configurations left. Just come under "Configure" now, then go to "Pipeline Flow" and mention the initial job. It will automatically pull the other jobs because they are connected as upstream and downstream.
Select the number of display builds as 5 then hit apply and save.
Now our pipeline is ready. In the same way, you can push the code, and you will see it running automatically from one job to another, just like water flowing seamlessly. If there is an issue in any phase, the next flow will be stopped.
Delivery Pipeline
This is just like a report. You can create it in the same way as a build pipeline. Choose "Delivery Pipeline" at the time of creation, and in the configuration, go to Pipelines. Under Components, select the initial job as the build job.
I explained the entire process with the help of one GitHub repo, although it could have involved different servers, environments, etc. So, the overall process would be similar—I won't say exactly the same, as I'm not a DevOps engineer, but closely similar. If you have any doubts, please feel free to ping me personally or comment below, and I'll happily resolve them. Goodbye and Happy Testing!
Software Developer at @solulab || Node js || Next js || AWS || Docker || Mongodb || MySQL || DevOps
7 个月Ngrok serves as an invaluable tool for facilitating external accessibility to our internal systems, enabling seamless exposure of locally hosted/ running APIs to the broader internet.
Very clean and clear