I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE COURSE

1. Introduction

In this article, we’ll cover various ways of scheduling jobs in Jenkins.

We’ll begin with scheduling a simple job that performs something as simple as printing a plain text message. And we’ll evolve the example into scheduling a job that gets automatically triggered by changes in an SCM repository such as GitHub, Bitbucket, etc.

2. Initial Setup

We assume that JDK and Maven have been installed in Global Tool Configuration with names as JDK9.0.1 and Maven3.5.2, respectively, on the Jenkins server.

It also assumes that we’ve got access to an SCM repository such as Bitbucket with a Maven project properly set up.

3. Scheduling a Simple Job

In the job configuration page, let’s scroll down straight to the Build Triggers section. Since we intend to create a straightforward job, let’s select the checkbox marked Build periodically. As soon as we select this checkbox, a Text Box is displayed with the Schedule label.

We have to provide value in a Cron-compliant format. There’s extensive information available on the page if we click the question mark next to the box.

Let’s type */2 * * * * here, which represents an interval of two minutes:

 

Upon tabbing out of the text box, we can see information right beneath the box. It tells us about when will the job run next.

Let’s save the job – in about two minutes, we should see the status of the first execution of the job:

Since we’ve configured the job to run every two minutes, we should see multiple build numbers when we go back to the job dashboard after waiting for some time.

4. Creating a Job That Polls SCM

Let’s move one step forward and create a job that pulls source code from SCM repository such as Bitbucket and performs a build.

Let’s create a new job as explained in the previous section, with a few modifications.

In the Build Triggers section, instead of selecting Build Periodically, let’s select Poll SCM. As soon as we do that, we should see a text box with Label Schedule.

Let’s type */5 * * * * in this box, which means we want to schedule the job to run every 5 minutes:

Let’s scroll up to Source Code Management section. Upon selecting the radio button next to Git, a new section appears labeled as Repositories.

This is where we need to configure details of our SCM repository. Let’s type the URL of the SCM repository in the Repository URL text field:

We also need to provide user credentials so that Jenkins can access the repository.

Let’s click the Add button next to Credentials, which will display a pop-up screen to create the user credentials.

Let’s select the Kind as Username with Password. We’ll have to type the username and password in the designated text fields:

On click of Add button, we’re taken back to the Source Code Management section.

Let’s select this user credential in the drop-down next to Credentials:

Now, the last thing we need to do is to set up the build script.

Let’s scroll down to Build section, click Add build step and select Execute Shell. Since we’re working on a Maven project in the SCM repository, we need to type mvn clean install, which will perform a Maven build.

Let’s try to understand what we’ve done here.

We’ve created a job that is scheduled to run every 5 minutes. The job’s been configured to pull source code from the master branch of the given Bitbucket repository.

It will use provided user credentials to log in to Bitbucket.

After pulling the source code, the job will execute the script containing a provided Maven command.

Now, if we save and wait approximately five minutes, we should see the build execution in Build History section on the job dashboard.

The Console Output should show the output of the Maven build. We can see in the console output that the source code has been pulled from Bitbucket and the command mvn clean install has been executed:

Since it’s a Maven build, the console output might be very long depending on the number of Maven dependencies that are downloaded.

But at the end of the output, we should see BUILD SUCCESS message.

5. Creating a Job That Uses Pipeline as Script

So far, we’ve seen how to create jobs that execute at the predefined scheduled time.

Now, let’s create a job that’s not bound to any particular schedule. Instead, we’ll configure it to automatically trigger whenever there is a new commit in SCM repository.

Going back to Jenkins dashboard, let’s click New Item. This time, instead of Freestyle project, we’ll select Pipeline. Let’s name this job PipelineAsScriptJob.

Upon clicking OK button, we’ll be taken to the pipeline configuration page. This page has several sections such as “General”, “Build Triggers”, “Advanced Project Options”, and “Pipeline”.

Let’s scroll down to “Build Triggers” section and the select checkbox next to Build when a change is pushed to Bitbucket. This option will be available only if we’ve installed the Bitbucket Plugin:

Let’s scroll down to Pipeline section. We need to select Pipeline Script in the drop-down next to Definition.

The Text Box just below this drop-down is waiting for the script to be placed in. There’re two ways of doing this.

We can either type the whole script, or we can make use of a utility provided by Jenkins, which is known as Pipeline Syntax.

Let’s choose the second option:

On click of Pipeline Syntax, as highlighted in the diagram above, a new tab opens up in the browser. This is a handy utility where we can specify the operation that we wish to perform, and the tool will generate the script in Groovy for us. We can then copy the script and paste it into the pipeline configuration.

Let’s select checkout: General SCM in the Sample Step drop-down. After providing the SCM Repo URL and user credentials, we need to click the Generate Pipeline Script button.

This generates the script in the text box:

Let’s copy this script and save it somewhere for later use.

On the same page, let’s scroll up and select withMaven: Provide Maven environment in the Sample Step drop-down. It must be noted here, that this option will be available only if Pipeline Maven Integration Plugin is installed.

We need to select the names of Maven and JDK installations next to the corresponding drop-downs. We need to click the Generate Pipeline Script button.

This generates the script in the text box:

