How to deploy a Mule application with a Maven and Jenkins pipeline

how to guide

Mule applications and deployments can be fully managed using Maven. In the development phase, Anypoint Studio makes it easy to manage your application dependencies using Maven. For Deployment tasks, Mule provides a Maven plugin that helps automate the application deployment to different target runtime environments such as Standalone, CloudHub, ARM, Cluster, and more.

There are different ways to install and run Mule Runtime:

  1. Local Standalone: This involves installing Mule Runtime on your local server; this means Mule runs as a single server instance.
  2. Local Cluster: This is similar to an individual instance set up on local, except the instances are part of a cluster that interact with each other.
  3. CloudHub: This is an Integration Platform as a Service (iPaaS) provided by MuleSoft, enabling your Mule apps to run in the cloud.
  4. Pivotal Cloud Foundry: This involves deploying Mule Runtime on Pivotal Cloud Foundry.

Deployments to all these environments can be managed through Manual Copy (for local), Anypoint Runtime Manager, or the Mule Maven Plugin.

In this post, we will see how we can leverage the Mule Maven plugin to perform deployments. We will do a deployment to a standalone Mule instance as well as to a CloudHub Instance. We will also automate our deployment using Jenkins Pipeline.

Version used in this how-to guide:

  • Mule Runtime 3.9.0
  • Jenkins 2.11.0
  • Mule Maven Plugin 2.2.1

1. Mule Maven Plugin

1.1 Introduction

The mule-maven-plugin is a Maven Plugin provided as a part of the Mule Framework. It allows us to run integration tests for Mule applications, and also to deploy any application to various environments.

1.2 Configuration

This plugin is available in the Mulesoft public repository, so you will need to add the below repository to your settings.xml

Once you have added the repository, you can include the plugin in your pom.xml as:

At this point, you are ready to add the configuration for your target server/environment.

1.3 Deployment Profiles

You can have different environments for deploying our application. In fact, you can have a mix of the local standalone and CloudHub approach described above.

So, instead of configuring our pom.xml for a single environment, we will use a Maven parent POM approach. We will define a Maven Parent POM and add a Maven Profile for each of our target environments.

The full parent POM is available on the Github here. Configuration parameters are available as properties, and with default values where applicable.

The profile below will help us deploy to a local Mule server:

  1. Target Mule version if you need to install Mule needs as a part of the deployment.
  2. This should be a path to our locally installed Mule server, such as /opt/mule. The Mule version and Mule home are exclusive to each other.
  3. The application name used when deploying to Mule instance.
  4. If you are using Community Edition, then this should be set to true.

The profile below will help you implement the CloudHub deployment:

  1. Target the Mule Runtime version where the application should be deployed.
  2. The Anypoint Platform username with appropriate access to target environment.
  3. The Anypoint Platform password.
  4. Target Environment should be configured as on ARM.
  5. If you are running ARM on a private instance then specify the URL here. Example: anypoint.example.com.
  6. Only platform users are allowed in this configuration. It’s not possible to use SSO/Federated as the username.

List of available parameters varies based on the deploymentType. See the documentation for a full list of parameters for each deployment type. You may have to modify your profile to add new parameters as needed.

1.4 Demo Application

The full demo application is available on the GitHub.

Once we have our Parent POM ready, we use that in our Mule Application POM as:

1.2 Manual Deployment

At this point, we can deploy our application using the Mule Maven Plugin and running the Maven commands in terminal.

Deploying to a local standalone Mule:

mvn clean deploy -P standalone -Dmule.home=/opt/mule

Deploying to CloudHub:

mvn clean deploy -P cloudhub -Dmule.version=3.9.0 -Danypoint.username={your username} -Danypoint.password={your password}

You can pass additional parameter values as needed.

2. Jenkins Pipeline

2.1 Introduction

As per the Jenkins website:

Jenkins Pipeline (or simply “Pipeline”) is a suite of plugins which supports implementing and integrating continuous delivery pipelines into Jenkins.

