Skip to content

Commit

Permalink
JENKINS-26673 - Environment Script leaves files in /tmp on Ubuntu
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidmalina committed Nov 15, 2015
1 parent c6fdd27 commit 334383d
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/main/java/com/lookout/jenkins/EnvironmentScript.java
Expand Up @@ -98,25 +98,31 @@ private Environment generateEnvironment(AbstractBuild<?, ?> build,
final Launcher launcher,
final BuildListener listener) throws IOException, InterruptedException {
// First we create the script in a temporary directory.
FilePath ws = build.getWorkspace(), scriptFile;
FilePath ws = build.getWorkspace(), scriptFile = null;

ByteArrayOutputStream commandOutput = new ByteArrayOutputStream();
int returnCode = -1;
try {
// Create a file in the system temporary directory with our script in it.
scriptFile = ws.createTextTempFile(build.getProject().getName(), ".sh", script, false);

// Then we execute the script, putting STDOUT in commandOutput.
returnCode = launcher.launch().cmds(buildCommandLine(scriptFile))
.envs(build.getEnvironment(listener))
.stderr(listener.getLogger())
.stdout(commandOutput)
.pwd(ws).join();
} catch (IOException e) {
Util.displayIOException(e, listener);
e.printStackTrace(listener.fatalError(Messages.EnvironmentScriptWrapper_UnableToProduceScript()));
return null;
} finally {
// Make sure we clean scriptFile
if (scriptFile != null && scriptFile.exists()) {
scriptFile.delete();
}
}

// Then we execute the script, putting STDOUT in commandOutput.
ByteArrayOutputStream commandOutput = new ByteArrayOutputStream();
int returnCode =
launcher.launch().cmds(buildCommandLine(scriptFile))
.envs(build.getEnvironment(listener))
.stderr(listener.getLogger())
.stdout(commandOutput)
.pwd(ws).join();

if (returnCode != 0) {
listener.fatalError(Messages.EnvironmentScriptWrapper_UnableToExecuteScript(returnCode));
return null;
Expand Down

0 comments on commit 334383d

Please sign in to comment.