Let’s save the script.

There are still a few more scripts that we need to generate. Let’s select node: Allocate node in the Sample Step drop-down, type master in the Label text field and click Generate Pipeline Script:

Let’s save the script.

And last one.

Let’s select stage: Stage in the Sample Step drop-down, type scm in the Stage Name text field and click Generate Pipeline Script:

Let’s save it.

It’s time to collate all the scripts generated so far and stitch them together:

node('master') {
    stage('scm') {
        checkout([$class: 'GitSCM', 
          branches: [[name: '*/master']], 
          doGenerateSubmoduleConfigurations: false, 
          extensions: [], 
          submoduleCfg: [], 
          userRemoteConfigs: [[credentialsId: 'e50f564f-fbb7-4660-9c95-52dc93fa26f7', 
          url: 'https://[email protected]/projects/springpocrepo.git']]])
    }

    stage('build') {
        withMaven(jdk: 'JDK9.0.1', maven: 'Maven3.5.2') {
            sh 'mvn clean install'
        }
    }
}

The first statement, node(‘master’) in the script above, indicates that the job will be executed on a node named master, which is the default node on the Jenkins server.

Let’s copy the above script to the Text Box in Pipeline section:

Let’s save this configuration.

Now, there’s only one thing that’s left out. We need to configure a Webhook in Bitbucket. The Webhook is responsible for sending out a push notification to Jenkins server, whenever a particular event takes place in Bitbucket.

Let’s take a look at how to configure it.

Let’s log in to Bitbucket and select a repository. We should see an option Settings on the left-hand side column. Let’s click it, and we should see an option Webhooks in WORKFLOW section. Let’s create a new Webhook.

We need to provide some name to this webhook in the Title field. We also need to provide a URL for the webhook in the URL field. This URL needs to point to one particular REST API Endpoint provided by Jenkins, and that endpoint is bitbucket-hook.

It must be noted that the URL MUST end with a trailing slash:

 

While configuring the Webhook above, we’ve selected option Repository push. This means Bitbucket will send a notification to Jenkins server whenever a push happens.

This behavior can be modified; there are several different options to choose from.

For now, let’s go with the default option,i.e., Repository Push.

We can setup webhook in Github, too; here‘s some helpful information on how to configure that.

Long story short: we’ve created a pipeline script in Groovy language – that should pull the source code from the master branch of the provided SCM repository (using the provided user credentials), whenever there’s a push in the repository and then execute mvn clean install command on the Jenkins server.

It must be noted that this job’s not going to run at any particular time. Instead, it’s going to wait till there’s a push in the master branch of the SCM repository.

As soon as there’s a new push event, we’ll see a new execution in the “Build History” section on the Jenkins job dashboard.

We should see a new build being initiated with a pending status next to it.

In a few seconds, this build should start execution, and we’ll be able to see the complete log in Console Output.

The first statement in the console output says “Started by Bitbucket push by..” – this means that the build was triggered automatically when a Push took place in Bitbucket:

 

If all goes well, the build should complete successfully.

6. Create a Job That Uses Jenkinsfile

It’s possible NOT to write any script in the Jenkins pipeline and still achieve build execution being triggered by the Bitbucket Webhook.

To do so, we have to create a new file in Bitbucket and name it as Jenkinsfile. The pipeline script needs to be transferred to this Jenkinsfile with a slight modification. Let’s see exacttly how to do that.

We’ll create a new pipeline in Jenkins and name it PipelineWithJenkinsfile.

On the pipeline configuration page, we’ll select Pipeline script from SCM next to Definition in the Pipeline section. We’ll see a drop-down with different options next to SCM. Let’s choose Git from the drop-down.

We then have to provide the URL of Bitbucket repository and user credentials. Let’s ensure that the text field next to Script Path contains the default value,i.e., Jenkinsfile:

 

As far as Jenkins is concerned, that’s all we need to configure.

However, we have to create the Jenkinsfile in the repository; so, let’s create a new text file, name it as Jenkinsfile and use this simple groovy script:

node('master') {
    stage('scm') {
        checkout scm
    }
    stage('build') {
        withMaven(jdk: 'JDK9.0.1', maven: 'Maven3.5.2') {
            sh 'mvn clean install'
        }
    }
}

This script is almost the same as the pipeline script that we created in the earlier section, with just one modification. The statement in stage(‘scm’) doesn’t need the URL and user credentials information. Instead, all it needs is checkout scm.

And that’s it. The moment we commit this file into Bitbucket, it’ll trigger the build in Jenkins – and we should see the build being triggered in the Build History.

The only difference between this section and the earlier one is that we defined the pipeline script in the Bitbucket repository.

So, the build script is part of the source code of the project that needs to be built. We’re not maintaining the script in Jenkins itself.

Instead, Jenkins has knowledge of the SCM repository details and the script file. Whenever there is a push to this repository, the script in Jenkinsfile gets executed on the Jenkins server.

7. Conclusion

In this tutorial, we’ve seen how jobs can be configured and scheduled in Jenkins using various strategies.

We’ve also seen how to configure a job in Jenkins so that it’s automatically triggered, based on certain actions performed in SCM repository such as Bitbucket.

I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE LESSONS