Jenkins - A begineer Guide
Jenkins is an open-source automation tool written in Java that helps automate various tasks in software development, primarily in the domain of continuous integration and continuous delivery (CI/CD). It provides a platform for building, testing, and deploying software projects, allowing developers to automate repetitive tasks and streamline the software delivery process.
Use Cases of Jenkins:
Continuous Integration (CI): Jenkins excels in automating the integration of code changes from multiple developers into a shared repository. It can automatically trigger builds and tests whenever changes are pushed to the repository, enabling quick feedback on code quality and preventing integration issues.
Continuous Delivery (CD): Jenkins enables the continuous delivery of software by automating the deployment process. It can deploy applications to various environments, such as development, staging, and production, based on predefined configurations. This helps ensure consistent and reliable deployments.
Automated Testing: Jenkins can integrate with different testing frameworks, allowing automated tests to be executed as part of the build process. It helps identify bugs and issues early in the development cycle, ensuring the quality of the software.
Deployment Orchestration: Jenkins can be used to automate the deployment of applications to different servers and environments. It streamlines the process of deploying and managing software across multiple servers or cloud platforms.
Scheduled Jobs and Batch Processing: Jenkins can be configured to perform routine tasks at scheduled intervals, such as running backups, generating reports, or executing batch jobs. This helps automate repetitive tasks and frees up developers’ time for more critical activities.
Different Types of Jenkins Projects:
Freestyle Project: This is the most basic project type in Jenkins. It provides a lot of flexibility and allows users to define build steps and configurations based on their specific requirements. It is suitable for simple or custom-build processes.
Pipeline Project: Jenkins Pipeline is a powerful and extensible way to define the build, test, and deployment workflows as code. It uses a domain-specific language (DSL) or a declarative syntax to define the entire build pipeline, including stages, steps, and conditions. Pipeline projects are highly versatile and recommended for complex build processes and CD workflows.
Multibranch Pipeline: This type of project is useful when you have multiple branches in your source code repository, and you want to build and test each branch separately. Jenkins automatically detects new branches and creates build pipelines for them, providing visibility into the status of different branches.
Benefits and Use Cases of Each Project Type:
- Freestyle Project:
Flexibility: Freestyle projects provide the highest level of flexibility in Jenkins. You have full control over the configuration and build steps, allowing you to customize the project to meet your specific requirements.
Easy setup: Freestyle projects are relatively simple to set up, making them ideal for small projects or quick automation tasks.
User-friendly interface: The user interface for configuring freestyle projects is intuitive and straightforward, making it easy for beginners to get started.
2. Pipeline Project:
- Continuous Delivery as code: Jenkins Pipeline allows you to define your entire build, test, and deployment workflows as code.
This provides several benefits:
Version control: Pipelines can be stored in a version control system, enabling easy collaboration, review, and rollback of changes.
Reusability: Pipeline code can be shared and reused across multiple projects, ensuring consistency and reducing duplication.
Scalability: Pipelines are highly scalable and can handle complex workflows with multiple stages, parallel execution, and conditional logic.
- Visualization and monitoring: Pipeline projects provide a visual representation of the entire workflow, making it easy to track the progress of builds and deployments. Jenkins provides detailed logs and reports, helping identify issues and bottlenecks in the pipeline.
3. Multibranch Pipeline:
Independent branch testing: With multibranch pipelines, each branch in your source code repository can have its own build and test pipeline. This allows for independent testing and validation of changes made in different branches, ensuring branch-specific issues are caught early.
Automatic branch detection: Jenkins automatically detects new branches and creates build pipelines for them, reducing the manual effort required to set up pipelines for each branch.
Streamlined management: Multibranch pipelines provide a consolidated view of the build status across all branches, making it easy to monitor and manage multiple branches simultaneously.
Overall, Jenkins is a versatile automation tool that can be adapted to various software development scenarios. Its extensive plugin ecosystem further enhances its capabilities, making it a popular choice for CI/CD and automation tasks in the industry.
Long time no see! After a long time, I got a chance to release and continue the complete Jenkins tutorial. In this article, we will learn how to use Git in our pipelines. Using Git is a most important topic in CI/CD pipelines as we store everything in Git, our source codes and pipeline codes themselves. To work with Git, you need the Jenkins Git plugin. I’ve prepared everything you need in the following Jenkins stack. The following Jenkins stack is available for both Docker and Kubernetes environments.
Jenkins Pipeline in Git repository:
When you’re about to create a new Jenkins job, pipeline, you can connect Jenkins to a specific Git repository to read your pipeline code, the Jenkinsfile. In the case of CI pipelines, your Jenkinsfile is along with your source code, and Jenkins will clone your Git repository, which consists of your application source code and Jenkinsfile. You need your source code in the Jenkins CI pipeline to be able to build it, test it, and release it. In CD pipelines, you may have multiple things like Kubernetes manifests, GitOps manifests, Ansible playbooks, Docker-compose files, Terraform codes, Pulumi codes, etc. along with your Jenkinsfile.
In the Pipeline section you can configure:
Git repository URL address
Credentials to clone private repository (User/Pass or SSH)
Target branch or Ref name
Script path, Jenkins pipeline file (Default: Jenkinsfile)
It’s super easy, but what if you want to clone the Git repository inside your pipeline or commit and push something into Git repo using pipeline? Jenkins provides you with a couple of ways to clone a Git repository inside the pipeline. The straight forward way is to use the Git plugin.
Clone Git repository in Jenkins Pipeline:
git(
url: "REPO_URL_OR_SSH_URI_ADDRESS",
branch: "REPO_BRANCH_NAME",
credentialsId: "REPO_CREDENTIAL_ID_IN_JENKINS",
changelog: true,
poll: true
)
For public repositories, you don’t need to specify credentialsId
option.
If poll
option is set to true, repo will be polled for changes.
If changelog
option is set to true, changelog will be computed.
Commit and Push to Git using Jenkins Pipeline:
Now it’s time to commit something new and push it back to the Git repository. Why do we need it? In many cases, you may need to add, commit and push from your pipeline. For example, updating GitOps manifests after building the image of the new application version, or pushing terraform.tfplan file into artifact repo.
First you need to configure Git plugin:
Dashboard > Manage Jenkins > Configure System > Git plugin
To be able to push to the GitHub repository, you need to have a token. To create a token, go to Developer Settings in your GitHub account.
Then back to Jenkins and create a new Username/Password credential.
Manage Jenkins > Credentials > System > Global credentials
For GitHub, write TOKEN in the Password field.
Now go to your pipeline, commit and push something to the Git repository. It doesn’t matter if you’ve cloned Git repository in the pipeline or Jenkins has cloned it because of Jenkinsfile. You just need a Git repository with .git
directory and credentials to push to the origin.
withCredentials
is a pipeline step to inject credentials to being used by applications in its block. As you can see, I injected a gitUsernamePassword
to be able to run git push
command. I will explain it more in future articles.
If you are faced with this error, you should solve it by running git config
command inside the Jenkins container.
git config --global user.name "SurendarSv"
git config --global user.email "surendar.sv024@gmail.com"
Now, check your Git repository. You should be able to see a new commit pushed by Jenkins pipeline.
Conclusion:
Using Git repositories in CI/CD pipelines is a daily task you should learn well. In the following articles, we will work more with Git repositories as everything we have is stored in Git repositories. To be honest, I’m super busy these days, but I promise to spend more time writing new articles.
Will continue addiction content to this article related to jenkins ci/cd