title: Jenkins declarative pipeline ssh agent plugin
body: <p>I'm trying to use sshagent plugin to deploy to remote server.</p>
  <p>when using below syntax, I'm getting</p>
  <pre><code>pipeline {
      agent any
      stages {
          stage('Deploy') {
              steps {
                  sshagent(['nginx-ec2']) {
                      // some block
                      sh &quot;ssh -o StrictHostKeyChecking=no ubuntu@&lt;host_ip&gt;&quot;
                      sh &quot;whoami&quot;
                  }
              }
          }
      }
  }
  </code></pre>
  <p>getting output:</p>
  <pre><code>[Pipeline] sh (hide)
  + whoami
  jenkins
  </code></pre>
  <p>while I'm expecting to run script at the remote server using provided credentials !!</p>
  <p>So, I had to run it this way</p>
  <pre><code>pipeline {
      agent any
      stages {
          stage('Deploy') {
              steps {
                  sshagent(['nginx-ec2']) {
                      // some block
                      sh &quot;ssh -o StrictHostKeyChecking=no -l ubuntu &lt;host_ip&gt; 'whoami &amp;&amp; \
                      sudo apt update  &amp;&amp; sudo apt install -y docker.io &amp;&amp; \
                      sudo usermod -aG docker ubuntu &amp;&amp; \
                      source .bashrc &amp;&amp; \
                      docker run -d nginx'&quot;
                  }
              }
          }
      }
  }
  </code></pre>
  <p>Is there any &quot;clean&quot; way to run script on remote server with ubuntu instead of jenkins user ?</p>
  <p>Edit:</p>
  <p>I understand I need to run it under the ssh command not as separate sh script otherwise, it will run as jenkins and I'm able to do it in the scripted way as below.</p>
  <p>That's why I'm asking if there's a better way to write it in the declarative way.</p>
  <pre><code>node {
     stage('Deploy'){
       def dockerRun = &quot;whoami &amp;&amp; \
              sudo apt update  &amp;&amp; sudo apt install -y docker.io &amp;&amp; \
              sudo usermod -aG docker ubuntu &amp;&amp; \
              source .bashrc &amp;&amp; \
              docker run -d nginx &quot;
              
       sshagent(['nginx-ec2']) {
         sh &quot;ssh -o StrictHostKeyChecking=no ubuntu@&lt;host_ip&gt; '${dockerRun}' &quot;
       }
     }
  }
  </code></pre>
  <p>Thanks,</p>