Jenkins is an open-source automation tool that helps in building, testing, and deploying software projects. Its main purpose is to provide Continuous Integration and Continuous Deployment pipelines, which ultimately lead to shorter development release cycles and improved product quality. The tool can be used with any programming language and can run on multiple platforms, including Windows, Linux, and macOS.
According to the Jenkins website:
Jenkins, originally founded in 2006 as “Hudson”, is one of the leading automation servers available. Using an extensible, plugin-based architecture, developers have created hundreds of plugins to adapt Jenkins to a multitude of build, test, and deployment automation workloads. In 2015, Jenkins surpassed 100,000 known installations, making it the most widely deployed automation server.
A Jenkins pipeline is a set of plugins used to create recurring automated workflows that constitute CI/CD pipelines. It enables the definition of a comprehensive set of events that occur in the code lifecycle, from building to testing and deployment.
Jenkins Pipeline is defined using a file called the Jenkinsfile. It is implemented as code using the Groovy Domain-specific language. The Jenkinsfile can be edited in an editor or on the Jenkins instance configuration page.
pipeline {
agent any
stages {
stage('Build') {
steps {
// compile code
// if successful, move build artifacts to the desired location
}
}
stage('Test') {
steps {
// check if artifacts are present in the correct location
// load test data into the database
// execute the test case
// continue only if tests passed
}
}uio
stage('Deploy') {
steps {
// Fetch tested code and deploy it to production
}
}
}
}
Jenkins offers two types of syntax to create pipelines. They are:
The Scripted Pipeline is the original pipeline syntax for Jenkins, and it is based on the Groovy scripting language. In this syntax, the entire workflow is defined in a single file called a Jenkinsfile, which is written in Groovy and executed by the Jenkins Pipeline plugin. Although the Scripted Pipeline provides more flexibility and control over the workflow, it can be more complex and verbose than the Declarative Pipeline.
Example for scripted pipeline:
node {
stage('Build') {
// Build the application
sh 'mvn clean install'
}
stage('Test') {
// Run the tests
sh 'mvn test'
}
stage('Deploy') {
// Deploy the application
sh 'deploy.sh'
}
}
The Declarative Pipeline is a newer feature in Jenkins that offers a more organized and concise method of defining pipelines. It uses a Groovy-based DSL (Domain-Specific Language) for pipeline configuration, which is based on the Groovy programming language. The most significant advantage of the Declarative Pipeline is its readability and simplicity, as it is intended to be more intuitive and less wordy than the Scripted Pipeline. Declarative Pipeline is a simplified and opinionated syntax on top of Jenkins Pipeline.
Example for declarative pipeline:
pipeline {
agent any
stages {
stage('Build') {
steps {
// Build the application
sh 'mvn clean install'
}
}
stage('Test') {
steps {
// Run the tests
sh 'mvn test'
}
}
stage('Deploy') {
steps {
// Deploy the application
sh 'deploy.sh'
}
}
}
}
Jenkins employs a Master-Slave architecture for managing distributed builds, where the Master and Slave communicate via TCP/IP protocol.
The main Jenkins server hosted is Master. The Jenkins Master Server is the primary control server responsible for arranging the pipeline's defined workflow. The jobs of this Master are as follows:
"A Slave" refers to a Java executable that can be executed on a remote machine like Linux or Windows VM.
Characteristics of the slave are:
A build parameter enables passing data into Jenkins jobs. It allows passing git branch names, secret credentials, hostnames, and ports.
In Jenkins, a parameterized build allows you to create flexible and customizable build jobs by defining parameters that can be passed when triggering a build.
By checking the box “This project is parameterized” on the General settings tab, any Jenkins job or pipeline can be parameterized.
NOTE: if you don’t have the Parameterized Build plugin installed in your Jenkins instance, you need to install it first. You can do this by going to “Manage Jenkins” => “Manage Plugins” => “Available” tab, searching for the “Parameterized Build” plugin, and installing it.
Let’s move on to the security part of Jenkins. They are:
Once it is done, we need to create the user in the Manage user.
Once the user is created in Manage Jenkins, search for Manage and Assign Roles. Click that and select Manage Roles. Under the Manage Roles page, we see two sections, where one is the Global roles and the other is the Item roles. Now, let’s start with the Global roles. Under the Global roles section, add a role with the name Employee and assign Read access inside the Overall category.
Under the Item roles section, add a role and a pattern to the role, for the role to add we need to provide any role that is understandable with the project. In this case, we have created Test_DEVs, and for the pattern, we have provided the RE of the projects to be authorized to this user.
Again, go to Manage Jenkins => Manage and Assign Roles => Assign Roles
Under Global roles, provide the created username, i.e. test_user and click on the Add button, the user will be added to the Global roles table and select the checkbox under the Employee.
Under the Item roles, provide the created username, i.e., test_user, and click on the Add button. The user will be added to the Item roles table, and select the checkbox under the Test-DEVs in front of test_user.
Thank you for stepping into the Jenkins world for the first time! You really should have a fair knowledge of Jenkins by now: how to install it and construct your first automated build processes. Remember that Jenkins is a powerful tool that can be applied in all different tasks of continuous integration and delivery, starting from code development and testing and finishing with application distribution. As you learn more and more about Jenkins, try combining it with other tools that are taking part in your development flow. Try new plugins or set up more complicated pipelines.