I'm new to cloud, I created one Jenkins server and tomcat servers, I've successfully tested the simple CICD implementation. Now I want to know how to deploy my code to multiple servers. for ex. if I have 4 different instances with tomcat installation. Then how do I deploy the single source code to different servers?
You have not mentioned but I assume you are using a Jenkinsfile with declarative pipeline, because you really should. Do not try to configure everything on the webpage, it is not scalable and there is no version control.
First, for steps that is used more than once, I always set it up as a shared library. Basically you setup a git repository for the shared library and import it in the Jenkinsfile. Read this official document for detailed instructions. An easier way to do this quickly is to simply place them at the end of your Jenkinsfile as functions.
In my Jenkinsfile where I have to deploy to more than one location, this is a minimal example of what I would do.
@Library('shared-library') _
pipeline {
agent any
environment
{
DEPLOYMENT_URL_A = 'a.example.com'
DEPLOYMENT_URL_B = 'b.example.com'
}
stages {
stage ('Build') {
steps {
script {
YOUR BUILD STEPS HERE
}
}
}
}
stage ('Deployment') {
steps {
script {
deployment("${deploymentUrlA}")
deployment("${deploymentUrlB}")
}
}
}
}
}
post {
always {
deleteDir()
}
}
}
Note that deployment is a method from the shared-library. It could be something like this (I have a jenkins user setup on all server to deploy remotely).
#!/usr/bin/env groovy
def call(deploymentUrl) {
sh """ssh -o StrictHostKeyChecking=no \\
jenkins@${deploymentUrl} \\
YOUR DEPLOYMENT STEP HERE"""
}
But this is definitely not a scalable solution. For a larger set of dynamic servers you might want to store the server list elsewhere then fetch it during runtime, and pass it as a list to the deployment function.