So far, all we've done is build job manually operated by admin. Then, how if we have a task that should be deployed based on certain time or let's say every minute for example. Jenkins has all the answer, so we can execute scheduled job. Since it's integrated with GitHub, we also can execute scheduled job only if there is a change on the repository. They work similar with cron (on Linux). Here we just need to decide the schedule.
Then, we can also disable job instead of delete it. Just in case you'll need it in the future, so you don't need to set up from begin. Beside that, we'll also play with parameter and variable. We'll really enjoy Jenkins features even we just started. Let's do it!
1. Create Scheduled Job
There are two kinds of this feature as I mentioned above. Those are:
- Build Periodically
I'll take the simple one by using five asterisks which it means build job every minute.
Slack Notifications:
- Poll SCM
Again I'll use five asterisks to check the repository every minute and build job if there is a change.
Then, I'll make a change by modifying the HTML file from Web 1 to Web 2.
Once again, I'll make a change like 3-4 minutes later. I just want to show you that it's different with periodically one.
Slack Notifications:
2. Environment Variables
To see all the "provided" variables, go to http://your-ip:port/env-vars.html
. There you can see the variables along with explanation like how we use it.
For example, I'll add echo command to define the variable output.
Beside that, we can also create our own variable and make it global. So, it can be used in all job. Go to Manage Jenkins => Configure System.
Then, I'll specify the variable like before.
3. Parameter
Here we'll try the Choice Parameter. I take a sample by using target hosts as options, by IP or hostname localhost.
I'll choose the IP (actually they're definitely same) and here is the output.
4. Disable Job
As I mentioned above, we can disable job if it's no longer needed but let's say maybe we need it later. We can easily checklist the Disable Project option. To enable it later, it's easy as we disable it. Just click the Enable button!
5. Keep Latest History
Currently we have some jobs and some of them already executed many times. For example, the scheduled one. As it run every minute, so it's going to be saved on build history as many as it run. By default, Jenkins will keep them all permanently and it can increase storage usage. So, Jenkins has log rotation feature which help you to keep the build history based on the number you want. In the example below, I want to keep one build history per day. I'll do it on Jenkins Job 1 considering it has the most build history.
6. Build Job Using URL
By using custom token, we can build job by accessing a custom URL. Custom URL I mean is the Jenkins job URL along with token mentioned right after the URL.
$ curl http://your-ip:port/job/job-name/build?token=your-token
For example, I'll do it for Jenkins Job 4 with secret token SECRET2022.
(This is just example, please never inform your token to anyone else outside your team).
$ curl http://108.137.62.173:8080/job/Jenkins%20Job%204/build?token=SECRET2022
Slack Notifications:
7. Job Grouping
(Mentioned as View in Jenkins) is a feature that allows us to create a group for listing some jobs based on category that you want. For example, I'll create view to list jobs that enable log rotation on it. We already enabled it on Jenkins Job 1, then I made some copy to Jenkins Job 4-6 that definitely enable log rotation as Jenkins Job 1. Beside that, we also can customize which value to be displayed.
From Dashboard, you can directly click the plus icon or click New View on the left hand side menu.
Alright, that's it for now! Any feedback are very welcome and thank you for coming. Follow me to get notified when new post is published by me! Thank you.
Let's continue to the next part!
Top comments (0)