Automating WSO2 Mediators Deployment with Jenkins, Nexus and Ansible

This article explains about an Automation process on how we can deploy custom mediators on to wso2 servers through a proper versioning and smooth deployment process which will reduce the cost of manual deployment.

Usually wso2 mediators build through maven, so here also I’m going to build the artifacts through maven and then push to the Nexus repository along with versioning mechanism and then we will use the Ansible to pull the artifact from the repository and install into wso2 servers.

To get explained this easily, below contents include in this article.

  1. Pipeline Implementation
  2. Ansible Script Implementation for Deployment
  3. Connecting the Pipeline -> Ansible
  4. Improvements
  5. References

1. Pipeline Implementation

What our pipeline should do is, for development environments it should build and deploy the SNAPSHOT artifacts to Nexus Repository.

for other environments it should build the release version of the artifacts and publish to the Nexus Repository.

Below Diagrams explains how we are going to implement this in a flow diagram.

Other Environment — Release Version Build, Publish and Deployment Process

1.1 Pre-Requisites

  • Jenkins Installed with plugins

Nexus Artifact Uploader: This plugin to upload the artifact to Nexus Repository.

Maven Release Plug-in Plug-in: A plug-in that enables you to perform releases using the maven-release-plugin from Jenkins.

Pipeline Utility Steps: Utility steps for pipeline jobs.

Maven Integration plugin: This plug-in provides, for better and for worse, a deep integration of Jenkins and Maven.

SSH Pipeline Steps: SSH Pipeline Steps.

  • Ansible
  • Nexus Repository
  • Source Code Repository ( Here I’m using GitHub as my code repository )

1.2 Setting up Pipeline

Hope already installed the Jenkins and Nexus, in my case below screen shots depicts it has been started under the specific ports.

1.2.1 SNAPSHOTS Build

As initial step we will prepare the SNAPSHOTS build pipeline, Go to Jenkins and follow the below steps to setup it.

We have created a folder, and now we can create the pipeline. Note that here we created a folder to include all the other mediators and group into one folder. If there is only one mediator then can go directly with the pipeline.

Copy the above and based on your configuration can modify and save it under pipeline script section pointed with arrow in the above Image.

Once you execute the build now option, the job will trigger the build and the built artifact will be published to Nexus Repository under maven-snapshots folder as below:

1.2.2 Release Build

The difference between the release build is we are using the maven release plugin and as mentioned in the Flow Diagram along with publishing the Artifact there are two other stuffs done are

  1. Creating a Tag using the current POM Version
  2. Committing the pom.xml with the next development version

So to create a release build pipeline, same as the previous steps create a folder mediator-releases and the create the pipeline with the name password-validator, then inside the pipeline script section copy the below groovy script. I have mentioned the parameter information as comments to understand easily.

Additional Step is to configure versioning for the release, this can be done using the below section.

Then you can copy the below content to the Pipeline script section.

Before executing the build need to update the below in the pom.xml of the specific code.

Need to add the below two tags:

And also create a settings.xml file and add it inside the project folder. The content of the settings.xml will be as below.

Now we are ready to execute the build and once the build ran successfully, then you can observe that the artifact published to maven-releases folder.

Note: If a particular git repository have multiple projects inside folders, you can provide the main git repository url and then can build specific projects inside the repository using the command mvn with providing -f option to set the specific pom.xml.

eg: mvn -f <folder-name>/pom.xml

2. Ansible Script Implementation for Deployment

When deploying mediators into wso2 servers there are two specific things to consider they are whether our build is jar or build. Based on it the mediator should copied to lib or dropins folder. Below is a common Ansible script I have created for this purpose may be useful for this kind of purpose.

Note: Before executing the ansible, the maven_artifact have a dependency with python-lxml. You need to installed it to the server where the particular deployment is happening.

The below task can be included as a task in a role and then it can be called during the ansible script execution.

And the usage of this Role can be invoked as below, this can be invoked inside a role or directly inside main task as below, here the <mediator_name> will be the mediator we are going to deploy and the values can be set as variables inside the inventories.

3. Connecting the Pipeline -> Ansible

Using the ssh pipeline steps plugin we can execute shell commands to the remote server, so we can use the same to invoke the ansible scripts from the Jenkins.

4. Improvements

This process can be improved if we can automate the merge of master to development branch repositories as there can be a difference after the release between master and development branches as the maven release plugin will update the master code only with the next development version, so there will be a manual update needed.

The maven_artifact in ansible having an issue when fetching the latest version of snapshot, so here when fetching the snapshots is happening based on provided snapshot version. This can be improved by finding a way to pull the latest. Note the Release fetch is happening properly and it can be fetched by providing version as “latest”.

5. References

[1] https://maven.apache.org/maven-release/maven-release-plugin/

[2] https://www.jenkins.io/doc/book/pipeline/jenkinsfile/

[3] https://www.jenkins.io/doc/book/using/using-credentials/

[4] https://www.mojohaus.org/build-helper-maven-plugin/parse-version-mojo.html

Senior Tech Lead — Enterprise Integration | WSO2 Certified Solution Architect | https://ajanthane.blogspot.com/