2

I have a setup at work where the vSphere host are manually restarted before execution of a specific jenkins job, as a noob in the office I automated this process by adding a extra build step to restart vm's with the help of https://wiki.jenkins-ci.org/display/JENKINS/vSphere+Cloud+Plugin! (vSphere cloud plugin). I would like to now integrate this as a pipeline code, please advise. I have already checked that this plugin is Pipeline compatible.

I currently trigger the vSphere host restart in pipeline by making it to remotely trigger a job configured with vSphere cloud plugin.

pipeline {
    agent any
    stages {
        stage('Restarting vSphere') {
            steps {
            script {
                sh "curl -v 'http://someserver.com/job/Vivin/job/executor_configurator/buildWithParameters?Host=build-114&token=bonkers'"                
            }
            }
        }
        stage('Setting Executors') {
            steps {
                script {
                def jenkins = Jenkins.getInstance()
                jenkins.getNodes().each { 
                if (it.displayName == 'brewery-133') {
                echo 'brewery-133'
                it.setNumExecutors(8)
                }                    
                }
                }
            }
        }

    }
}

I would like to integrate the vSphere cloud plugin directly in the Pipeline code itself, please help me to integrate.

pipeline {
    agent any
    stages {
        stage('Restarting vSphere') {
            steps {

            vSphere cloud plugin code that is requested

            }
            }
        }
        stage('Setting Executors') {
            steps {
                script {
                def jenkins = Jenkins.getInstance()
                jenkins.getNodes().each { 
                if (it.displayName == 'brewery-133') {
                echo 'brewery-133'
                it.setNumExecutors(8)
                }                    
                }
                }
            }
        }

    }
}
Vivin
  • 146
  • 1
  • 8

1 Answers1

2

Well I found the solution myself with the help of 'pipeline-syntax' feature found in the menu of a Jenkins pipeline job.

'Pipeline-syntax' feature page contains syntax of all the possible parameters made available via the API of the installed plugins of a Jenkins server, using which we can generate or develop the syntax based on our needs.

http://<jenkins server url>/job/<pipeline job name>/pipeline-syntax/

enter image description here

My Jenkinsfile (Pipeline) now look like this

pipeline {
    agent any
    stages {
        stage('Restarting vSphere') {
            steps {
                vSphere buildStep: [$class: 'PowerOff', evenIfSuspended: false, ignoreIfNotExists: false, shutdownGracefully: true, vm: 'brewery-133'], serverName: 'vspherecentral'
                vSphere buildStep: [$class: 'PowerOn', timeoutInSeconds: 180, vm: 'brewery-133'], serverName: 'vspherecentral'
            }
        }        
        stage('Setting Executors') {
            steps {
                script {
                    def jenkins = Jenkins.getInstance()
                    jenkins.getNodes().each { 
                        if (it.displayName == 'brewery-133') {
                        echo 'brewery-133'
                        it.setNumExecutors(1)
                        }                    
                    }
                }
            }
        }
    }
}
Vivin
  • 146
  • 1
  • 8