5

I am trying to render information obtained from the shell in an active Active Choices parameter with a groovy script. I can easily access the shell from a groovy script in a jenkins pipeline with the sh method like this:

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

But when I try this in the groovy script of Active choices, it crashes and the fallback script is executed.

Is it possible to switch to a node in this context and execute a shell command ?

Thanks for the help!

CurlyFire
  • 712
  • 2
  • 6
  • 18

1 Answers1

11

Here is sample snippet using the active choice plugin.

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()
  • 1
    Not working for me for this command: `grep something ~/path/file | cut -d' ' -f2 | cut -d] -f1` – Varun Chandak Mar 08 '19 at 06:00
  • 1
    Snippet of gold. I needed to fetch a remote file list and populate the active choice plugin. Changed the cmd to ```def command = ['/bin/sh', '-c', 'ssh user@host \'find /tmp/files/*pattern* \' ']``` – wowbagger Jul 15 '20 at 09:09
  • Saved the day. Thank you – vijay Sep 23 '22 at 18:28