0

I'm executing a nodejs script in my pipeline and for some reason Jenkins adds a newline after a variable.

The code:

sh """cd /var/jenkins/fcs/cars/ && node validateCommit.js \"${reporthashref}\" 'Suzuki, Ignis'"""

When the pipeline is called it fails. The console output looks like this:

18:08:07  + node validateCommit.js a0ccb50b
18:08:07   Suzuki, Ignis

A new line is added and the command is doing something else.

I remember I had to change this already 1-2 times. Everything worked and now it shows up again.

Is there a proper way to do it?

Qohelet
  • 1,459
  • 4
  • 24
  • 41

1 Answers1

3

Use the trim function to remove spaces and new lines before and after your variable :

${reporthashref.trim()}
fmdaboville
  • 1,394
  • 3
  • 15
  • 28
  • That was it! Thank you. Bonus-Question: Is there anything like a `.format(...)` method in Jenkins-Groovy the set variables in a more meaningful way? – Qohelet Aug 26 '21 at 12:13
  • 1
    @Qohelet Groovy is a full superset of Java, so there is `java.lang.String.format(...)`. – Gerold Broser Aug 26 '21 at 13:23
  • @GeroldBroser - yes, but actually no. Have you ever tried using Java-functions in Jenkins? Most of them don't work - you have the option to use the full functionality of the groovy-language but lose the Pipeline-features or use the Pipeline-Features. Most of the time I get errors like `Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods execute java.lang.String` Unfortunately Pipeline-Jenkins-Groovy is very different from Vanilla-Groovy – Qohelet Aug 27 '21 at 12:00
  • 1
    You have to authorize this in the admin section. Below you can find an example : https://stackoverflow.com/questions/38276341/jenkins-ci-pipeline-scripts-not-permitted-to-use-method-groovy-lang-groovyobject – fmdaboville Aug 27 '21 at 12:14
  • @Qohelet See _fmdaboville_'s comment and see [my answer to _Output the console text of a Jenkins job that my Job is running_](https://stackoverflow.com/a/31573477/1744774) (though this is not a Pipeline project which didn't exist yet then). – Gerold Broser Aug 27 '21 at 16:33