MuleSoft Automate Deployment
Deploy Mule Applications to CloudHub using the Mule Maven Plugin and Bitbucket Pipelines
Hello and welcome. In this post I will explain how to automate MuleSoft Application deployment to CloudHub using Mule Maven Plugin and Bitbucket Pipeline.
Prerequisite
- MuleSoft CloudHub Account
- Bitbucket Atlasssian Account — for code repository.
- If you are using the HTTP Listener as source for your flow, you need to set its host to 0.0.0.0 and its port to ${http.port}
- Declare all external classes and resources in the exportedPackages and exportedResources fields on the
mule-artifact.json
file.
Project Structure
Pipeline file should have the name — bitbucket-pipelines.yml
. It should be at repository root-level as described below in a sample project structure. Once it is defined at the root-level, Bitbucket will automatically pick-up any changes to the code commit and will start to run the pipeline.
Pipeline Stages
- Git Security Scan
- Validate & Compile
- Test
- Deploy to Sandbox CloudHub
Alright, a big question — how to integrate this pipeline to a MuleSoft Project? Let’s check this out in the next section.
How to integrate pipeline to your own MuleSoft Project
- Update pom.xml: open pom.xml — plugin element: Inside the plugin element in your project’s pom.xml file, search for plugin section with mule-maven-plugin
artifactId
and replace the belowplugin
placeholder.
2. Create Variable References in Bitbucket: In step 1, as you can see, we are using pipeline variables e.g. — {MULESOFT_ENVIRONMENT}
. These pipeline variables are convenient as you can set these once and reuse in your pipeline at build time.
- So let’s create
Workspace Variables
as described below;
The reason we’re using
Workspace Variables
is for the reusability. Once defined, can be reused within the workspace, i.e. for all repository; so you don’t have to create these variables for each and every repository within the workspace.We’re using
MULESOFT_USERNAME
andMULESOFT_PASSWORD
variables assecured
. Bitbucket supports variables stored assecured
so it’s value is hidden and not visible in the variables list and in the build logs. Secure variables are stored as encrypted values.
- Next, Let’s create
Deployment Variables
; these variables can only be used in a specific deployment environment. We will setMULESOFT_ENVIRONMENT
variables with the value of a deployment environment in your MuleSoft CloudHub, e.g.Sandbox
Ensure that the pipeline is enabled before creating
Deployment Variables
- Change the
Test
environment name toSandbox
; [we’re going to use CloudHub’sSandbox
environment, you can use the one that’s available] - Under Variables section for the
Sandbox
environment, createMULESOFT_ENVIRONMENT
variable with valueSandbox
For more details about Bitbucket variables, you can visit — https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/
3. Create bitbucket-pipelines.yml
as described below
4. that’s it basically, all done.
5. Push the code to your remote master branch — and Bitbucket will trigger the deployment.
Demo Project
Remember — the objective is to deploy the application to CloudHub. I have used a sample MuleSoft Application — with a Listener that returns a “Hello World!” JSON response.
You can use this repository included with pipeline configurations here — https://bitbucket.org/kuldeepbhati/cloud-hub-deploy/src/master/
Common Issues
- 4.3.0–20210119 is not a valid value for muleVersion.version.
Caused by: org.mule.tools.client.core.exception.ClientException: 400 Bad Request: {“status”:400,”message”:”4.3.0–20210119 is not a valid value for muleVersion.version.”}
If you get this error; this is due to the incorrect
<app.runtime>4.3.0–20210119</app.runtime>
in yourpom.xml
file. Correct it as<app.runtime>4.3.0</app.runtime>
Important Notes
- We’re using a single environment in this pipeline — that’s
Sandbox
. You can always extend this pipeline and use as many upstream environments as it pleases you. - Ensure that you have correct environment naming across —
bitbucket-pipelines.yml
,Deployment Variables
and your CloudHub environment name. - You may have noticed, that we’re using
MULESOFT_USERNAME
andMULESOFT_PASSWORD
variables for deployment; although these are stored assecured
and not visible in the variables list and also not in build logs, it is advised to use Connected App functionality, please visit this link for more information — https://help.mulesoft.com/s/article/How-to-deploy-an-application-to-Cloudhub-using-Connected-App-functionality - We have not introduced any branching model in this post; so If you push code from any branch; the deployment will be triggered. You can use this link to configure branch specific deployment — https://support.atlassian.com/bitbucket-cloud/docs/pipeline-triggers/
- As of now; we kept this pipeline very simple; if there’s need for using Mule EE repositories you would need to create a Maven
settings.xml
file that will have Mule EEusername
andpassword
. You can use this post — https://medium.com/@chris.shellenbarger/continuous-integration-a-complete-guide-to-using-bitbucket-pipelines-with-maven-repositories-e835bd0f5f11 that describes how to achieve that. Please leave a comment below if you need any help.