Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jenkins

groovy

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?

like image 294
Frost Avatar asked Jan 27 '23 03:01

Frost


1 Answers

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)" """
like image 166
Michael Avatar answered May 11 '23 10:05

Michael