Ubuntu – Bash script if condition not working when called from Jenkins Pipeline

bashJenkinsscripts

The below is my Jenkins Pipeline stage

stage ('Import') {

            steps {
                sshagent([sshCredentials]) {
                    sh '''
                        #!/bin/bash
                        sh -x ETL_AUTOMATION/Scripts/export.sh
                    '''

Build output:-

[[ RMO_TST12 ==  ]]

ETL_AUTOMATION/Scripts/export.sh: 17: ETL_AUTOMATION/Scripts/export.sh: [[: not found

if condition in script:-

if [[ "$SOURCE_FOLDER" == "" ]]; then

    echo "SOURCE_FOLDER not specified... exiting"
    exit 1
fi

I have mentioned #!/bin/bash in my script also.

Please help me, I am really stuck here.

Best Answer

You are running sh -x ETL_AUTOMATION/Scripts/export.sh. This means that export.sh is being run by sh and not bash. On Ubuntu, sh is a simple POSIX shell called dash and that doesn't support the [[ construct, which is a bash thing. So just change your script so that it is launched with bash instead of sh. I don't know the Jenkins syntax at all, but I suspect you want one of these:

sshagent([sshCredentials]) {
    sh '''
        ETL_AUTOMATION/Scripts/export.sh
        '''

or, if your export.sh isn't executable:

sshagent([sshCredentials]) {
    sh '''
        bash ETL_AUTOMATION/Scripts/export.sh
        '''

Or, perhaps simply:

sshagent([sshCredentials]) {
    bash '''
        ETL_AUTOMATION/Scripts/export.sh
        '''

Alternatively, you can change export.sh so it doesn't use bash-only features:

if [ -z $SOURCE_FOLDER ]; then
    echo "SOURCE_FOLDER not specified... exiting"
    exit 1
fi