fix: render before command with options in CommandsWrapper (#7496)

This commit is contained in:
Mathieu Gabelle
2025-02-19 17:02:31 +01:00
committed by GitHub
parent 45a9ea0cc5
commit c6d21776ec
2 changed files with 46 additions and 6 deletions

View File

@@ -4,6 +4,8 @@ import io.kestra.core.exceptions.IllegalVariableEvaluationException;
import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.models.property.Property;
import io.kestra.core.models.tasks.*;
import io.kestra.core.models.tasks.runners.RemoteRunnerInterface;
import io.kestra.core.models.tasks.runners.ScriptService;
import io.kestra.core.models.tasks.runners.TargetOS;
import io.kestra.core.models.tasks.runners.TaskRunner;
import io.kestra.core.runners.RunContext;
@@ -166,9 +168,14 @@ public abstract class AbstractExecScript extends Task implements RunnableTask<Sc
.withOutputFiles(runContext.render(this.getOutputFiles()).asList(String.class))
.withEnableOutputDirectory(runContext.render(this.getOutputDirectory()).as(Boolean.class).orElse(null))
.withTimeout(runContext.render(this.getTimeout()).as(Duration.class).orElse(null))
.withTargetOS(runContext.render(this.getTargetOS()).as(TargetOS.class).orElseThrow());
.withTargetOS(runContext.render(this.getTargetOS()).as(TargetOS.class).orElseThrow())
.withFailFast(runContext.render(this.getFailFast()).as(Boolean.class).orElse(false));
}
/**
* Rendering of beforeCommands will be done in the CommandsWrapper to give access to the workingDir variable
*/
@Deprecated(since = "0.22")
protected List<String> getBeforeCommandsWithOptions(RunContext runContext) throws IllegalVariableEvaluationException {
return mayAddExitOnErrorCommands(runContext.render(this.getBeforeCommands()).asList(String.class), runContext);
}

View File

@@ -21,16 +21,14 @@ import io.kestra.plugin.scripts.runner.docker.Docker;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.With;
import org.apache.commons.lang3.SystemUtils;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Path;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import static io.kestra.core.utils.Rethrow.throwFunction;
@@ -54,6 +52,12 @@ public class CommandsWrapper implements TaskCommands {
@With
private Property<List<String>> commands;
@With
private boolean beforeCommandsWithOptions;
@With
private boolean failFast;
private Map<String, String> env;
@With
@@ -109,6 +113,8 @@ public class CommandsWrapper implements TaskCommands {
interpreter,
beforeCommands,
commands,
beforeCommandsWithOptions,
failFast,
envs,
logConsumer,
runnerType,
@@ -176,7 +182,7 @@ public class CommandsWrapper implements TaskCommands {
renderedCommands :
ScriptService.scriptCommands(
renderedInterpreter,
renderedBeforeCommands,
this.isBeforeCommandsWithOptions() ? getBeforeCommandsWithOptions(renderedBeforeCommands) : renderedBeforeCommands,
renderedCommands,
Optional.ofNullable(targetOS).orElse(TargetOS.AUTO)
);
@@ -290,4 +296,31 @@ public class CommandsWrapper implements TaskCommands {
taskRunner instanceof RemoteRunnerInterface
);
}
protected List<String> getBeforeCommandsWithOptions(List<String> beforeCommands) throws IllegalVariableEvaluationException {
if (!this.isFailFast()) {
return beforeCommands;
}
if (beforeCommands == null || beforeCommands.isEmpty()) {
return getExitOnErrorCommands();
}
ArrayList<String> newCommands = new ArrayList<>(beforeCommands.size() + 1);
newCommands.addAll(getExitOnErrorCommands());
newCommands.addAll(beforeCommands);
return newCommands;
}
protected List<String> getExitOnErrorCommands() {
TargetOS os = this.getTargetOS();
// If targetOS is Windows OR targetOS is AUTO && current system is windows and we use process as a runner.(TLDR will run on windows)
if (os == TargetOS.WINDOWS ||
(os == TargetOS.AUTO && SystemUtils.IS_OS_WINDOWS && this.getTaskRunner() instanceof Process)) {
return List.of("");
}
// errexit option may be unsupported by non-shell interpreter.
return List.of("set -e");
}
}