如何在 Jenkins 中使用沙盒通过 groovy 脚本在工作流中建立 ssh 连接?我们需要与服务器建立 ssh 连接,并在该服务器上使用特定用户 ID 运行特定脚本..
有什么办法吗?
如何在 Jenkins 中使用沙盒通过 groovy 脚本在工作流中建立 ssh 连接?我们需要与服务器建立 ssh 连接,并在该服务器上使用特定用户 ID 运行特定脚本..
有什么办法吗?
没有诀窍。
sh 'ssh -u someone server some-script'
我对我们的构建系统有类似的要求,我从这个 ssh.gradle 任务开始。
归结为使用 ant 的 sshexec 如下:
class SshTask extends DefaultTask {
// various properties for the host etc
@Input @Optional String host
@Input @Optional String userName
@Input @Optional String password
@Input @Optional String keyfile
@Input @Optional String passphrase
private static boolean antInited = false
SshTask() {
if (!antInited) {
antInited = true
initAnt()
}
}
protected initAnt() {
project.configurations { sshAntTask }
project.dependencies {
sshAntTask "org.apache.ant:ant-jsch:1.8.2"
}
ant.taskdef(name: 'sshexec',
classname: 'org.apache.tools.ant.taskdefs.optional.ssh.SSHExec',
classpath: project.configurations.sshAntTask.asPath, loaderref: 'ssh')
}
def ssh(Object... commandLine) {
def outputAntProperty = "sshoutput-" + System.currentTimeMillis()
if (keyfile != null) {
ant.sshexec(host: host, username: userName, keyfile: keyfile, passphrase: passphrase, command: commandLine.join(' '), outputproperty: "$outputAntProperty")
} else if (password != null) {
ant.sshexec(host: host, username: userName, password: password, command: commandLine.join(' '), outputproperty: "$outputAntProperty")
} else {
throw new GradleException("One of password or keyfile must be set to perform ssh command")
}
def sshoutput = ant.project.properties."$outputAntProperty"
project.logger.lifecycle sshoutput
}
}