Heads up! ScriptRunner for Bamboo documentation has moved to https://docs.adaptavist.com/sr4bam. Adaptavist will keep this site up for a bit, but no future updates to documentation will be published here. ScriptRunner 6.20.0 will be the last release to link to scriptrunner.adaptavist.com for in-app help.

Introduction

Scriptable tasks allow you to dynamically create and execute tasks with different parameters depending on build variables, files changed, etc, using the Bamboo Specs API.

For example, you might use different Maven goals depending on the branch or secure copy (scp) files to different hosts for different staging environments.

The generated task or tasks take the place of the scriptable task.

Usage

  1. Have a standard task (like a Maven 3 task) configured as part of your job.

  2. Add a new task, ScriptRunner Scriptable Task.

  3. Click the Scriptable Task heading because there is only one type of task in this cateogry.

  4. Click the Copy from Other Task highlighted link instead of writing the code for the task from scratch.

    add scriptable task
  5. Click the dropdown that shows you all tasks in all jobs, and then select the task to generate the code.

    copy task code dlg
  6. Click OK.

    Result: The generated code for the task appears in the Code Editor.

    copy task code complete
  7. Now you can add conditional logic to change the paramters as required. You can also paste the code into a file under your script root and use the File tab to point to the .groovy file.

    Here’s a full example that adds the deploy tasks if the branch is master:

    import com.atlassian.bamboo.specs.builders.task.MavenTask
    import com.atlassian.bamboo.variable.CustomVariableContext
    import com.atlassian.sal.api.component.ComponentLocator
    
    def variableContext = ComponentLocator.getComponent(CustomVariableContext)
    def branchName = variableContext.getVariableContexts().get("planRepository.branchName")?.getValue()
    
    def goals = "clean test"
    
    // if the branch is master, then also deploy
    if (branchName == "master") {
        goals += " deploy"
    }
    
    new MavenTask()
        .description("Execute maven goals")
        .goal(goals)
        .jdk("JDK")
        .executableLabel("mvn3x")
        .hasTests(true)

Multiple Tasks

You can also create/execute multiple tasks rather than a simple task. The following trivial example will execute two script tasks:

import com.atlassian.bamboo.specs.builders.task.ScriptTask

[
    new ScriptTask().inlineBody("echo First"),
    new ScriptTask().inlineBody("echo Second"),
]

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.