如何使用Java Api在Bamboo中获取当前分支名称

时间:2018-06-22 08:12:25

标签: java api bamboo

我使用以下项目创建了项目

atlas-create-bamboo-plugin

我需要使用Java Api获取当前的分支名称。 我需要类似的东西:

@Override
    public TaskResult execute(final TaskContext taskContext) throws TaskException {
        final BuildLogger buildLogger = taskContext.getBuildLogger();
        buildLogger.addBuildLogEntry("BranchName: " + /*Some magic*/);

        return TaskResultBuilder.newBuilder(taskContext).success().build();
    }

1 个答案:

答案 0 :(得分:0)

您可以将变量上下文注入任务中,并可以访问必须构建和部署变量。如果您熟悉Spring,它的工作原理类似于Spring bean注入。

@Scanned
public class YourTask implements CommonTaskType {

    private final CustomVariableContext customVariableContext;

    public YourTask(@ComponentImport CustomVariableContext customVariableContext) {
        this.customVariableContext = customVariableContext;
    }

    @Override
    public TaskResult execute(@NotNull CommonTaskContext taskContext) throws TaskException {
        final TaskResultBuilder taskResultBuilder = TaskResultBuilder.newBuilder(taskContext);
        final BuildLogger buildLogger = taskContext.getBuildLogger();

        Map<String, String> buildVariables = customVariableContext.getVariables(taskContext.getCommonContext());

        // get branch name from build variables
        String branchName = buildVariables.get("planRepository.branchName");

        // log all build variables to console
        buildVariables.forEach((key, value) -> buildLogger.addBuildLogEntry(key + ": " + value));

        return taskResultBuilder.success().build();
    }
}