Overview
In this article, we will go through a step-by-step process to create a Jenkins job that clones a project from GitLab. We will also configure email notifications to be sent once the job completes its execution. This article will serve as an introduction to Jenkins. Moving forward, we will build a CI/CD pipeline for our webMethods package deployment. Please note that this article does not include Jenkins/Git installation.
Prerequisite
- Basic Git commands knowledge.
- An Active Jenkins server.
- GitLab installed and configured.
What is Jenkins
Jenkins is an open-source automation server that helps you build, test, and deploy software projects. It allows developers to automate tasks such as running tests, building code, and deploying applications, making it easier to continuously integrate and deliver software updates.
Git Plugin Installation
- Log in to the Jenkins server as an Administrator.
- Navigate to Dashboard > Manage Jenkins > Plugins and install the Git server Plugin.
- Restart Jenkins Server.
Setup System Environment Properties
- Open environment property.
- Add the Git and cmd paths into the properties and save.
Email Server configuration
Since we will send email notifications based on the Jenkins job status, we need to configure the SMTP server details. You can also use Google’s SMTP server to send emails from your personal email ID.
- Navigate to Dashboard > Manage Jenkins > System in Jenkins.
- Configure the below property to send email notifications.
- System Admin e-mail address? ->your email address.
- SMTP server -> Your smtp server host.
- SMTP Port -> Your smtp port.
- Click on the Advanced option, then click on the Add button, provide the email credentials, and save the changes.
- You can use the E-mail Notification section to test the email server connectivity.
Create Jenkins Job
- Navigate to Jenkins Dashboard and click on New Item.
- Give a name to your project and use Pipeline as the project type, click ok.
- Select
- Add a string parameter like below which will allow the user to pass the project which should be cloned.
- Add Choice Parameter, which will allow the user to select the branch.
- Go to the pipeline section and use the below script to clone the git project and replace *Git Url* with your git URL and *Your Email ID* with the recipient email ID who will receive the email.
pipeline { agent any parameters { string(name: 'PROJECT_NAME', defaultValue: '', description: 'Git repository name') choice(name: 'BRANCH_NAME', choices: ['master', 'main','developer'], description: 'Branch to build') } stages { stage('Checkout') { steps { script { bat """ cd /d D:\\Jenkins git clone *Git Url*/%PROJECT_NAME%.git cd %PROJECT_NAME% git checkout %BRANCH_NAME% """ } } } // Add other stages here } post { success { emailext ( subject: "SUCCESS: Build ${env.JOB_NAME} ${env.BUILD_NUMBER}", body: "Build ${env.JOB_NAME} ${env.BUILD_NUMBER} was successful.\n\n" + "Details: ${env.BUILD_URL}", to: "*Your Email ID*" ) } failure { emailext ( subject: "FAILURE: Build ${env.JOB_NAME} ${env.BUILD_NUMBER}", body: "Build ${env.JOB_NAME} ${env.BUILD_NUMBER} failed.\n\n" + "Details: ${env.BUILD_URL}", to: "*Your Email ID*" ) } unstable { emailext ( subject: "UNSTABLE: Build ${env.JOB_NAME} ${env.BUILD_NUMBER}", body: "Build ${env.JOB_NAME} ${env.BUILD_NUMBER} is unstable.\n\n" + "Details: ${env.BUILD_URL}", to: "*Your Email ID*" ) } } }
- Break down :
- Pipeline Block.
pipeline { agent any ... }
This block defines the pipeline and specifies that it can run on any available agent (Jenkins node).
- Parameters Block.
parameters { string(name: 'PROJECT_NAME', defaultValue: '', description: 'Git repository name') choice(name: 'BRANCH_NAME', choices: ['master', 'main', 'developer'], description: 'Branch to build') }
PROJECT_NAME
: A string parameter to specify the name of the Git repository.BRANCH_NAME
: A choice parameter to select the branch to build from (master
,main
, ordeveloper
).
- Stages Block.
stages { stage('Checkout') { steps { script { bat """ cd /d D:\\Jenkins git clone *Git Url*/%PROJECT_NAME%.git cd %PROJECT_NAME% git checkout %BRANCH_NAME% """ } } } // Add other stages here }
stages
: Contains the different stages of the pipeline.stage('Checkout')
: The first stage named “Checkout”.steps
: The commands to execute during this stage.script
: A block to run a script (in this case, a batch script for Windows).bat
: Executes batch commands:- Change directory to
D:\Jenkins
. - Clone the Git repository specified by
PROJECT_NAME
. - Navigate into the cloned repository directory.
- Check out the branch specified by
BRANCH_NAME
.
- Change directory to
- Post Block.
post { success { emailext ( subject: "SUCCESS: Build ${env.JOB_NAME} ${env.BUILD_NUMBER}", body: "Build ${env.JOB_NAME} ${env.BUILD_NUMBER} was successful.\n\n" + "Details: ${env.BUILD_URL}", to: "*Your Email ID*" ) } failure { emailext ( subject: "FAILURE: Build ${env.JOB_NAME} ${env.BUILD_NUMBER}", body: "Build ${env.JOB_NAME} ${env.BUILD_NUMBER} failed.\n\n" + "Details: ${env.BUILD_URL}", to: "*Your Email ID*" ) } unstable { emailext ( subject: "UNSTABLE: Build ${env.JOB_NAME} ${env.BUILD_NUMBER}", body: "Build ${env.JOB_NAME} ${env.BUILD_NUMBER} is unstable.\n\n" + "Details: ${env.BUILD_URL}", to: "*Your Email ID*" ) } }
post
: Defines actions to take after the pipeline stages have been completed.success
: Actions to perform if the pipeline is successful.emailext
: Sends an email with the subject “SUCCESS”, the body containing build details.
failure
: Actions to perform if the pipeline fails.emailext
: Sends an email with the subject “FAILURE”, the body containing build details.
unstable
: Actions to perform if the pipeline is unstable.emailext
: Sends an email with the subject “UNSTABLE”, the body containing build details.
- Environment Variables
${env.JOB_NAME}
: The name of the Jenkins job.${env.BUILD_NUMBER}
: The build number.${env.BUILD_URL}
: The URL of the build detail
- Pipeline Block.
- Click on Save.
Testing
- Run the Jenkins job with the parameters, and you will see that the build completes successfully and an email notification is sent.
I hope you enjoyed this article and found it useful. Cheers!