shelljenkinssshksh

Jenkins double ssh only do the fist ssh step and doesn't do all the things asked


I am currently building a big jenkins job, and I need, in the same stage and step to do multiple ssh as showed in the code below. The problem is that everytime jekins finishes the first ssh connection, it ends the stage, and the other parts are not ran.

Does anyone have the solution?

Here is my stage code:

stage('(02) Export file') {
            withCredentials([gitUsernamePassword(credentialsId: gitCredentials, gitToolName: 'Default')]) {
                    sh '''
                        ssh user@env << EOF
                            mkdir /alim1 || true
                            mkdir /alim2 || true
                            mkdir /alim3 || true
                        << EOF

                        scp script1.ksh user@env:/alim1
                        scp script2.ksh user@env:/alim2
                        scp script3.ksh user@env:/alim3
                            
                        ssh user@env << EOF
                            echo "Starting export"
                            ksh /alim1/script1 || error1=1
                            ksh /alim2/script2 || error2=1
                            ksh /alim3/script3 || error3=1 
                        << EOF;
                    '''
            }
}

EDIT: I changed the here-docs end, it now look like that:

stage('(02) Export file') {
            withCredentials([gitUsernamePassword(credentialsId: gitCredentials, gitToolName: 'Default')]) {
                    sh '''
                       ssh user@env << EOF 
                           mkdir /alim1 || true; 
                           mkdir /alim2 || true; 
                           mkdir /alim3 || true; 
                       EOF

                       scp script1.ksh user@env:/alim1
                       scp script2.ksh user@env:/alim2
                       scp script3.ksh user@env:/alim3

                       ssh user@env << EOF
                           echo "Starting export"
                           ksh /alim1/script1 || error1=1;
                           ksh /alim2/script2 || error2=1;
                           ksh /alim3/script3 || error3=1;
                       EOF
                       '''
            }
}

I get this error: -ksh[11]: EOF: not found [No such file or directory of this type]


Solution

  • The answer to the problem was quite simple. I lacked knowledge about here-document and I didn't know that EOF needed to be the only caracters on the line, there shouldn't be any indentation. The working/corrected code is this one:

    stage('(02) Export file') {
                withCredentials([gitUsernamePassword(credentialsId: gitCredentials, gitToolName: 'Default')]) {
                        sh '''
                           ssh user@env << EOF 
                               mkdir /alim1 || true; 
                               mkdir /alim2 || true; 
                               mkdir /alim3 || true; 
    EOF
    
                           scp script1.ksh user@env:/alim1
                           scp script2.ksh user@env:/alim2
                           scp script3.ksh user@env:/alim3
    
                           ssh user@env << EOF
                               echo "Starting export"
                               ksh /alim1/script1 || error1=1;
                               ksh /alim2/script2 || error2=1;
                               ksh /alim3/script3 || error3=1;
    EOF
                           '''
                }
    }