diff --git a/src/main/java/io/jenkins/plugins/projectenv/WithProjectEnvStepExecution.java b/src/main/java/io/jenkins/plugins/projectenv/WithProjectEnvStepExecution.java index 45f31e9..eb3b1bd 100644 --- a/src/main/java/io/jenkins/plugins/projectenv/WithProjectEnvStepExecution.java +++ b/src/main/java/io/jenkins/plugins/projectenv/WithProjectEnvStepExecution.java @@ -2,7 +2,6 @@ import hudson.EnvVars; import hudson.FilePath; -import hudson.Launcher.ProcStarter; import hudson.Util; import io.jenkins.plugins.projectenv.agent.AgentInfo; import io.jenkins.plugins.projectenv.agent.AgentInfoCallable; @@ -84,22 +83,35 @@ private void execute() throws Exception { AgentInfo agentInfo = getAgentInfo(); FilePath temporaryDirectory = createTemporaryDirectory(); + EnvVars projectEnvVars = new EnvVars(); - FilePath projectEnvCliArchive = downloadProjectEnvCliArchive(agentInfo, temporaryDirectory); - extractProjectEnvCliArchive(projectEnvCliArchive, temporaryDirectory); - - FilePath executable = resolveProjectEnvCliExecutable(agentInfo, temporaryDirectory); + String executable = resolveProjectEnvCliExecutableFromPath(); + if (executable == null) { + FilePath projectEnvCliArchive = downloadProjectEnvCliArchive(agentInfo, temporaryDirectory); + extractProjectEnvCliArchive(projectEnvCliArchive, temporaryDirectory); + executable = resolveProjectEnvCliExecutable(agentInfo, temporaryDirectory); + projectEnvVars.put(PATH_VAR_PREFIX + "PROJECT_ENV_CLI", temporaryDirectory.getRemote()); + } Map> allToolInfos = executeProjectEnvCli(executable); - - EnvVars projectEnvVars = processToolInfos(allToolInfos); + processToolInfos(projectEnvVars, allToolInfos); + projectEnvVars.put(PATH_VAR_PREFIX + "PROJECT_ENV_CLI", temporaryDirectory.getRemote()); projectEnvVars.put(PATH_VAR_PREFIX + "PROJECT_ENV_CLI", temporaryDirectory.getRemote()); - BodyExecutionCallback callback = createTempDirectoryCleanupCallback(temporaryDirectory); - invokeBodyWithEnvVarsAndCallback(projectEnvVars, callback); } + private String resolveProjectEnvCliExecutableFromPath() throws Exception { + String executable = getProjectEnvCliExecutableName(getAgentInfo()); + + OperatingSystem operatingSystem = getAgentInfo().getOperatingSystem(); + if (operatingSystem == OperatingSystem.WINDOWS) { + return ProcHelper.executeAndGetStdOut(getContext(), "where", executable); + } else { + return ProcHelper.executeAndGetStdOut(getContext(), "/bin/sh", "-c", "which " + executable); + } + } + private AgentInfo getAgentInfo() throws Exception { return StepContextHelper.getComputer(getContext()).getChannel().call(new AgentInfoCallable()); } @@ -208,14 +220,18 @@ private void extractProjectEnvCliArchive(FilePath archive, FilePath target) thro } } - private FilePath resolveProjectEnvCliExecutable(AgentInfo agentInfo, FilePath sourceDirectory) throws Exception { - String executableFilename = CLI_EXECUTABLE_FILE_NAME + getExecutableExtension(agentInfo); + private String resolveProjectEnvCliExecutable(AgentInfo agentInfo, FilePath sourceDirectory) throws Exception { + String executableFilename = getProjectEnvCliExecutableName(agentInfo); FilePath executable = sourceDirectory.child(executableFilename); if (!executable.exists()) { throw new IllegalStateException("could not find Project-Env CLI at " + executable); } - return executable; + return executable.getRemote(); + } + + private String getProjectEnvCliExecutableName(AgentInfo agentInfo) { + return CLI_EXECUTABLE_FILE_NAME + getExecutableExtension(agentInfo); } private String getExecutableExtension(AgentInfo agentInfo) { @@ -223,16 +239,9 @@ private String getExecutableExtension(AgentInfo agentInfo) { CLI_EXECUTABLE_FILE_EXTENSION_WINDOWS : CLI_EXECUTABLE_FILE_EXTENSION_OTHERS; } - private Map> executeProjectEnvCli(FilePath executable) throws Exception { - List commands = createProjectEnvCliCommand(executable); - FilePath workspace = StepContextHelper.getWorkspace(getContext()); - - ProcStarter procStarter = StepContextHelper.getLauncher(getContext()) - .launch() - .cmds(commands) - .pwd(workspace); - - ProcResult procResult = ProcHelper.executeAndReturnStdOut(procStarter, getContext()); + private Map> executeProjectEnvCli(String executable) throws Exception { + String[] commands = createProjectEnvCliCommand(executable); + ProcResult procResult = ProcHelper.execute(getContext(), commands); if (procResult.getExitCode() != 0) { throw new IllegalStateException("received non-zero exit code " + procResult.getExitCode() + " from Project-Env CLI"); } @@ -240,20 +249,18 @@ private Map> executeProjectEnvCli(FilePath executable) th return ToolInfoParser.fromJson(procResult.getStdOutput()); } - private List createProjectEnvCliCommand(FilePath executable) { + private String[] createProjectEnvCliCommand(String executable) { List command = new ArrayList<>(); - command.add(executable.getRemote()); + command.add(executable); command.add("--config-file=" + configFile); if (cliDebug) { command.add("--debug"); } - return command; + return command.toArray(new String[0]); } - private EnvVars processToolInfos(Map> allToolInfos) { - EnvVars envVars = new EnvVars(); - + private void processToolInfos(EnvVars envVars, Map> allToolInfos) { for (Map.Entry> entry : allToolInfos.entrySet()) { for (ToolInfo toolInfo : entry.getValue()) { List pathElements = toolInfo.getPathElements(); @@ -266,8 +273,6 @@ private EnvVars processToolInfos(Map> allToolInfos) { envVars.putAll(toolInfo.getEnvironmentVariables()); } } - - return envVars; } private BodyExecutionCallback createTempDirectoryCleanupCallback(FilePath tempDirectory) { diff --git a/src/main/java/io/jenkins/plugins/projectenv/context/StepContextHelper.java b/src/main/java/io/jenkins/plugins/projectenv/context/StepContextHelper.java index b4a76ae..0f4d7ae 100644 --- a/src/main/java/io/jenkins/plugins/projectenv/context/StepContextHelper.java +++ b/src/main/java/io/jenkins/plugins/projectenv/context/StepContextHelper.java @@ -1,5 +1,6 @@ package io.jenkins.plugins.projectenv.context; +import hudson.EnvVars; import hudson.FilePath; import hudson.Launcher; import hudson.model.Computer; @@ -19,6 +20,10 @@ public static Launcher getLauncher(StepContext stepContext) throws Exception { return getOrThrow(stepContext, Launcher.class); } + public static EnvVars getEnvVars(StepContext stepContext) throws Exception { + return getOrThrow(stepContext, EnvVars.class); + } + public static TaskListener getTaskListener(StepContext stepContext) throws Exception { return getOrThrow(stepContext, TaskListener.class); } diff --git a/src/main/java/io/jenkins/plugins/projectenv/proc/ProcHelper.java b/src/main/java/io/jenkins/plugins/projectenv/proc/ProcHelper.java index 80a1f82..fb412f0 100644 --- a/src/main/java/io/jenkins/plugins/projectenv/proc/ProcHelper.java +++ b/src/main/java/io/jenkins/plugins/projectenv/proc/ProcHelper.java @@ -15,9 +15,24 @@ private ProcHelper() { // noop } - public static ProcResult executeAndReturnStdOut(ProcStarter procStarter, StepContext context) throws Exception { + public static String executeAndGetStdOut(StepContext context, String... commands) throws Exception { + ProcResult procResult = execute(context, commands); + if (procResult.getExitCode() == 0) { + return procResult.getStdOutput(); + } else { + return null; + } + } + + public static ProcResult execute(StepContext context, String... commands) throws Exception { try (ByteArrayOutputStream stdOutInputStream = new ByteArrayOutputStream()) { + ProcStarter procStarter = StepContextHelper.getLauncher(context) + .launch() + .cmds(commands) + .envs(StepContextHelper.getEnvVars(context)) + .pwd(StepContextHelper.getWorkspace(context)); + Thread stdErrThread; int exitCode; try (PipedInputStream stdErrInputStream = new PipedInputStream();