3. DevOps : Jenkins
Hey Guys,
So far, we have looked into Basics of DevOps and Git. Today, let's take a step ahead and start learning about a very important and quite useful tool : Jenkins
Jenkins is a popular open-source automation server used primarily for continuous integration (CI) and continuous delivery (CD). Jenkins automates the building, testing, and deployment of software projects, making it easier for developers to integrate changes and deliver software efficiently. These tasks can be automated by configuring Jenkins through its UI or by using a file called Jenkinsfile to define the steps, which Jenkins will then execute.
In this article we are going to look into what is Jenkins files, how to write one and certain good practices. Here I am assuming that Jenkins installation and setup is done. Lets get started.
What is Jenkinsfile?
As mentioned above, in Jenkinsfile, we write down the step to perform like, build and package, creating docker image, uploading to docker hub or nexus , deploy on server or any container platform and test. Once you have written these steps and pushed the code, you can navigate to Jenkins where your project will be listed. There will be a 'Build Now' button to start the build process.
How to write Jenkinsfile?
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
}
}
This is the example of Declarative Pipeline which is easier to understand and read. Note that you can achieve same result by configuring the Jenkins using its UI. Given that pipelines can get big and might need constant modifications, modifying a file is always easier. Also the fact that we can reuse same file. In this example, we got :
Most Useful Plugins:
There are over 1900+ plugins for Jenkins. Below are few most commonly used:
Useful Global Variable/Object/Methods:
While writing pipeline code, decisions about executing certain steps are often made at runtime using conditional statements. These conditions can be based on useful global variables provided by Jenkins.
currentBuild: Provides detailed information and control over the currently running build
env: Provides access to environment variables available to the build process
scm: Provides access to source code management (SCM) details for the current build
param: Used to access parameters passed to the Jenkins job or pipeline
manager: Interact with Jenkins and perform various tasks related to logging, build actions etc.
trigger: Start the execution of a Jenkins job or pipeline based on certain events or schedules.
After reviewing the details, it's clear that we can draft an effective Jenkinsfile using the information provided. As Jenkins continues to evolve and expand, this article has only scratched the surface. I hope it has offered valuable insights into leveraging Jenkins more effectively and gets you started with it.
If you have any further questions or need additional clarification, please feel free to reach out. I've included links to my previous two articles in the first section for additional context.
Thank you for reading!