Frost
Frost

Reputation: 159

Groovy multiline shell script in Jenkins sh step does not return stdout

I am trying to save the output of a groovy shell script in a variable.

test = sh(returnStdout: true, script: "#!/bin/bash -l && export VAULT_ADDR=http://ourVault.de:8100 && export VAULT_SKIP_VERIFY=true && vault auth ${VAULT_TOKEN} && vault read -field=value test/${RELEASE2}/ID").trim()

But there is no output and I wonder why it does not capture the output?

If I do this:

def test = ""
sh"""#!/bin/bash -l
     export VAULT_ADDR=http://ourVault.de:8100
     export VAULT_SKIP_VERIFY=true
     vault auth ${VAULT_TOKEN}
     ${test}=\"\$(vault read -field=value emea/test/hockey/ios/${RELEASE2}/appID)\"
  """

I see the output in the console. However, it doesn't get captured either. Is there any other way of capturing the output of multiline sh script?

Upvotes: 7

Views: 11568

Answers (1)

Michael
Michael

Reputation: 2683

The ${} syntax is not working that way. It can only be used add content to a string.

The returnStdout option can also be used with triple quoted scripts. So you probably want to do the following:

def test = sh returnStdout:true, script: """
    #!/bin/bash -l
    export VAULT_ADDR=http://ourVault.de:8100 
    export VAULT_SKIP_VERIFY=true
    vault auth ${VAULT_TOKEN}
    echo "\$(vault read -field=value emea/test/hockey/ios/${RELEASE2}/appID)" """

Upvotes: 15

Related Questions