Last updated Mar 13, 2024

How do I start a build programatically?

Below is the code for starting a manual build (it'll show the changes since the last build). The work happens through the BuildExecutionManager. The buildExecutionManager.tryToDetectAndBuild takes a callback function BuildExecutionManager.BuildDetectionAction that creates the BuildContext. You can get away with not populating the BuildChangesImpl if you don't want to (no changes will be shown for the generated build result)

1
2
buildExecutionManager.tryToDetectAndBuild(buildKey, new BuildExecutionManager.BuildDetectionAction()
{
    public BuildContext process()
    {
        try
        {
            BuildLogger buildLogger = buildPlan.getBuildLogger();
            BuildDefinition buildDefinition = buildPlan.getBuildDefinition();
            Repository repository = buildDefinition.getRepository();

            // Some feedback to the UI for what's going down
            buildLogger.addBuildLogEntry("Manual build triggered by " + user);
            
            // This block only needed if you care about changes
            String lastVcsRevisionKey = buildPlan.getLastVcsRevisionKey();
            BuildChanges buildChanges;
            if (lastVcsRevisionKey != null)
            {
                buildChanges = changeDetectionManager.collectChangesSinceLastBuild(buildPlan.getKey(), repository, lastVcsRevisionKey);
            }
            else
            {
                buildChanges = new BuildChangesImpl();
            }

            // A trigger reason
            TriggerReason triggerReason = triggerReasonManager.getTriggerReason(ManualBuildTriggerReason.KEY, buildChanges,
                                                                                EasyMap.build(ManualBuildTriggerReason.TRIGGER_MANUAL_USER, user.getName()));

            BuildContext buildContext = new BuildContextImpl(buildPlan,
                                                             buildManager.retrieveNextBuildNumber(buildPlan.getKey()), // needed to get the correct build result number
                                                             triggerReason,
                                                             buildDefinition,
                                                             buildChanges);

            return buildContext;
        }
        catch (Exception e)
        {
            String message = "Error encountered while triggering manual build: " + e.getMessage();
            log.error(buildPlan.getBuildLogger().addBuildLogEntry(message), e);
            errorUpdateHandler.recordError(buildPlan.getKey(), message, e);
        }

        return null;
    }
}, true);

or if you just want to fire off a manual build, you can use call the method

1
2
public void startManualBuild(@NotNull final Build buildPlan, @NotNull final User user)

Rate this page: