9

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.

Masoud Mokhtari
  • 2,390
  • 1
  • 17
  • 45
Chris
  • 183
  • 1
  • 2
  • 11
  • Make sure to add what you have tried so far and what went wrong. Also try to include output and/or errors so we can better help you. Remember, Stack Overflow is not a code-writing service, but there are a lot of helpful people here that are willing to take a look if you make an effort to explain your problem. – Pax Vobiscum Feb 14 '18 at 12:55
  • 2
    the plugin help page has no information on pipelines, it has information only for other types, that is why i have clearly mentioned that i dont know how to proceed for pipeline. https://wiki.jenkins.io/display/JENKINS/Publish+Over+SSH+Plugin – Chris Feb 14 '18 at 13:48
  • I made a ticket for it as I'd like to know myself https://issues.jenkins-ci.org/browse/JENKINS-49728 – Eman Feb 25 '18 at 02:52
  • This is a similar question https://stackoverflow.com/questions/44377238/use-ssh-credentials-in-jenkins-pipeline-with-ssh-scp-or-sftp – Jorge Nunez Newton Jul 05 '18 at 22:03

5 Answers5

8

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

J Selecta
  • 171
  • 4
  • 15
4

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/

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
        )
      ]
    )
  }
}
The Fool
  • 16,715
  • 5
  • 52
  • 86
1

I got this question some time ago, and here is the answer. Change the code according to your requirement.

pipeline {
    agent any
    options { timestamps () }
    stages {
        stage('Publish over ssh plugin in pipeline') {
            steps([$class: 'BapSshPromotionPublisherPlugin']) {
                script {
                    List SERVERS_LIST = ["Server_1", "Server_2"]
                    for(cr_server in SERVERS_LIST){
                        sshPublisher(
                            publishers: [
                                sshPublisherDesc(
                                    configName: cr_server, 
                                    transfers: [
                                        sshTransfer(
                                            cleanRemote: false, 
                                            excludes: '', 
                                            execCommand: '', 
                                            execTimeout: 120000, 
                                            flatten: false, 
                                            makeEmptyDirs: false, 
                                            noDefaultExcludes: false, 
                                            patternSeparator: '[, ]+', 
                                            remoteDirectory: '', 
                                            remoteDirectorySDF: false, 
                                            removePrefix: '', 
                                            sourceFiles: '**/*'
                                        )
                                    ], 
                                    usePromotionTimestamp: false, 
                                    useWorkspaceInPromotion: false, 
                                    verbose: false
                                )
                            ]
                        )
                    }
                }
            }
        } 
    }
}
Andrew
  • 124
  • 1
  • 12
-2

I don't know how helpful this'll be but I found a tutorial on something that should work until they have a nicer way to do it.

Eman
  • 1,093
  • 2
  • 26
  • 49