Skip to content

Commit ccfa7a3

Browse files
author
TechsCode
committed
Cleaned up DeploymentManager.java
1 parent b9afef9 commit ccfa7a3

1 file changed

Lines changed: 45 additions & 41 deletions

File tree

src/main/java/me/TechsCode/ReleaseServer/DeploymentManager.java

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -44,60 +44,64 @@ public void run() {
4444
}
4545

4646
for (Deployment deployment : project.getDeployments()) {
47-
if (!deployment.isEnabled()) continue;
48-
49-
Remote remote = deployment.getRemote();
47+
if (deployment.isEnabled()){
48+
deploy(project, artifact, deployment);
49+
}
50+
}
51+
});
52+
}
5053

51-
try {
52-
Properties config = new Properties();
53-
config.put("StrictHostKeyChecking", "no");
54+
this.previousReleases = artifacts.stream().map(a -> a.getRelease().getId()).collect(Collectors.toList());
5455

55-
Session session = jsch.getSession(remote.getUsername(), remote.getHostname(), remote.getPort());
56-
session.setPassword(remote.getPassword());
57-
session.setConfig(config);
58-
session.connect();
56+
try {
57+
sleep(DELAY);
58+
} catch (InterruptedException e) {
59+
e.printStackTrace();
60+
}
61+
}
62+
}
5963

60-
ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
61-
sftp.connect();
62-
sftp.cd(deployment.getPath());
64+
private void deploy(Project project, Artifact artifact, Deployment deployment){
65+
Remote remote = deployment.getRemote();
6366

64-
for (File asset : artifact.getAssets()) {
65-
sftp.put(new FileInputStream(asset), asset.getName(), ChannelSftp.OVERWRITE);
66-
}
67+
try {
68+
Properties config = new Properties();
69+
config.put("StrictHostKeyChecking", "no");
6770

68-
sftp.exit();
71+
Session session = jsch.getSession(remote.getUsername(), remote.getHostname(), remote.getPort());
72+
session.setPassword(remote.getPassword());
73+
session.setConfig(config);
74+
session.connect();
6975

70-
for (String command : deployment.getCommands()) {
71-
ChannelExec channel = (ChannelExec) session.openChannel("exec");
72-
channel.setCommand(command);
73-
channel.connect();
76+
ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
77+
sftp.connect();
78+
sftp.cd(deployment.getPath());
7479

75-
StringWriter writer = new StringWriter();
76-
IOUtils.copy(channel.getInputStream(), writer, StandardCharsets.UTF_8);
77-
String output = writer.toString();
80+
for (File asset : artifact.getAssets()) {
81+
sftp.put(new FileInputStream(asset), asset.getName(), ChannelSftp.OVERWRITE);
82+
}
7883

79-
System.out.println(output);
84+
sftp.exit();
8085

81-
channel.disconnect();
82-
}
86+
for (String command : deployment.getCommands()) {
87+
ChannelExec channel = (ChannelExec) session.openChannel("exec");
88+
channel.setCommand(command);
89+
channel.connect();
8390

84-
session.disconnect();
85-
System.out.println("[" + project.getName() + "] Deployment of release [" + artifact.getReleaseTag() + "] to remote [" + remote.getHostname() + "] was successful");
86-
} catch (JSchException | SftpException | IOException e) {
87-
System.out.println("[" + project.getName() + "] Error while deploying release [" + artifact.getReleaseTag() + "] to [" + remote.getHostname() + "]");
88-
System.out.println(e.getMessage());
89-
}
90-
}
91-
});
92-
}
91+
StringWriter writer = new StringWriter();
92+
IOUtils.copy(channel.getInputStream(), writer, StandardCharsets.UTF_8);
93+
String output = writer.toString();
9394

94-
this.previousReleases = artifacts.stream().map(a -> a.getRelease().getId()).collect(Collectors.toList());
95+
System.out.println(output);
9596

96-
try {
97-
sleep(DELAY);
98-
} catch (InterruptedException e) {
99-
e.printStackTrace();
97+
channel.disconnect();
10098
}
99+
100+
session.disconnect();
101+
System.out.println("[" + project.getName() + "] Deployment of release [" + artifact.getReleaseTag() + "] to remote [" + remote.getHostname() + "] was successful");
102+
} catch (JSchException | SftpException | IOException e) {
103+
System.out.println("[" + project.getName() + "] Error while deploying release [" + artifact.getReleaseTag() + "] to [" + remote.getHostname() + "]");
104+
System.out.println(e.getMessage());
101105
}
102106
}
103107
}

0 commit comments

Comments
 (0)