This tutorial covers how to power up a Jenkins Docker Container and execute a Declarative logical Pipeline.
At the end of this tutorial, you will be able to execute a fully functional Groovy code that generates a random number and validates if it is below or above a threshold.
A logic will determine whether to pass or fail the job, based on the results.
More on Jenkins can be found on the official Jenkins website: https://jenkins.io/
Steps
Spin up a Jenkins Docker container
Initialize Jenkins
Create new Job
Write a Declarative Pipeline with Groovy
Build Periodically
Step 1 - Spin up a Jenkins Docker Container
The fastest end easiest way to get started with Jenkins is by running it with Docker.
For this tutorial, we are going to use the jenkins/jenkins Image.
Expose ports 8080 and 50000 with the -p argument.
Use the -v argument to assign a local disk location. I mapped it to ~/Downloads/jenkins but you can choose any other location.
Execute the following command to start Jenkins
docker run -p 8080:8080 -p 50000:50000 -v ~/Downloads/jenkins:/var/jenkins_home jenkins/jenkins
Step 2 - Initialize Jenkins
Navigate to http://localhost:8080 on your browser. You should see the following screen, where you should paste an initial admin password - it is located on your Jenkins terminal. Paste it in the text field and press Continue.
Jenkins will offer you to install recommended plugins - select this choice and press Continue.
Plugin installations will take a few minutes.
Create your profile on the Create First Admin User page and press Continue
Step 3 - Create a new Job
Once you completed all initial steps, you should be redirected to the Jenkins main page.
Select New Item at the top left.
Provide a meaningful name at the Enter an item name field
Select Pipeline
Press OK
At this point, you should be inside your new job configuration screen.
Step 4 - Write a Declarative Pipeline with Groovy
Scroll down to the Pipeline section. Copy and paste the code below and Save.
Click the Build Now button and your job should run.
Once your Job is running, you should see the Pipeline progress bar. here is an example -
Code Explanation
Script Logic
The code generates a random number between 0 - 100.
If that number is greater than the threshold (which is set to 50), then the job pass.
If it less, the job fails.
Declarative Pipeline Structure
Pipeline - The root of the hierarchy.
Agent - Declares which node to use. in our case we have set up only 1 Node which is the Master.
Stages - Contains all parts of the pipeline. Each Stage should have a name, which is going to be displayed in the Pipeline Progress Bar. Stage must have Steps.
Post - Will run no matter job is passed or failed. Here we usually perform post-execution logics, triggering Email notifications, and more.
getRandomNumber() - We can add methods just like in a regular groovy script. This method returns a random number between 0 - 100.
Step 5 - Build Periodically
You can schedule your job to run periodically.
Scroll to the Build Triggers section and check the Build Periodically checkbox.
Here you need to insert a Cron expression. If you don't have any experience with it, you can generate one using an online Cron Calculator. I recommend using Crontab.guru
In this example, I set it to run every 12 hours.
Comments