How to use publish over ssh plugin in pipeline

16,122

Solution 1

If you are using a pipeline project and a Jenkinsfile, then all you need to do is go into your project in Jenkins and click configure. In the pipeline section of the configuration, at the bottom there is a link "pipeline syntax". It will take you to the Snippet Generator. Its self explanatory and in our case it allows to generate "publish over ssh" snippets that you would add to your Jenkinsfile (add it to a steps section inside a stage definition). In the generator you can define what to publish, options to run a shell command, etc. source

Solution 2

In case you were looking for the syntax for a declarative pipeline (Jenkinsfile) for Publish-Over-SSH, (Instead of the scripted pipeline, which is all I could find). Here's what finally worked for me.

pipeline{
  agent any
  environment {
    RELEASENAME="yourProject-ci"
  }
  stages{
    stage("Get the charts..."){
        steps {checkout scm}
    }
    stage('SSH transfer') {
        steps([$class: 'BapSshPromotionPublisherPlugin']) {
            sshPublisher(
                continueOnError: false, failOnError: true,
                publishers: [
                    sshPublisherDesc(
                        configName: "kubernetes_master",
                        verbose: true,
                        transfers: [
                            sshTransfer(execCommand: "/bin/rm -rf /opt/deploy/helm"),
                            sshTransfer(sourceFiles: "helm/**",)
                        ]
                    )
                ]
            )
        }
    }
    stage('Deploy Helm Scripts'){
        steps([$class: 'BapSshPromotionPublisherPlugin']) {
            sshPublisher(
                continueOnError: false, failOnError: true,
                publishers: [
                    sshPublisherDesc(
                        configName: "kubernetes_master",
                        verbose: true,
                        transfers: [
                            sshTransfer(execCommand: "cd /opt/deploy/helm;helm upgrade ${RELEASENAME} . --install"),
                        ]
                    )
                ]
            )
        }
    }
  }
}

I have a checkout that happens first and then I copy some helm charts from the checkout to my kubernetes master and then run the charts.

configName: "kubernetes_master" is something I setup in the Publish_over_ssh plugin configuration section (Found under Manage Jenkins > Configure System) so I could reference it. It includes a username, sshkey, destination hostname, and base directory for the destination which I put as /opt/deploy.

FYI execCommand does not use the base directory... it assumes you will use full pathing.

Hope that helps.

edit: I should probably mention that there are lots more options for the sshPublisher than what I used. You can find them here: https://jenkins.io/doc/pipeline/steps/publish-over-ssh/

Solution 3

Based on levis answer, the below has worked for me.

stage('Deploy') {
  agent any
  steps {
    sh 'mv target/my-app-0.0.1-SNAPSHOT.jar my-app.jar'
    sshPublisher(
      continueOnError: false, 
      failOnError: true,
      publishers: [
        sshPublisherDesc(
          configName: "my-ssh-connection",
          transfers: [sshTransfer(sourceFiles: 'my-app.jar')],
          verbose: true
        )
      ]
    )
  }
}
Share:
16,122
Chris
Author by

Chris

Updated on June 15, 2022

Comments

  • Chris
    Chris almost 2 years

    I would like to SSH to linux server from Jenkins hosted on windows and execute a command over in linux machine, I tried installing publish over ssh plugin and tested the connection in global config and it works fine, I don't know how to proceed next in pipeline. Any help would be appreciated.