The popular Continuous Integration tool ‘Jenkins’ comes with multiple plugins which increase the functionality of Jenkins many folds and helps DevOps achieve automation in many ways. One of the most important plugins it support is Publish Over SSH, which helps to transfer artifacts to any SSH server and/or execute commands over SSH session. One of the important features of this plugin is to support Parameterized publishing.
This post will tell you how to use parameterized publishing with Jenkins. There are scenarios when we want to use Publish plugin only for some set of servers and want to skip for others. Suppose there are two type of servers (say App & API) in different environments where we usually deploy and do some changes through SSH, or using Publish Over SSH Plugin.
Now, there can be multiple situations where we want to deploy few artifacts on all the app servers irrespective of the environment or deploy artifacts on complete production environment irrespective of the type of server.
The first thing you will do is to add those servers on the Jenkins – ‘Configure Systems.’
You create a parameterized Job where you use a String Parameter to enter the expression which matches it against the LABEL of the servers and execute accordingly:
Next thing is to configure the Job using Publish Over SSH Plugin. You just add a Build step: Send Files or execute commands over SSH. Here you select the server where you want to execute this section:
The server selected here is cw-staging-app. You must add a LABEL for this server. You can choose any label here. Just for understanding, let us use the same name. It must be noted that the expression on the aforementioned Parameter will match this label to decide which one to use and which one to skip.
Similarly, add other servers for the same action by using ‘Add Server’ button just at the below of the build step. So, at last, you will have other three servers added as well like below:
Now, the last step is to add Parameterized Publishing Parameter Name for this build step. You will find that just below the ‘Add Server’ button and above the ‘Add Build Step’ drop down. Add the Parameter name same as what you have used in Parameterized Job. This means that anything mentioned on DEPLOYMENT parameter will be matched against the LABELs of the servers for the section.
Now, it’s time to run the Job!
In case the regex pattern is wrong or does not matches it will give skip all the server sections:
In case regex pattern matches for all the app servers irrespective of the environment name:
For example: .*-app$
In case regex pattern matches for all the prod servers irrespective of the type of server:
For example: .*-prod.*-.*
If a Label is missing on a server it will default to the empty string. You can use the same label multiple times, e.g. UAT for a web server and again for the database server. Labels can also use the standard Jenkins variables e.g. $NODE_NAME.
So, when you would like to have a single Jenkins job that accepts a parameter to build for different environments i.e. Dev, Stage, Prod etc you can definitely consider Parameterised Publishing on Jenkins.