Invoke a bamboo job programatically

Prameesha Samarakoon September 14, 2017

Hi, 

I have a requirement to invoke a bamboo job from a bamboo plugin. Th erequirement is to rerun specific jobs upon failure.  Using rest calls does not seem to be an option for various reasons.  I've found two options so far, to capture the failed job event. 

1. Event Listener - JobCompletedEvent

2. PostJobAction Module

 

And I use the following code to invoke the plugin, however this snippet throws null exceptions and I'm not sure if it only invokes the failed job or the whole build. Is there an alternative way to do this?

 @ComponentImport
private VariableDefinitionManager variableDefinitionManager;
@ComponentImport
private ImmutablePlanCacheService immutablePlanCacheService;
@ComponentImport
private CredentialsAccessor credentialsAccessor;
@ComponentImport
private BuildResultsSummaryManager buildResultSummaryManager;
@ComponentImport
private BuildDefinitionManager buildDefinitionManager;
@ComponentImport
private ChangeDetectionManager changeDetectionManager;


public PostTaskListener(VariableDefinitionManager variableDefinitionManager, ImmutablePlanCacheService immutablePlanCacheService, CredentialsAccessor credentialsAccessor, BuildResultsSummaryManager buildResultSummaryManager, BuildDefinitionManager buildDefinitionManager, ChangeDetectionManager changeDetectionManager) {
this.variableDefinitionManager = variableDefinitionManager;
this.immutablePlanCacheService = immutablePlanCacheService;
this.credentialsAccessor = credentialsAccessor;
this.buildResultSummaryManager = buildResultSummaryManager;
this.buildDefinitionManager = buildDefinitionManager;
this.changeDetectionManager = changeDetectionManager;
this.jobExecutionManager = jobExecutionManager;
}

public void handleEvent(Event event) {

if(event instanceof JobCompletedEvent){

JobCompletedEvent jobCompletedEvent = (JobCompletedEvent) event;

ResultsSummary resultsSummary = buildResultSummaryManager.getResultsSummary(jobCompletedEvent.getPlanResultKey());

BuildDefinition buildDefinition = buildDefinitionManager.getBuildDefinition(((JobCompletedEvent) event).getPlanKey());

this.buildContextBuilderFactory = new BuildContextBuilderFactoryImpl(new BranchIntegrationServiceImpl(immutablePlanCacheService), variableDefinitionManager, credentialsAccessor);
BuildContextBuilder buildContextBuilder = buildContextBuilderFactory.createBuilder();
BuildContext buildContext = buildContextBuilder.build();

jobExecutionManager.requestExecution(buildContext);

}
}

 

Link : https://developer.atlassian.com/bamboodev/development-resources/bamboo-developer-faq/how-do-i-start-a-build-programatically   

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events