【问题标题】:How to execute shell from Active Choices Plugin groovy script in Jenkins如何在 Jenkins 中从 Active Choices Plugin groovy 脚本执行 shell
【发布时间】:2018-02-28 16:24:28
【问题描述】:

我正在尝试使用 groovy 脚本在活动的 Active Choices 参数中呈现从 shell 获得的信息。我可以使用 sh 方法从 jenkins 管道中的 groovy 脚本轻松访问 shell,如下所示:

node()
{
   sh 'git log ...'
}

但是当我在 Active 选项的 groovy 脚本中尝试此操作时,它会崩溃并执行回退脚本。

是否可以切换到此上下文中的节点并执行shell命令?

感谢您的帮助!

【问题讨论】:

    标签: jenkins jenkins-plugins jenkins-pipeline jenkins-groovy


    【解决方案1】:

    这里是使用主动选择插件的示例 sn-p。

    def command = $/aws ec2 describe-instances \
                   --filters Name=tag:Name,Values=Test \
                   --query Reservations[*].Instances[*].PrivateIpAddress \
                   --output text /$
    def proc = command.execute()
    proc.waitFor()              
    
    def output = proc.in.text
    def exitcode= proc.exitValue()
    def error = proc.err.text
    
    if (error) {
        println "Std Err: ${error}"
        println "Process exit code: ${exitcode}"
        return exitcode
    }
    
    //println output.split()
    return output.tokenize()
    

    【讨论】:

    • 这个命令不适合我:grep something ~/path/file | cut -d' ' -f2 | cut -d] -f1
    • 黄金片段。我需要获取远程文件列表并填充活动选择插件。将 cmd 更改为 def command = ['/bin/sh', '-c', 'ssh user@host \'find /tmp/files/*pattern* \' ']
    猜你喜欢
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多