【问题标题】:How to make ssh connection in workflow through groovy script by using sand box in Jenkins?如何在 Jenkins 中使用沙盒通过 groovy 脚本在工作流中建立 ssh 连接?
【发布时间】:2015-07-23 16:12:26
【问题描述】:

如何在 Jenkins 中使用沙盒通过 groovy 脚本在工作流中建立 ssh 连接? 我们需要与服务器建立 ssh 连接并在该服务器上使用特定用户 ID 运行特定脚本..

有什么办法吗?

【问题讨论】:

    标签: groovy jenkins jenkins-cli groovyshell jenkins-workflow


    【解决方案1】:

    没有诀窍。

    sh 'ssh -u someone server some-script'

    【讨论】:

    • 问题要求调用者是 groovy,而不是 sh。
    【解决方案2】:

    我对我们的构建系统有类似的要求,我从this ssh.gradle task开始。

    归结为使用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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多