pvpkiran
pvpkiran

Reputation: 27048

Jenkins Publish Over SSh pipeline parameterized

I am using Publish over SSH plugin in Jenkins to deploy the jar file which is created from the build process. I have created a pipeline like this

node {
 {
        stage('Checkout') {
            git([url: '.............', branch: 'myBranch', credentialsId: 'mycredentials'])
        }
        stage('Build') {
            script{
                sh 'chmod a+x mvnw'
                sh './mvnw clean package'
            }
        }
        stage('Deploy to Server'){
            def pom = readMavenPom file: 'pom.xml'
            script {
                sshPublisher(publishers: [sshPublisherDesc(configName: 'server-instance1',
                        transfers: [sshTransfer(cleanRemote:true,
                                sourceFiles: "target/${env.PROJECT_NAME}-${pom.version}.jar",
                                removePrefix: "target",
                                remoteDirectory: "${env.PROJECT_NAME}",
                                execCommand: "mv ${env.PROJECT_NAME}/${env.PROJECT_NAME}-${pom.version}.jar ${env.PROJECT_NAME}/${env.PROJECT_NAME}.jar"),
                                    sshTransfer(
                                            execCommand: "/etc/init.d/${env.PROJECT_NAME} restart -Dspring.profiles.active=${PROFILE}"
                                    )
                        ])
                ])
            }
        }
    }
}

This works. I have a SSH Server configured under Manage Jenkins >> Configure System >> Publish Over SSH.

Now I want to deploy on multiple servers. Lets say I create multiple ssh configurations by name server-instance1, server-instance2. How do I make this Jenkins job parameterized ? I tried with checking the checkbox and selecting a Choice Parameter. But I am not able to figure out how to make the values for this dropdown come from the SSH server list(instead of hardcoding)

I tried few things as mentioned here(How to Control Parametrized publishing in Jenkins using Publish over SSH plugin's Label field). Unfortunately none of the articles talks about doing this from a pipeline.

Any help is much appreciated.

Upvotes: 1

Views: 2630

Answers (1)

Noam Helmer
Noam Helmer

Reputation: 6824

If you want to select the SSH server name dynamically, you can use the Extended Choice Parameter plugin which allows you to execute groovy code that will create the options for the parameter.
In the plugin you can use the following code to get the values:

import jenkins.model.*
def publish_ssh = Jenkins.instance.getDescriptor("jenkins.plugins.publish_over_ssh.BapSshPublisherPlugin")

configurations = publish_ssh.getHostConfigurations() // get all server configurations
return configurations.collect { it.name } // return the list of all servers

To configure this parameter in you pipeline you can use the following code for scripted pipeline:

properties([
      parameters([
            extendedChoice(name: 'SERVER', type: 'PT_SINGLE_SELECT', description: 'Server for publishing', visibleItemCount: 10, 
            groovyScript: '''
                import jenkins.model.*
                def publish_ssh = Jenkins.instance.getDescriptor("jenkins.plugins.publish_over_ssh.BapSshPublisherPlugin")
                return publish_ssh.getHostConfigurations() .collect { it.name }
            ''')
      ])
])

Or the following code for declarative pipeline:

pipeline {
     agent any
     parameters {
          extendedChoice(name: 'SERVER', type: 'PT_SINGLE_SELECT', description: 'Server for publishing', visibleItemCount: 10,
               groovyScript: '''
               import jenkins.model.*
               def publish_ssh = Jenkins.instance.getDescriptor("jenkins.plugins.publish_over_ssh.BapSshPublisherPlugin")
               return publish_ssh.getHostConfigurations() .collect { it.name }
          ''')
     }
     ...
}   

Once the parameter is defined just use it in your sshPublisher step:

sshPublisher(publishers: [sshPublisherDesc(configName: SERVER, transfers: ...

Another option you can have when using the Extended Choice Parameter is to configure it as Multi-Select instead of Single-Select so a user can then select multiple severs, and you can use the parallel option to publish over all selected servers in parallel.

Upvotes: 2

Related Questions