What is Jenkins?
Jenkins is an open-source automation tool programmed natively in Java that aims to facilitate continuous integration and continuous development (CI/CD) processes .
Most code-based products need to be constantly updated to add improvements and fix possible programming errors. To ensure a quality product, it is important that certain aspects be reviewed before updating the code that is in operation.
For example, a common practice is that before the code is updated and new functionality is added, it has to pass some tests. If they are not passed, the update cannot be incorporated.
This helps to not update the code until it works perfectly. These procedures are called CI/CD and are normally They are carried out by professionals called DevOps , whose name comes from English: Development Operations.
Jenkins allows you to establish all the stages that the code will have to pass before it can be deployed in production. The first phase is build where an environment is generated where the code can be executed and tested.
Normally, this construction stage is carried out within a Docker container, in order to run the program in an isolated environment and with the necessary dependencies.
Once the environment where the program can be executed has been built, it comes the testing phase . In this stage, unit and integration tests are executed to ensure that the code is ready for deployment to the production server.
Finally, once the tests have been passed, the code can be updated with the new functionality. As we will see later, other stages can be defined within the Jenkins pipeline, such as reviewing the programming style to comply with standards.
Jenkins integration with cloud repositories
In most IT projects, Jenkins is always integrated with a version control repository in the cloud, such as Github, Gitlab or Bitbucket.
When we work with code, it is important to control the different versions of it using what are known as version control systems or VCS (Version Control System). One of the best known is Github.
These systems allow the code to be divided into what are known as branches. Branches allow you to branch the code and work with different versions of it. For example, if I want to incorporate a new functionality into my program, I will not do it in the deployment version, but rather I will do it in a branch and when I am sure that everything is correct then I will join it with the master branch, where is the code that will be uploaded to the production server.
This is where Jenkins' role comes into play. It is at this point, when we want to join our functionality within our branch with the main code that Jenkins will be in charge of verifying that the code I have developed is suitable to put into production.
At this point the Jenkins pipeline will be executed. If the different steps or stages of the pipeline fail, the new code will have to be reviewed again before it can be joined with the main code for updating.
What is a pipeline in Jenkins?
The pipeline is the set of defined steps that the code will have to pass before it can be sent to the production server. As we have briefly mentioned before, there are three basic phases or stages. These are: build, test and deploy.
- Build : The environment is prepared with the dependencies and environment variables necessary for the effective execution of the code. It typically runs in a Docker container.
- Test : at this point the code is executed within the container and the different unit and integration tests are passed that ensure good functionality and quality of the code.
- Deploy - The new functionality is merged with the main code and deployed to production.
How to create the Jenkins pipeline
There are several ways to specify the different stages of the continuous integration process. With Jenkins we can do it through the UI it provides us or create it directly in a file called Jenkinsfile.
The most common thing is to use the Jenkinsfile to specify each of the stages and conditions that will have to be executed within the workflow.
Below you can see an example of how a pipeline would be defined within the Jenkinsfile:
xxxxxxxxxx
pipeline {
agent none
stages {
stage('Build') {
agent {
docker {
image 'python:3.8-alpine'
}
}
steps {
sh 'python3.8 -m py_compile sources/prog.py sources/calc.py'
stash(name: 'compiled-results', includes: 'sources/*.py*')
}
}
}
}
The first block should always start with the pipeline command. If it is written outside the block, an error will always appear since we cannot define logic outside the pipeline block.
After pipeline we see the agent instruction. Agent informs Jenkins where each phase of continuous integration will be executed. We see the first agent that says none. This means that the agents will all run in the same place.
Next we see stages which incorporates the different stages or stages that will take place. Within each stage we have different steps. The steps are the different instructions that we have to give to Jenkins so that he can complete the stage satisfactorily.
Important: The steps block is unique for each stage. We cannot define more than one.
There is another important command that we can define within the Jenkinsfile pipeline called post. Within post we will put the necessary instructions that have to be carried out after the Jenkins pipeline has been executed.
Normally it is used to clean and delete data from files or databases that no longer interest us.
What we have seen now is simply a summary of what pipelines are within Jenkins. However, in order to effectively execute these workflows, more knowledge is required and looking at step-by-step manuals on how to perform each of the steps.
Advantages of using Jenkins for CI/CD
Jenkins has many advantages and for that reason it is one of the most used continuous integration tools within the development and programming sector:
- Jenkins is open source and supported by a large community of developers.
- It is a very versatile tool thanks to the large number of integrations and plugins that have been designed to extend its functionality.
- Being open-source, it is a completely free tool.