Skip to content

Commit

Permalink
Merge pull request #313 from presPetkov/JENKINS-48258-busy-ssh-text-file
Browse files Browse the repository at this point in the history


Jenkins 48258 busy ssh text file fix
  • Loading branch information
MarkEWaite committed May 18, 2018
2 parents 98eea34 + 0cce450 commit 315ad4e
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java
Expand Up @@ -1971,6 +1971,8 @@ private File createWindowsGitSSH(File key, String user) throws IOException {

private File createUnixGitSSH(File key, String user) throws IOException {
File ssh = createTempFile("ssh", ".sh");
File ssh_copy = new File(ssh.toString() + "-copy");
boolean isCopied = false;
try (PrintWriter w = new PrintWriter(ssh, Charset.defaultCharset().toString())) {
w.println("#!/bin/sh");
// ${SSH_ASKPASS} might be ignored if ${DISPLAY} is not set
Expand All @@ -1981,7 +1983,31 @@ private File createUnixGitSSH(File key, String user) throws IOException {
w.println("ssh -i \"" + key.getAbsolutePath() + "\" -l \"" + user + "\" -o StrictHostKeyChecking=no \"$@\"");
}
ssh.setExecutable(true, true);
return ssh;
//JENKINS-48258 git client plugin occasionally fails with "text file busy" error
//The following creates a copy of the generated file and deletes the original
//In case of a failure return the original and delete the copy
String fromLocation = ssh.toString();
String toLocation = ssh_copy.toString();
//Copying ssh file
try {
new ProcessBuilder("cp", fromLocation, toLocation).start().waitFor();
isCopied = true;
ssh_copy.setExecutable(true,true);
//Deleting original file
deleteTempFile(ssh);
}
catch(InterruptedException ie)
{
//Delete the copied file in case of failure
if(isCopied)
{
deleteTempFile(ssh_copy);
}
//Previous operation failed. Return original file
return ssh;
}

return ssh_copy;
}

private String launchCommandIn(ArgumentListBuilder args, File workDir) throws GitException, InterruptedException {
Expand Down

0 comments on commit 315ad4e

Please sign in to comment.