The Ops Community

Cover image for Custom Settings For Jenkins Job To Optimize Deployment
Nurul Ramadhona
Nurul Ramadhona

Posted on • Updated on

Custom Settings For Jenkins Job To Optimize Deployment

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.

Job Setting 1

Job Setting 2

Slack Notifications:

Job Setting 3

  • Poll SCM

Again I'll use five asterisks to check the repository every minute and build job if there is a change.

Job Setting 4

Then, I'll make a change by modifying the HTML file from Web 1 to Web 2.

Job Setting 5

Job Setting 6

Job Setting 7

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.

Job Setting 8

Job Setting 9

Job Setting 10

Slack Notifications:

Job Setting 11

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.

Job Setting 12

For example, I'll add echo command to define the variable output.

Job Setting 13

Job Setting 14

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.

Job Setting 15

Then, I'll specify the variable like before.

Job Setting 16

Job Setting 17

3. Parameter
Here we'll try the Choice Parameter. I take a sample by using target hosts as options, by IP or hostname localhost.

Job Setting 18

Job Setting 19

Job Setting 20

I'll choose the IP (actually they're definitely same) and here is the output.

Job Setting 21

Job Setting 22

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!

Job Setting 23

Job Setting 24

Job Setting 25

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.

Job Setting 26

Job Setting 27

Job Setting 28

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
Enter fullscreen mode Exit fullscreen mode

Job Setting 29

Slack Notifications:

Job Setting 30

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.

Job Setting 31

Job Setting 32

Job Setting 33

Job Setting 34

Job Setting 35

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!

Discussion (0)