Merge pull request #16 from DerDavidBohl/15-compare-commit-ids-and-only-deploy-when-it-differs

Compare commit REFs and only deploy when differs
This commit is contained in:
David Bohl
2025-01-21 06:57:44 +01:00
committed by GitHub
3 changed files with 32 additions and 5 deletions

View File

@@ -2,8 +2,8 @@ package org.davidbohl.dirigent.deployments.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.davidbohl.dirigent.deployments.service.GitService;
import org.davidbohl.dirigent.deployments.models.DeploynentConfiguration;
import org.davidbohl.dirigent.deployments.service.GitService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@@ -26,7 +26,7 @@ public class DeploymentsConfigurationProvider {
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
if (gitUrl != null)
gitService.cloneOrPull(gitUrl, "config");
gitService.updateRepo(gitUrl, "config");
File configFile = new File("config/deployments.yml");

View File

@@ -9,7 +9,6 @@ import org.davidbohl.dirigent.deployments.models.events.NamedDeploymentStartRequ
import org.davidbohl.dirigent.deployments.models.events.SourceDeploymentStartRequestedEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
@@ -163,7 +162,10 @@ public class DeploymentsService {
File deploymentDir = new File("deployments/" + deployment.name());
try {
gitService.cloneOrPull(deployment.source(), deploymentDir.getAbsolutePath());
boolean updated = gitService.updateRepo(deployment.source(), deploymentDir.getAbsolutePath());
if(!updated)
return;
List<String> commandArgs = new java.util.ArrayList<>(Arrays.stream(composeCommand.split(" ")).toList());
commandArgs.add("up");

View File

@@ -6,8 +6,10 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Objects;
@@ -19,21 +21,28 @@ public class GitService {
@Value("${dirigent.git.authToken}")
private String authToken;
public void cloneOrPull(String source, String destination) throws IOException, InterruptedException {
public boolean updateRepo(String source, String destination) throws IOException, InterruptedException {
logger.info("Cloning or pulling git repository '{}' to dir '{}'", source, destination);
File destinationDir = new File(destination);
boolean changed = false;
if (destinationDir.exists() && Arrays.asList(Objects.requireNonNull(destinationDir.list())).contains(".git")) {
logger.debug("Local Repo exists. Pulling latest changes.");
String currentHeadRev = getHeadRev(destinationDir);
new ProcessBuilder("git", "fetch", "--all")
.directory(destinationDir).start().waitFor();
new ProcessBuilder("git", "reset", "--hard", "HEAD")
.directory(destinationDir).start().waitFor();
new ProcessBuilder("git", "pull")
.directory(destinationDir).start().waitFor();
String newHeadRev = getHeadRev(destinationDir);
changed = !currentHeadRev.equals(newHeadRev);
} else {
changed = true;
logger.debug("Local Repo does not exist. Cloning repository.");
deleteDirectory(destinationDir);
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(source);
@@ -46,6 +55,22 @@ public class GitService {
new ProcessBuilder("git", "clone", uriComponentsBuilder.toUriString(), destination)
.start().waitFor();
}
return changed;
}
private static String getHeadRev(File destinationDir) throws IOException, InterruptedException {
Process process = new ProcessBuilder("git", "rev-parse", "HEAD")
.directory(destinationDir).start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
StringBuilder stringBuilder = new StringBuilder();
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
String currentRev = stringBuilder.toString();
process.waitFor();
return currentRev;
}
void deleteDirectory(File directoryToBeDeleted) {