Skip to content

Commit

Permalink
JENKINS-49681 Allowing spaces in front of interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Heid committed Feb 21, 2018
1 parent d0159e9 commit a72f93f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
15 changes: 10 additions & 5 deletions README.md
Expand Up @@ -12,11 +12,11 @@ This plugin allows you to run the following actions after a build:
* Groovy scripts
* Build steps

You can configure these actions depending on the build status (i.e., only run when build is successful).
You can configure these actions depending on the build status (i.e., only run when build is successful).
Scripts can also be executed on the master, on slaves or both. On matrix projects, the build can be executed on
each axis.

With version 1.1.0 scripts can be executed on the master, on slaves or both.

Please refer to the [plugin description](https://wiki.jenkins.io/display/JENKINS/PostBuildScript+Plugin) for further
Please refer to the [plugin description](https://plugins.jenkins.io/postbuildscript) for further
information.

## Downloads
Expand All @@ -40,7 +40,6 @@ Each commit is built by the official Jenkins CI. The current build status is acc

https://ci.jenkins.io/job/Plugins/job/postbuildscript-plugin


## Contributing

The plugin was created in 2011 by Gregory Boissinot. It was adopted by Daniel Heid in October 2017. Feel free
Expand Down Expand Up @@ -89,6 +88,12 @@ changes without needing to run to `package` phase.

## Release Notes

### Version 2.5.1

If the shebang contains a space in front of the interpreter, it will be stripped out.

* JENKINS-49681 - Scripts always fail when run in build, but succeed from command line

### Version 2.5.0

This version introduces the ability to run Groovy scripts in a sandbox when activated using the checkbox in the configuration view.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.4</version>
<version>3.5</version>
</parent>

<artifactId>postbuildscript</artifactId>
Expand Down
Expand Up @@ -14,9 +14,12 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

public class CommandExecutor {

private static final Pattern SHEBANG_WITH_SPACES = Pattern.compile("^#!\\s+");

private final TaskListener listener;

private final Logger logger;
Expand All @@ -32,9 +35,8 @@ public CommandExecutor(Logger logger, TaskListener listener, FilePath workspace,
this.launcher = launcher;
}


public int executeCommand(Command command) throws PostBuildScriptException {
String scriptContent = resolveScriptContent(command);
String scriptContent = removeSpaceInFrontOfInterpreter(resolveScriptContent(command));
try {
List<String> arguments = buildArguments(command, scriptContent);
return launcher.launch().cmds(arguments).stdout(listener).pwd(workspace).join();
Expand All @@ -44,6 +46,13 @@ public int executeCommand(Command command) throws PostBuildScriptException {

}

private static String removeSpaceInFrontOfInterpreter(String scriptContent) {
if (scriptContent.startsWith("#!")) {
return SHEBANG_WITH_SPACES.matcher(scriptContent).replaceFirst("#!");
}
return scriptContent;
}

private List<String> buildArguments(Command command, String scriptContent)
throws IOException, InterruptedException {
CommandInterpreter interpreter = createInterpreter(scriptContent);
Expand All @@ -66,7 +75,9 @@ private String resolveScriptContent(Command command)
FilePath script = new ScriptFilePath(workspace).resolve(command.getScriptPath());
logger.info(Messages.PostBuildScript_ExecutingScript(script, command.getParameters()));
LoadScriptContentCallable callable = new LoadScriptContentCallable();
return new Content(callable).resolve(script);
Content content = new Content(callable);
String resolvedContent = content.resolve(script);
return resolvedContent.trim();

}

Expand Down
Expand Up @@ -12,6 +12,7 @@
import org.mockito.junit.MockitoJUnitRunner;

import java.io.File;
import java.io.IOException;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
Expand All @@ -23,20 +24,39 @@ public class CommandExecutorIT {
public JenkinsRule jenkinsRule = new JenkinsRule();
@Mock
private Logger logger;
private File scriptFile;
private CommandExecutor executor;

@Test
public void executesCommand() throws Exception {

LocalLauncher launcher = jenkinsRule.createLocalLauncher();
File tempFile = File.createTempFile(CommandExecutorIT.class.getName(), ".script");
tempFile.deleteOnExit();
FilePath workspace = new FilePath(tempFile.getParentFile());
TaskListener listener = jenkinsRule.createTaskListener();
CommandExecutor executor = new CommandExecutor(logger, listener, workspace, launcher);
scriptFile = File.createTempFile(CommandExecutorIT.class.getName(), ".script");
scriptFile.deleteOnExit();
givenExecutor();

int command = executor.executeCommand(new Command(tempFile.getName() + " param1 param2"));
int command = executor.executeCommand(new Command(scriptFile.getName() + " param1 param2"));

assertThat(command, is(0));

}

@Test
public void supportsShebangWithSpacesInFrontOfInterpreter() throws Exception {

scriptFile = new File(getClass().getResource("/shebang_with_spaces.sh").toURI());
givenExecutor();

int command = executor.executeCommand(new Command(scriptFile.getName() + " param1 param2"));

assertThat(command, is(0));

}

private void givenExecutor() throws IOException {
LocalLauncher launcher = jenkinsRule.createLocalLauncher();
FilePath workspace = new FilePath(scriptFile.getParentFile());
TaskListener listener = jenkinsRule.createTaskListener();
executor = new CommandExecutor(logger, listener, workspace, launcher);
}

}
2 changes: 2 additions & 0 deletions src/test/resources/shebang_with_spaces.sh
@@ -0,0 +1,2 @@
#! /bin/bash
echo "Hello world"

0 comments on commit a72f93f

Please sign in to comment.