Wednesday, December 29, 2021

[SOLVED] syntax error: unexpected end of file (expecting "fi")

Issue

So I have a simple script running in an IF statement. I always get: syntax error: unexpected end of file (expecting "fi") I am wondering what could be the possible solution for this ?

def call(Map config) {
    withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'JENKINS_AWS'],
      sshUserPrivateKey(credentialsId: 'JENKINS-SSH', keyFileVariable: 'SSH_KEY')]) {
        sh """
            #!/bin/bash
            source add_ssh_key "${SSH_KEY}"           
            source init_env "${TARGET_STAGE}"
            source create-bastion-pod "${PROMETHEUS_PUSHGATEWAY}" "${PROMETHEUS_PUSHGATEWAY_PORT}"  
            if [ ${TARGET_STAGE} == 'dev' ]; then
               cat <<-EOF | curl --data-binary @- \${BASTION_URL}/metrics/job/sone_job 
            # TYPE some_metric counter
            some_metric{label="val1"} 42 
            EOF
            fi
            delete-bastion-pod
        """
    }
}

Solution

<<- only strips tabs from the here-document; your closing delimiter appears to be indented (according to what Groovy actually presents to the shell) with a couple of spaces. Try something like

    sh """
        #!/bin/bash
        if [ ${TARGET_STAGE} == 'dev' ]; then
           cat <<EOF | curl --data-binary @- \${BASTION_URL}/metrics/job/some_job 
        # TYPE some_metric counter
        some_metric{label="val1"} 42 
        EOF
        fi
    """

Note that as far as the shell executing the script is concerned, the here-document and the closing EOF aren't indented at all.



Answered By - chepner