在docker容器中执行包含要使用JGit ssh克隆存储库的代码的
通过JGit使用密钥来安全地访问Git存储库
public static void main(String[] args) {
createRepository("git@github.com:username/Docker-Script.git", "", getPassword_1, "master", "/opt/fx/clone");
try {
} catch (Exception ex) {
System.out.println(ex.getLocalizedMessage());
}
}
private static Repository createRepository(String url, String username, String password, String branch, String path) {
Repository repository = null;
try {
CloneCommand cloneCommand = Git.cloneRepository()
.setURI(url)
.setDirectory(new File(path));
// ssh public/private key auth
if (StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
cloneCommand.setTransportConfigCallback(new TransportConfigCallback() {
@Override
public void configure(Transport transport) {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(getSshSessionFactory());
}
});
}
Git git = cloneCommand.call();
repository = git.getRepository();
} catch (GitAPIException ex) {
System.out.println(ex.getLocalizedMessage());
} catch (Exception ex) {
System.out.println(ex.getLocalizedMessage());
}
return repository;
}