This is part 2 of my series on deploying Jenkins to create an efficient CI/CD Pipeline. In part one, I covered installing and launching Jenkins on a CentOS instance. In this article, I will cover creating and scheduling Jobs.
Creating a Job
Log onto Jenkins and sign in as the admin user. Click ‘New Item’, and you’ll arrive at a screen like this:
Select ‘Freestyle project’, enter a valid name, and hit ‘OK’. If you want a more complex project, you can select one of the other options provided, but for my requirements for this demo, a freestyle project works just fine.
Enter a description for your project, the scroll to ‘Build’. Click ‘Add Build Step’, and select ‘Execute Shell’ as indicated in the image below.
In the build step, type “echo this is my first pipeline”, and Apply and Save. You should have something that looks like this:
Once you Save, click on the ‘Back to Dashboard button’, then select the pipeline you just created. You should be at a screen that looks like this:
Click ‘Build Now’ and wait for your project to finish building. In the ‘Build History’ Section you should see a ‘#1’ that corresponds to the build number, as shown below.
Click on the ‘#1’ and click ‘Console Output’. Your output should look similar to this:
Running Scripts in a Build
Running Scripts during a build step is very similar to what we have done above. Our first step is to create a script. I’ll be using a simple python script but this can be done for scripts in any language.
Copy Paste the following into “myScript.py” as a demo python script to run in your pipeline.
#!/usr/bin/env pythonfrom datetime import datetime
import sysif __name__ == '__main__':
print("this script was run at " + str(datetime.now()))
Save the file in the following path:
Execute your script by running. We need to give the script execute permissions so it can be run directly by invoking just the script name.
chmod +x /home/centos/myScript.py
The output should be something like this:
this script was run at 2020-07-19 21:36:30.041405
Right now, we cannot run this script from Jenkins because the Jenkins user
jenkins , doesn’t have permissions to execute this script. We need to give the Jenkins user the permissions it needs to find and execute this script using
chmod . Jenkins is neither the owner of the file, nor a member of the same group as the
centosuser. So, in the POSIX permission model, we have to give the needed permissions to others
So, we give the Jenkins user read
r, and execute
x permissions on the
centos directory. Do this by running:
chmod o+rx /home/centos
We now give the same permissions for our script as well.
chmod o+rx /home/centos/myScript.py
Now, go back into the Jenkins Project and add a new build step. Enter
and Apply and Save. Your new Console Output should look something like this:
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/MyProject
[MyProject] $ /bin/sh -xe /tmp/jenkins2330752522522290271.sh
+ echo this is a test pipeline
this is a test pipeline
this script was run at 2020-07-19 21:54:54.464272
We are now able to run scripts from Jenkins as part of a pipeline. We can take this one step further, and schedule these scripts to run using cron. Jenkins has built in support for cron scheduling and we can access it pretty easily.
For this example, I’ll schedule this script to run hourly. First, open your project, and click on ‘Configure’. Scroll to the ‘Build Triggers’ section and select ‘Build Periodically’. Enter the following to schedule Jenkins to run hourly: We use
H instead of
0 to make better use of limited resources. More about Jenkins’ cron configuration can be found here
H * * * *
Apply and Save and Jenkins will now run the script for you! Here is an example of what the output looks like after a few hours.
You now have a fully functioning pipeline that will run hourly!
In the next article, I’ll discuss receiving notifications for each build, and decorating the console output of the build.