Scriptable Tasks

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.

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 scripted 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 category.

  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 parameters as required. You can also paste the code into a file under your script root and use theFiletab 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"),
]