Skip to content

Instantly share code, notes, and snippets.

@porcelli
Created May 26, 2020 19:20
Show Gist options
  • Save porcelli/1db9d705b744f457b79c3e402c6de412 to your computer and use it in GitHub Desktop.
Save porcelli/1db9d705b744f457b79c3e402c6de412 to your computer and use it in GitHub Desktop.
Code for blog post: Using SSH to push changes to external repository
//common push configuration
final PushCommand pushCommand = git.push() <1>
.setRefSpecs(new RefSpec(ref + ":" + ref))
.setRemote(remoteURL);
//check if ssh should used
if (properties.getUseSSH()) { <2>
// setup of ssh transport config
pushCommand.setTransportConfigCallback(transport -> { <3>
final SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(new JschConfigSessionFactory() {
@Override
protected void configure(OpenSshConfig.Host host, Session session) {
}
});
});
} else {
// if not ssh, it requires credentials
pushCommand.setCredentialsProvider(integration.getCredentialsProvider()); <4>
}
// push changes to the remote repository
pushCommand.call(); <5>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment