XenonStack Recommends

DevOps

Implementing Pipeline as Code with Jenkins and Apache Groovy

Gursimran Singh | 28 August 2024

Overview of Jenkins and Apache Groovy

Jenkins is an open-source automation tool written in Java for continuous integration purposes. Jenkins gives the complete view of the application's software development life cycle, which starts from version control and goes till the packaging. Advantages of Using Jenkins:
  • Plug-in Availability
  • Easily Configurable
  • Jenkins supports the ability to build cloud-native applications.
  • Platform Independent
Groovy is giving its share by offering its Java-compatible programming language, mainly working on object-oriented functionality. Jenkins is also focused on java, so they, side by side, are giving immense support to give out the best outcome. Groovy helps in interacting with java objects and libraries, which are getting managed by Jenkins. Most organizations and companies are getting dependent upon the continuous trio, i.e., continuous integration, continuous deployment, and continuous delivery. Pipeline as code means you are just briefing the entire flow of the software development cycle in the code that could be managed and modified according to the business needs.

Jenkins Pipeline Execution Flow

The following points tell how a Jenkins Pipeline executes: 1. This pipeline starts from the version control system, which maintains the code followed by continuous integration. 2. The further proceeding includes the build phase, compile phase, testing, and review. After continuous integration, we have continuous delivery, which handles the application's delivery to the test servers. 3. After this, last but not least is continuous deployment, which is responsible for deploying the application to the production phase. The delivery flexibility associated with the pipeline as code is beneficial as it allows the client to make their own decisions on when and how to deliver/deploy.

Implementing Pipeline as Code

1. Initialize the Jenkins after installing Jenkins into your Linux environment.
  • systemctl Start Jenkins.
  • systemctl Enable Jenkins.
2. After this, you also have to initiate the docker as well by systemctl start docker. 3. As we also want to add groovy in our environment, add the Jenkins' groovy plugin. 4. Now move forward to the localhost:8180 to get access into the Jenkins where you will be asked for a username and password. 5. Now, your main task is to create jobs, including building the project, testing the project, and then deploying the project, and for every step we take, we have to create a separate job for that.
  • You can select the jobs from the dashboard and add them to the pipeline.
  • But before adding them to the pipeline, you have to attach a version control system or repository with it, for example, git.
  • Go to the configuration of jobs and go-to source code managing tab and select git.
  • You have to follow this same process for every job.
6. After adding jobs to the repository. Execute the following steps -
  • Add a source code.
  • Go to configuration, move to the build tab and select the execute shell from the drop-down list and move to the command shell where you will add scripts.
  • Each job has scripts, be it the build phase, test phase, and the deploy phase.
  • In the test job, you have to add the docker container, while in the case of the deploy job, you have to cross-check whether the image is already created or no.
  • Jenkins uses maven for installing packages that handle taking care of all dependencies.
  • mvn install (will install the dependencies).
7. Now, you have to connect all the jobs into a single pipeline to work in a continuum.
  • Use post-build actions from the configuration of each job and add the next job associated with every post.
  • Use ‘+’, after choosing for view name for providing the pipeline name.
  • Use various options available and also mention the job should be the first job. (Select apply after that) and after that RUN.
8. Now to cross-check whether the pipeline is working or not. localhost:8180

A Comprehensive Approach

Jenkins offers easy configurations as per the continuous integration requirements and continuous delivery and automates the software development process. To discover more about Jenkins, we advise taking the subsequent steps -