Bamboo Tasks API
REST APIs

Executing external Processes using ProcessService

ProcessService is a bean which can be injected into a TaskType to execute external processes.

ProcessService uses the ExternalProcessBuilder to collect information such as the command, environment variables and working directory to be used to launch a new process. When the process is executed its output is automatically appended to the build log as it becomes available.

Example

The following Task uses ProcessService to execute the ls Unix/Linux/Mac utility to list the files and directories available in the working directory. The TaskResultBuilder is then used to check that the return code of the process is zero (e.g. that the task is successful).

Example:

1
2
import com.atlassian.bamboo.process.ExternalProcessBuilder;
import com.atlassian.bamboo.process.ProcessService;
import com.atlassian.bamboo.task.TaskContext;
import com.atlassian.bamboo.task.TaskException;
import com.atlassian.bamboo.task.TaskResult;
import com.atlassian.bamboo.task.TaskResultBuilder;
import com.atlassian.bamboo.task.TaskType;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.atlassian.utils.process.ExternalProcess;

import java.util.Arrays;

public class ListDirectoryTask implements TaskType {
    private final ProcessService processService;

    public ListDirectoryTask(final @ComponentImport ProcessService processService) {
        this.processService = processService;
    }

    @Override
    public TaskResult execute(final TaskContext taskContext) throws TaskException {
        TaskResultBuilder builder = TaskResultBuilder.newBuilder(taskContext);

        ExternalProcess process = processService.createExternalProcess(taskContext,
                new ExternalProcessBuilder()
                        .command(Arrays.asList("/bin/ls"))
                        .workingDirectory(taskContext.getWorkingDirectory()));

        process.execute();

        return builder.checkReturnCode(process, 0).build();
    }
}

Rate this page: