0

Jenkinsfile contents:

pipeline {
  environment {
    SOMEVAR = "${sh(returnStdout: true, script: "node -p -e "require('./package.json').version")}"
  }
}

Throws an error about unexpected char.

Just tried:

SOMEVAR = /${sh(returnStdout: true, script: 'echo 1234')}/

It works, but then i tried

SOMEVAR = /${sh(returnStdout: true, script: 'node -v')}/

It returned: "node: command not found"

Alexander Kim
  • 17,304
  • 23
  • 100
  • 157
  • Can you provide the error message? You have to escape chars like backslash by the way, see: https://stackoverflow.com/questions/36547680/how-to-do-i-get-the-output-of-a-shell-command-executed-using-into-a-variable-fro?rq=1 – Michael Kemmerzell Jan 23 '20 at 13:14
  • Updated my question, in a later steps i actually can use `node` or `npm`, but not in an environment block, any ideas? – Alexander Kim Jan 23 '20 at 14:24

1 Answers1

0
script {
    env.SOMEVAR = sh script: 'node -v'
}

Try to put this block after 'steps {' in first stage of your pipeline. You use Declarative pipeline, not Scripted pipeline syntax.

Dmitry G.
  • 51
  • 1
  • 3