⬆️ ⬇️

Jenkins Pipeline Shared Libraries

Hello. In this article I want to share the knowledge gained in the process of automating the deployment of our services to different servers in different data centers.



The task was the following: there is a certain set of scripts for deploying services that need to be run on each server of each data center. Scripts perform a series of operations: status check, output from under load balancer, release version, deployment, status check, sending notifications via email and Slack, etc. This is simple and convenient, but as the number of data centers and services grows, the process of rolling out a new version can take a whole day. In addition, some commands are responsible for some actions, for example, the setting of a load balancer. I also wanted the process control code to be stored in a public repository, so that each team member could support it.



The problem was solved with the help of Jenkins Pipeline Shared Libraries: the process steps were divided visually into logical parts, the code is stored in the repository, and it became possible to deliver to 20 servers in one click. Below is an example of a similar test project:

')

image



Now I will tell and show examples of how to achieve this. I hope this article will help save time to other developers, and I will also be glad to make useful comments.



Creating a library



The first thing you need to do is create your own library in which our functions will be stored.

Create a new repository, which should have the following structure:



image

The src directory is used for Groovy classes that are added to the classpath when executing Pipeline.



The vars directory is used in scripts that define global variables accessible from Pipeline.



Below is an example Groovy class



@Grab(group = 'org.apache.commons', module = 'commons-lang3', version = '3.6') import org.apache.commons.lang3.StringUtils class Deployer { int tries = 0 Script script def run() { while (tries < 10) { Thread.sleep(1000) tries++ script.echo("tries is numeric: " + StringUtils.isAlphanumeric("" + tries)) } } } 


In classes, you can use any language features: create streams, connect via FTP, etc.



Important:

- to output console logs from Pipeline, you need to transfer Script;

- to import libraries simply use @Grab .



Below is an example script:



 #!/usr/bin/env groovy def call(body) { echo "Start Deploy" new Deployer(script:this).run() echo "Deployed" currentBuild.result = 'SUCCESS' //FAILURE to fail return this } 


You can use any language features in scripts and also get Jenkins assembly variables and parameters.



Important:



- To stop execution, it is enough to set the value of currentBuild.result = 'FAILURE' ;

- You can get the parameters of a parameterized assembly through the env variable. For example, env.param1 .



Here is an example repository with other examples.



Repository connection



The next step is to add our repository as a global Pipeline library.

To do this, go to Jenkins: Manage Jenkins → Configure System ( Configure Jenkins → Configure the system ). In the Global Pipeline Libraries block , add our repository as in the picture below:



image



Create Pipeline



The final step is to create a Pipeline.



Our Pipeline will look like this:



 @Library('jenkins-pipeline-shared-lib-sample')_ stage('Print Build Info') { printBuildinfo { name = "Sample Name" } } stage('Disable balancer') { disableBalancerUtils() } stage('Deploy') { deploy() } stage('Enable balancer') { enableBalancerUtils() } stage('Check Status') { checkStatus() } 


Those. we simply add @Library('jenkins-pipeline-shared-lib-sample')_ (do not forget to add _ at the end) and call our functions by the name of scripts, for example, deploy .



Our Pipeline is ready.



Next time, I'll show you how I set up a parameterized build to elegantly get dependent parameters from a REST service.



Thanks for attention!

Source: https://habr.com/ru/post/338032/



All Articles