Say Goodbye to Manual CI/CD: Automate Project Management with Jenkins, GitHub and Groovy Scripts
What is Jenkins
Jenkins is one of the most popular automation tool used by DevOps engineer for continuous integration and continuous deployment, which also enables to build, integrate and test code as soon as it is committed to GitHub repository.
What is GitHub
GitHub is a web-based platform that provides version control using Git, enabling developers to store, manage, and collaborate on code projects in a centralized remote server. Developers can access their work from anywhere. GitHub also supports integration with CI/CD pipelines, allowing for seamless automation of testing and deployment.
Pre-requisites
To be familiar with GitHub, including how to create an access token and navigate the GitHub UI,
To set up a Jenkins server, install the necessary plugins, and create a new user in the Jenkins server.
Basics of GIT for SCM.
Let’s get started with practical
Creating a Personal Access Token: Generate a token from GitHub with the necessary permissions, including
org-hook
,repo-hook
,repo
,user
, andnotifications
. This token will be used to authenticate the Jenkins server with the GitHub server.Add a user in the Jenkins server: Use the GitHub personal access token as the password for the new user. Username must your GitHub account name
Remember the ID provided as we will be used later.Plugins: Install the DSL (Domain Specific Language) plugin. This plugin provides a programmatic way to define and configure Jenkins jobs, making the process more time-efficient and less prone to errors.
Creating and configuring Job: Create Freestyle Job named as
SeedJob
.In discard old build set Max # of builds to keep as 1. This means, Setting Max # of builds to keep to 1 in a Jenkins seed job means that the system will only retain the most recent build artifact for each created job.
Set git, by providing repository link, GitHub credentials and branch name
Add build step, this will take all files with .groovy extension. This will help to integrate our GitHub account with Jenkins Server
Set Action for removed jobs as delete, this will delete previously generated job which is not referenced anymore in groovy files only for the last Job DSL build step.
Set Action for removed views as delete, this will delete previously generated view is not referenced anymore in groovy files only for the last Job DSL build step.
Set Action for removed config file as delete, this will delete previously generated config files is not referenced anymore in groovy files only for the last Job DSL build step.
Approval Script: When you run the build for the first time, it will likely fail. First, approve the script in the Manage Plugins section, then rebuild the job to successfully create the build artifact.
Adding our project in groovy files: To add our project using Groovy files, define the organization and URL of the project. In this case,
ABHI
represents my GitHub username, whilescmAbhi
is the GitHub repository URL.Add the credentials defined in Jenkins. Here, we will use the ID that we assigned to the credentials in Jenkins.
Configuring the Groovy File: Add our project to the Groovy file. Here, "project" refers to the name of the repository in our GitHub. Setting the variable as an empty string means that no specific action will be taken.
Push all the changes to GitHub.
Approval Script: Approve the script from the Jenkins server, then trigger the build for the job. After that, navigate to the console output to view the results.
From dashboard we can verify that our project is successfully added to our Jenkins server.
Note:
SeedJob: SeedJob in Jenkins are specialized jobs that serve as template or blueprints for creating other Jobs within Jenkins instance. They provide a way to standardize and automate the process of setting up new jobs, ensuring consistency and reducing manual configuration
While setting the git repository URL if you get following error, Failed to connect to repository : Error performing git command: git ls-remote -h <repo-url> HEAD
If encountered with this error message, make sure git is installed in serverERROR: (views.groovy, line 20) plugin 'build-monitor-plugin' needs to be installed. As build monitor plugins is not installed hence this error is thrown.
Conclusion:
All jobs created are stored on the GitHub server. In the event that our Jenkins server crashes, we can easily rebuild all the jobs by simply running the Groovy files.
Adding or deleting jobs can be done by making changes to the Groovy files, instead of manually adjusting them through the Jenkins server.
If we need to migrate jobs from one server to another, we can do so by running the Groovy files on the newly created server.
Resource:
To know about Jenkins plugins: https://plugins.jenkins.io/
Setting up Jenkins server on Linux: https://www.digitalocean.com/community/tutorials/how-to-install-jenkins-on-ubuntu-22-04
Creating GitHub access token: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens
Sample Groovy file repository: https://github.com/agileguru/jenkins_job_maker
Subscribe to my newsletter
Read articles from Abhishek Yadav directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Abhishek Yadav
Abhishek Yadav
IT professional skilled in cloud computing, DevOps, and automation, specializing in scalable infrastructure, CI/CD, and workflow optimization across AWS, GCP, and container orchestration. Core Competencies: Operating Systems: Windows, Linux Version Control: Git Cloud Platforms: AWS, GCP Containers & Orchestration: Docker, Kubernetes CI/CD: Jenkins Infrastructure as Code (IaC): Terraform Monitoring & Alerting: Prometheus, Grafana Ticketing & Issue Management: Ivanti Certifications: AWS Certified Cloud Practitioner Let's Connect: Email: 07abhiyadav07@gmail.com GitHub: https://github.com/yadav-7abhishek hashnode: https://hashnode.com/@7abhishek