-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed container deletion bug and added git caching
- Loading branch information
1 parent
20f50cf
commit f9c99b7
Showing
8 changed files
with
92 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
simofa/src/main/java/dev/truewinter/simofa/GitFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package dev.truewinter.simofa; | ||
|
||
import dev.truewinter.simofa.common.LogType; | ||
import dev.truewinter.simofa.common.SimofaLog; | ||
import dev.truewinter.simofa.common.Util; | ||
import dev.truewinter.simofa.docker.WebsiteBuild; | ||
import org.apache.commons.io.FileUtils; | ||
import org.eclipse.jgit.api.*; | ||
import org.eclipse.jgit.api.errors.GitAPIException; | ||
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
public class GitFetcher { | ||
public static void fetch(WebsiteBuild build, File tmpInDir, File websiteCacheDir) throws GitAPIException, IOException { | ||
File gitCacheDir = new File(websiteCacheDir, "git"); | ||
|
||
if (gitCacheDir.exists()) { | ||
build.addLog(new SimofaLog(LogType.INFO, "Using git cache")); | ||
pull(build, tmpInDir, websiteCacheDir); | ||
} else { | ||
clone(build, tmpInDir, websiteCacheDir); | ||
} | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
private static void setCredentials(WebsiteBuild build, TransportCommand cloneCommand) { | ||
GitCredential gitCredential = build.getWebsite().getGitCredential(); | ||
if (gitCredential != null) { | ||
cloneCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider( | ||
gitCredential.getUsername(), | ||
gitCredential.getPassword() | ||
)); | ||
} | ||
} | ||
|
||
private static void clone(WebsiteBuild build, File tmpInDir, File websiteCacheDir) throws GitAPIException, IOException { | ||
CloneCommand cloneCommand = Git.cloneRepository() | ||
.setURI(build.getWebsite().getGitUrl()) | ||
.setBranch(build.getWebsite().getGitBranch()) | ||
.setCloneAllBranches(false) | ||
.setDirectory(tmpInDir); | ||
|
||
setCredentials(build, cloneCommand); | ||
cloneCommand.call().close(); | ||
|
||
if (!Util.isBlank(build.getCacheDir())) { | ||
System.out.println("will copy git"); | ||
File gitCacheDir = new File(websiteCacheDir, "git"); | ||
if (!gitCacheDir.exists() && !gitCacheDir.mkdir()) { | ||
build.addLog(new SimofaLog(LogType.WARN, "Unable to create git cache directory for website " + build.getWebsite().getId())); | ||
} else { | ||
System.out.println("copying git"); | ||
FileUtils.copyDirectory(tmpInDir, gitCacheDir); | ||
} | ||
} | ||
} | ||
|
||
private static void pull(WebsiteBuild build, File tmpInDir, File websiteCacheDir) throws IOException, GitAPIException { | ||
File gitCacheDir = new File(websiteCacheDir, "git"); | ||
try (Git git = Git.open(gitCacheDir)) { | ||
PullCommand pullCommand = git.pull(); | ||
setCredentials(build, pullCommand); | ||
pullCommand.call(); | ||
|
||
FileUtils.copyDirectory(gitCacheDir, tmpInDir); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters