Hi! I’m writing a pipeline step plugin which should be able to use sh/bat step inside. The question is how to call pipeline step inside plugin?
To do so, I’ve found source code of Durable Task Step plugin: GitHub - jenkinsci/workflow-durable-task-step-plugin and started to research how it works.
I’ve tried to initialize bat step, receive executor and just start it, using context that I’ve received to my build step. Here is my code:
String script = "dir";
BatchScriptStep batchScriptStep = new BatchScriptStep(script);
StepExecution batStepExecution = batchScriptStep.start(context);
if (batStepExecution.start()) {
Logger.log("Synchronously finished execution of the step");
} else {
Logger.log("Asynchronously execution of the step started");
}
// Some other code
In this case other code starts to execute when bat step has not finished yet.
To be sure that bat step has been finished I had to do the following hack:
private static final String UNFINISHED_TASK_MARKER = " done? false";
if (batStepExecution.start()) {
Logger.log("Synchronously finished execution of the step");
} else {
Logger.log("Asynchronously execution of the step started");
String status;
while ((status = batStepExecution.getStatus()) != null && status.contains(UNFINISHED_TASK_MARKER)) {
Logger.log("Waiting to finish the execution of the step. Status: " + batStepExecution.getStatus());
Thread.sleep(50);
}
}
// Some other code
But in this case I still have no any possibility to get command’s exit code.
I know that Jenkins architecture is not developed to call pipeline steps from another step but still. Is it possible to execute such steps without loosing any functionality, waiting for step to finish and obtain output of step? If it is not possible to do in traditional way, can I do so, using Java reflections? Where I obtain the step result? And where I can find source code of how pipeline actually checks if step finished with non-zero exit code?