This step provides an easy way to enable ssh keyagent support for your shell commands.
When you use this wrapper the step will try to automatically provide the correct key for the given targetUrl.
Especially in company environments where you may have one SSH account
for your testing environments the scpTransfer
task makes your life a
lot easier by automatically setting up the ssh keyagent.
If you provide a JSON file at this location
resources/credentials/ssh/credentials.json
in the format described in
Credentials the step will
automatically try to lookup the SSH credentials for the target host and
uses the credentials for authentication.
This step uses the best match by using the PatternMatcher so the SSH credentials with the most matching characters will be used for the sshagent.
When no targets are provided the step will not wrap the body into the sshagent
step.
💡 At the moment only authentication via SSH keys stored in the Jenkins instance are supported.
String targetHost = "testserver.yourcompany.de"
sshAgentWrapper(targetHost)
sh "ssh [email protected] 'pwd'"
}
In this example we are providing the
CredentialAware
ScpCommandBuilderImpl
.
During the ssh credential autolookup the found credentials are
automatically added to the SSHTarget
object. With this information we are
able to use the username configured in the credential for building the command.
import io.wcm.devops.jenkins.pipeline.shell.ScpCommandBuilderImpl
import io.wcm.devops.jenkins.pipeline.ssh.SSHTarget
ScpCommandBuilderImpl commandBuilder = new ScpCommandBuilderImpl((DSL) this.steps)
commandBuilder.setHost("testserver.yourcompany.de")
commandBuilder.setSourcePath("/path/to/source")
commandBuilder.setDestinationPath("/path/to/destination/")
commandBuilder.addArgument("-r")
SSHTarget sshTarget = new SSHTarget(host)
// use the sshAgentWrapper for ssh credential auto lookup
sshAgentWrapper([sshTarget]) {
commandBuilder.setCredential(sshTarget.getCredential())
// build the command
command = commandBuilder.build()
// execute the command
sh(command)
}
import io.wcm.devops.jenkins.pipeline.ssh.SSHTarget
List<SSHTarget> sshTargets = [
new SSHTarget("testserver1.yourcompany.de"),
new SSHTarget("testserver2.yourcompany.de"),
]
sshAgentWrapper(sshTargets)
sh "ssh [email protected] 'pwd'"
sh "ssh [email protected] 'pwd'"
}