In this post, I will not go into detail about Jenkins Pipeline. If you’d like to learn more, then please refer to this Jenkins Document to get a good overview.

2.2 Jenkins Setup

CloudHub and ARM deployment need to access Anypoint Platform. To provide access, let’s add Anypoint credentials in the Jenkins credential page. We will use these credentials in our Pipeline to avoid hardcoding and password exposure. If you don’t know how to create credentials, I suggest you follow this nice tutorial.

Configure Jenkins Credentials

If you do not specify anything in the ID attribute, Jenkins will assign an auto-generated ID. We will need this ID value for credential lookup in Pipeline.

2.3 Simple Pipeline

Now, we have all that we need to add a Jenkins Pipeline to our Mule Application. For this, we will need to create a file named Jenkinsfile in the root of our application.

Here is the content of our Jenkinsfile:

  • Stage to run Unit tests on our Mule Application.
  • Deploy your Mule application to a standalone server. Since you are not passing in the mule.home parameter value, this step will actually download the Mule server from Mule Maven Repository and install it in the Jenkins workspace. If you want to deploy it to an existing, installed instance, then you may add a parameter -Dmule.home=/opt/mule in the Maven command.
  • Deploy to a Mule Server Managed via Anypoint Runtime Manager.
  • Credential lookup: You need Anypoint Platforms credentials for this step. We will use jenkins credentials() function to look up our credentials using ID value. This function automatically sets two environment variables named {ourvariable}_USR and {ourvariable}_PSW.
  • Use Credential variables to set Anypoint username and password, while calling our arm profile.
  • Deploy to CloudHub using your CloudHub profile. The process to setup credentials is same as deploying to ARM.
  • For CloudHub deployments, mule.version must be same as it appears on CloudHub available Runtime version names.

To understand the syntax of the Jenkins file, you may refer this Jenkins Syntax Reference document.

2.4 Jenkins Pipeline Job

Now, we can create a Jenkins Pipeline Job for our demo and configure it to use Jenkinsfile from SCM.

Jenkins Pipeline Job

2.4.1 Job Execution

When this job runs, your pipeline should run and perform all deployments:

Mule Maven Pipeline Job Execution

2.5 Deployment Result

2.5.1 Standalone deployment

  1. We did not specify mule.home parameter, so this step actually downloaded Mule Runtime and installed it.
  2. Then it deployed the generated application zip.

2.5.2 ARM deployment

As the application was already deployed to ARM, it just redeployed our application.
ARM deployed application

2.5.3 CloudHub deployment

Redeploying our application to CloudHub.

CloudHub deployed application

3. Conclusion

As this how-to blog has demonstrated, Mule Runtime can be installed on many platforms and the Mule Maven plugin simplifies the deployment process. We can also do integration tests using this plugin. We used the Mule Maven plugin and Jenkins Pipeline to automate our Mule deployment to Anypoint Runtime Manager as well as CloudHub environment.

For more information, please refer to the source code on GitHub: mule-maven-deployment-demo and mule-maven-parent-pom.

This is a guest post is by one of our MuleSoft Ambassadors, Manik Magar.


 


We'd love to hear your opinion on this post


3 Responses to “How to deploy a Mule application with a Maven and Jenkins pipeline”

  1. Hi Manik,

    I’m trying to do the same job with Bamboo. But while building, I’m getting an error:

    uild 05-Jun-2018 19:05:20 [ERROR] The goal you specified requires a project to execute but there is no POM in this directory (C:\Users\skurra\bamboo-home\xml-data\build-dir\PLAYW-UNB-JOB1). Please verify you invoked Maven from the correct directory. -> [Help 1]

    Can you please help?

  2. What would you suggest if you want to separate build and deploy?
    Meaning not having to build each time you want to deploy. It would allow to reduce deployment time and also to store your artifacts somewhere in case you need them?

  3. Hi Manik,

    Do you have any sample, insight on how we can use the same strategy using encrypted credentials (secure vault) ?

    is any option in Jenkins we can store the master kwy an retrieve it from the jenkins file?

    Thanks!