use of org.jenkinsci.plugins.workflow.steps.EnvironmentExpander in project workflow-cps-plugin by jenkinsci.
the class DSL method invokeStep.
/**
* When {@link #invokeMethod(String, Object)} is calling a {@link StepDescriptor}
* @param d The {@link StepDescriptor} being invoked.
* @param name The name used to invoke the step. May be {@link StepDescriptor#getFunctionName}, a symbol as in {@link StepDescriptor#metaStepsOf}, or {@code d.clazz.getName()}.
* @param args The arguments passed to the step.
*/
protected Object invokeStep(StepDescriptor d, String name, Object args) {
final NamedArgsAndClosure ps = parseArgs(args, d);
CpsThread thread = CpsThread.current();
FlowNode an;
// TODO: generalize the notion of Step taking over the FlowNode creation.
boolean hack = d instanceof ParallelStep.DescriptorImpl || d instanceof LoadStep.DescriptorImpl;
if (ps.body == null && !hack) {
if (!(d.getClass().getName().equals("org.jenkinsci.plugins.workflow.support.steps.StageStep$DescriptorImpl")) && d.takesImplicitBlockArgument()) {
throw new IllegalStateException(String.format("%s step must be called with a body", name));
} else {
an = new StepAtomNode(exec, d, thread.head.get());
}
} else {
an = new StepStartNode(exec, d, thread.head.get());
}
final CpsStepContext context = new CpsStepContext(d, thread, handle, an, ps.body);
EnvVars allEnv = null;
Set<String> sensitiveVariables = Collections.emptySet();
try {
allEnv = context.get(EnvVars.class);
EnvironmentExpander envExpander = context.get(EnvironmentExpander.class);
if (envExpander != null) {
sensitiveVariables = new HashSet<>(envExpander.getSensitiveVariables());
}
} catch (IOException | InterruptedException e) {
LOGGER.log(Level.WARNING, "Unable to retrieve environment variables", e);
}
// Ensure ArgumentsAction is attached before we notify even synchronous listeners:
ArgumentsActionImpl argumentsAction = null;
try {
// No point storing empty arguments, and ParallelStep is a special case where we can't store its closure arguments
if (ps.namedArgs != null && !(ps.namedArgs.isEmpty()) && isKeepStepArguments() && !(d instanceof ParallelStep.DescriptorImpl)) {
// Get the environment variables to find ones that might be credentials bindings
Computer comp = context.get(Computer.class);
if (comp != null && allEnv != null) {
allEnv.entrySet().removeAll(comp.getEnvironment().entrySet());
}
argumentsAction = new ArgumentsActionImpl(ps.namedArgs, allEnv, sensitiveVariables);
an.addAction(argumentsAction);
}
} catch (Exception e) {
// Avoid breaking execution because we can't store some sort of crazy Step argument
LOGGER.log(Level.WARNING, "Error storing the arguments for step: " + d.getFunctionName(), e);
}
// TODO: use CPS call stack to obtain the current call site source location. See JENKINS-23013
thread.head.setNewHead(an);
Step s;
boolean sync;
ClassLoader originalLoader = Thread.currentThread().getContextClassLoader();
try {
TaskListener listener = context.get(TaskListener.class);
logInterpolationWarnings(name, argumentsAction, ps.interpolatedStrings, allEnv, sensitiveVariables, listener);
if (unreportedAmbiguousFunctions.remove(name)) {
reportAmbiguousStepInvocation(context, d, listener);
}
d.checkContextAvailability(context);
Thread.currentThread().setContextClassLoader(CpsVmExecutorService.ORIGINAL_CONTEXT_CLASS_LOADER.get());
if (Util.isOverridden(StepDescriptor.class, d.getClass(), "newInstance", Map.class)) {
s = d.newInstance(ps.namedArgs);
} else {
DescribableModel<? extends Step> stepModel = DescribableModel.of(d.clazz);
s = stepModel.instantiate(ps.namedArgs, listener);
}
// Persist the node - block start and end nodes do their own persistence.
CpsFlowExecution.maybeAutoPersistNode(an);
// Call any registered StepListeners.
for (StepListener sl : ExtensionList.lookup(StepListener.class)) {
try {
sl.notifyOfNewStep(s, context);
} catch (Throwable e) {
LOGGER.log(Level.WARNING, "failed to notify step listener before starting " + s.getDescriptor().getFunctionName(), e);
}
}
if (!context.isCompleted()) {
StepExecution e = s.start(context);
thread.setStep(e);
sync = e.start();
} else {
s = null;
sync = true;
}
} catch (Exception e) {
context.onFailure(e);
s = null;
sync = true;
} finally {
Thread.currentThread().setContextClassLoader(originalLoader);
}
if (sync) {
assert context.withBodyInvokers(List::isEmpty) : "If a step claims synchronous completion, it shouldn't invoke body";
if (context.getOutcome() == null) {
context.onFailure(new AssertionError("Step " + s + " claimed to have ended synchronously, but didn't set the result via StepContext.onSuccess/onFailure"));
}
thread.setStep(null);
// we just move on accordingly
if (an instanceof StepStartNode) {
// no body invoked, so EndNode follows StartNode immediately.
thread.head.setNewHead(new StepEndNode(exec, (StepStartNode) an, an));
}
thread.head.markIfFail(context.getOutcome());
return context.replay();
} else {
// if it's in progress, suspend it until we get invoked later.
// when it resumes, the CPS caller behaves as if this method returned with the resume value
Continuable.suspend(name, new ThreadTaskImpl(context));
// so the execution will never reach here.
throw new AssertionError();
}
}
Aggregations