'Jenkinsfile and BASH shell code - Why some comments lines are honored and some are ignored and giving errors

Jenkins version: 2.164.3

I have my Jenkinsfile and in one of the stage, I'm calling the following code but it gives me an error. I don't see why it's giving me an error when syntax wise, it looks ok.

Jenkinsfile code snapshot:

 stages {
     stage("Checkout Dependent Code") {
         steps {
             sh  '''
                     set +x
                     echo -e "\n-- Checking out dependent code:"

                     echo -e "\n-- Cloning Common Code.\n"
                     git clone -b ${COMMON_REPO_BRANCH} ${SSH_GIT_URL}/project/common_utilities.git

                     ## Comment 1. Old repo
                     echo -e "\n-- Cloning Exporter Tool\n"
                     git clone -b ${TOOL_REPO_BRANCH} ${SSH_GIT_URL}/project/jira-exporter.git

                     ## Comment 2. New - 3 new repos. Comment the code for now.
                     #echo -e "\n-- Cloning Some Exporter Tool Repos\n"
                     #for r in core client extractor;
                     #do
                     #    echo -e "\n   -- Cloning: ${r}"
                     #    git clone -b ${TOOL_REPO_BRANCH} ${SSH_GIT_URL}/project/${r}.git
                     #    echo
                     #done

                     echo -e "\n\n`echo -en "\n-- Current Directory: "; pwd; echo; ls -l`\n\n"
                 '''
         }
     }
 }

The error message that I'm getting is:

10:38:38 -- Checking out dependent code:
10:38:38 
10:38:38 -- Cloning Common Code.
10:38:38 
10:38:38 Cloning into 'common_utilities'...
10:38:39 
10:38:39 -- Cloning Exporter Tool
10:38:39 
10:38:39 Cloning into 'jira-exporter'...
10:38:39 /jenkins_workspaces/workspace/development/project/my_jenkins_job/251@tmp/durable-f88d4c2a/script.sh: line 21: --: command not found

Question:

The first comment (Comment 1 line) in the shell script logic, is honored by Jenkinsfile and no syntax issue there and we can see the output the following echo and git command works (I can see in my workspace, git repository has been cloned successfully).

Starting 2nd comment Comment 2. line onwards, the next few lines are all commented out in Shell logic but script fails for a line (which is commented out) somewhere before the last echo line where I'm printing Current Directory: .. line and this last echo line is not printed at all (as error happened before reaching this last echo line. If all lines before this last echo line were commented, then why did I get an error. Running the shell code (from a file) works fine on the machine.



Solution 1:[1]

I'm not sure what the exact issue, but try rewriting the script using more well-defined commands like printf.

stages {
     stage("Checkout Dependent Code") {
         steps {
             sh  '''
                 set +x
                 printf '\n-- Checking out dependent code:\n'

                 printf '\n-- Cloning Common Code.\n\n'
                 git clone -b "${COMMON_REPO_BRANCH}" ${SSH_GIT_URL}/project/common_utilities.git

                 ## Comment 1. Old repo
                 printf '\n-- Cloning Exporter Tool\n'
                 git clone -b "${TOOL_REPO_BRANCH}" "${SSH_GIT_URL}/project/jira-exporter.git"

                 ## Comment 2. New - 3 new repos. Comment the code for now.
                 #echo -e "\n-- Cloning Some Exporter Tool Repos\n"
                 #for r in core client extractor;
                 #do
                 #    echo -e "\n   -- Cloning: ${r}"
                 #    git clone -b ${TOOL_REPO_BRANCH} ${SSH_GIT_URL}/project/${r}.git
                 #    echo
                 #done

                 printf '\n\n\n-- Current Directory '
                 pwd
                 printf '\n'
                 ls -l
                 printf '\n\n'
             '''
         }
     }
 }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chepner