Script Jobs in ScriptRunner allow you to script actions that will be run automatically at a specified time interval. We use crontab syntax to schedule tasks. You can access script jobs by clicking on:

Administration Menu > "Add-ons"

In the menu bar on the left hand side, under the SR heading you should see the "Script Jobs" option. If you click this it will take you to the Script Jobs menu.

ScriptJobMenu

Basic Script Job usage

Add a Custom Script Job

A custom script job allows you to write your own script that will be run at a timed interval. Let’s take a look at how we set one up!

Step One

Click the "Add New Item" button to reveal a list of the Script Jobs you can create.

Step Two

Click on "Custom scheduled job" from the list that appears.

ScriptJobAddNewItem

Step Three

Populate the form as appropriate to your use case.

ScriptJobAddCustomScriptJob
Field Description

User

The user that we want the script to run as.

Note

A reference that describes what your scheduled job is doing. This will be the text visible in the script job list.

Script

You can add either an inline script or reference to a script that exists in your "Script Root". This will be the action that you want to be done at a scheduled interval.

Cron Expression

There are built-in examples of cron expressions that you can put in by clicking the "Expand Examples" menu.

Step Four

Once the form is complete you can save the job by clicking the "Add" button.

The job WON’T be active until you click the "Add" button
You can test your script, at any time, by clicking the "Run now" button

Once the save is complete you should now see your scheduled job in the list of scheduled jobs in the main menu.

ScriptJobList

Edit a Scheduled Job

Sometimes you need to make a change to a script job that already exists.

  1. Click the actions menu for the job that you wish to edit.

  2. Click "edit" from the menu that is presented.

Delete a Scheduled Job

In some cases you may wish to remove jobs that you no longer require.

  1. Click the actions menu for the job that you wish to edit.

  2. Click "delete" from the menu that is presented.

The Build SLA reminder Script Job

In this example we are going to use a Script Job to regularly add a comment to all build plans that are currently failing. For example, check for builds that have been in a failing state for more than one day.

We will then configure Bamboo build plan notifications so that when the comment is added, a notification is then sent to HipChat.

Adding the Script Job

First go to the Script Jobs overview page as described above (this time selecting "Build SLA reminder" from the "Add New Item" menu) and fill in the details as shown in the screenshot below. Making sure to use whichever user you plan to run this Script Job as, rather than "Admin". The cron expression in this case means "Run at half past midnight every day".

failed builds scripted job

Once the Script Job has been saved, you should start to see comments like the below start appearing on failing build plans.

failed build comment

Configuring the Bamboo notifications

Now that the Script Job is monitoring the failing builds and commenting on those that have not been fixed within a specified amount of time (one day in this example), we can next look at configuring the relevant build plan notification settings.

As shown in the screenshot below, select Event "Comment Added" and Recipient type "Hipchat".

The API Token can be provided by your HipChat administrator and the Room Name is the name or ID of the HipChat room you wish the notifications to appear in.

hipchat comment notification config
The "Comment Added" notification type is only available at the project plan level. So you will need to configure this notification for each of the project plans you are interested in.

Once the above HipChat notification is configured you should now start receiving HipChat notifications like the following:

hipchat comment notification

Have questions? Visit the Atlassian Community to connect, share, and learn with other Atlassian users and experts, including Adaptavist staff.

Ask a question about ScriptRunner for JIRA, Bitbucket Server, or Confluence.

Want to learn more? Check out courses on Adaptavist Learn, an online platform to onboard and train new users for Atlassian solutions.