2

I've read a lot here and then but still cannot find how to fix this:

steps.wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'XTerm']) {
    steps.withCredentials([steps.usernamePassword(credentialsId: "test", usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {
        steps.sh """                                                      
        curl  --silent -u \${GIT_USERNAME}:\${GIT_PASSWORD} -H "Content-Type: application/json" -X POST https://some.url --data-binary @- <<-EOF
        {
        "state": "$STATE",
        "key": "\$JOB_NAME",
        "name": "\$BUILD_TAG",
        "url": "\$BUILD_URL",
        "description": "$DESCRIPTION"
        }
        EOF"""

This resides in jenkins shared library. I've read that I have to avoid whitespaces, but I don't see what whitespaces I've created here. I keep receiving this error:

line 11: warning: here-document at line 2 delimited by end-of-file (wanted `EOF')
2

1 Answer 1

5

Apart from potentially creating invalid JSON unless you can assume that your variables are already JSON-encoded, you have an indentation issue with your here-document.

You are using <<-EOF to start the here-document containing your JSON document. Such a here-document should be delimited by the string EOF and this string needs to be flush left (no indentation at all) or it may be indented using tab-characters (not spaces). There may be no whitespace or other characters of any kind on the same line after the EOF string.

My guess is that the code is indented using spaces, which is why the shell's parser can't find the terminating EOF string.

5
  • I've tried with tabs only but I still get it: { "state": "$STATE", "key": "\$JOB_NAME", "name": "\$BUILD_TAG", "url": "\$BUILD_URL", "description": "$DESCRIPTION" } EOF
    – WhoAmI
    Jul 8, 2021 at 10:38
  • @WhoAmI I'm not familiar with Jenkins, so I don't know what transformations it applies to quoted strings before it's passed to a shell interpreter. (and judging from random forum posts I'm seeing, neither do people that actually use it)
    – Kusalananda
    Jul 8, 2021 at 10:39
  • @WhoAmI, please check if you have spaces, tabs or \r chars after the "EOF"
    – JJoao
    Jul 8, 2021 at 10:56
  • I changed my example and put the 3 double quotes immidiately after EOF to show that there are no whitespaces
    – WhoAmI
    Jul 8, 2021 at 11:00
  • @WhoAmI It still looks as if your editor i using multiples of four spaces for indentation, not spaces. As I also said, it's unclear what Jenkins does with the triple-double-quoted string before handing it over to a shell interpreter. You may want to try removing all indentation on the EOF line.
    – Kusalananda
    Jul 8, 2021 at 11:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .