12

i am trying to ssh into a remote host and then execute certain commands on the remote host's shell. Following is my pipeline code.

pipeline {
    agent any
    environment {
        // comment added
         APPLICATION = 'app'
         ENVIRONMENT = 'dev'
         MAINTAINER_NAME = 'jenkins'
         MAINTAINER_EMAIL = 'jenkins@email.com'
    }
    stages {
         stage('clone repository') {
             steps {
                 // cloning repo
                 checkout scm
             }
         }
         stage('Build Image') {
             steps {
                 script {
                     sshagent(credentials : ['jenkins-pem']) {
                        sh "echo pwd"
                        sh 'ssh -t -t ubuntu@xx.xxx.xx.xx -o StrictHostKeyChecking=no'
                        sh "echo pwd"
                        sh 'sudo -i -u root'
                        sh 'cd /opt/docker/web'
                        sh 'echo pwd'
                    }
                 }
             }
         }
     }
}

But upon running this job it executes sh 'ssh -t -t ubuntu@xx.xxx.xx.xx -o StrictHostKeyChecking=no' successfully but it stops there and does not execute any further commands. I want to execute the commands that are written after ssh command inside the remote host's shell. any help is appreciated.

Shoaib Iqbal
  • 2,420
  • 4
  • 26
  • 51

4 Answers4

9

I would try something like this:

sshagent(credentials : ['jenkins-pem']) {
  sh "echo pwd"
  sh 'ssh -t -t ubuntu@xx.xxx.xx.xx -o StrictHostKeyChecking=no "echo pwd && sudo -i -u root && cd /opt/docker/web && echo pwd"'
}
Prikkeldraad
  • 1,347
  • 9
  • 14
9

I resolve this issue

script 
{
    sh """ssh -tt login@host << EOF 
    your command
    exit
    EOF"""
}
Alexey
  • 91
  • 1
  • 2
2
    stage("DEPLOY CONTAINER"){
        steps {
            script {
                    sh """
                    #!/bin/bash
                    sudo ssh -i /path/path/keyname.pem username@serverip << EOF
                    sudo bash /opt/filename.sh
                    exit 0
                    << EOF
                    """
                }
        }
    }
1

There is a better way to run commands on remote using SSH. I know this is late answer but I just explored this thing so would like to share and this will help others to resolve this problem easily.

I just found this link helpful on how to run multiple commands on remote using SSH. Also we can run multiple commands conditionally as mentioned in above blog. By going through it, I found the syntax:

ssh username@hostname "command1; command2;commandN"

Now, how to run command inside remote hots using SSH in Jenkins pipeline?

Here is the solution:

pipeline {
  agent any
  environment {
    /*
     define your command in variable
     */
    remoteCommands =
      """java --version;
    java --version;
    java --version """
  }
  stages {
    stage('Login to remote host') {
      steps {
        sshagent(['ubnt-creds']) {
          /*
              Provide variable as argument in ssh command
          */
          sh 'ssh -tt username@hostanem $remoteCommands'
        }
      }
    }
  }
}

Firstly and optionally, you can define a variable that holds all commands separated by ;(semicolon) and then pass it as parameter in command.

Another way, you can also pass your commands directly to ssh command as

sh "ssh -tt username@hostanem 'command1;command2;commandN'"

I have used it in my code and it's working great! see the output here

Happy Learning :)

Vladimir
  • 615
  • 5
  • 12