Author: Ashutosh Tripathi
Prerequisites:
- AnyPoint Studio (version 7.15.0 or later)
Why to setup a CICD pipeline for Mule apps?
With the use of the CI/CD pipeline in MuleSoft, high-quality code can be delivered at a much faster pace. It provides end-to-end visibility and control of the pipeline processes. Below are some major advantages of having the CI/CD pipeline in Mule applications.
- Automating the process of building and deploying the applications.
- Automating the run of MUnit test suites.
- Automating the workflow pipeline.
What’s new in Cloudhub 2.0?
- Clustering support.
- Introduction of shared and private space.
- Every private space will have its own load balancer which will be assigned automatically.
- Load balancing logs.
- Setting up Outbound firewall rules.
- Deployment rollback
Understanding the Bitbucket pipeline
Bitbucket is a Git-based source code repository hosting service owned by Atlassian. It’s another CI/CD tool, similar to GitHub Actions or Jenkins. With the help of Bitbucket’s CI/CD pipeline, we can build, test, and deploy code based on the configuration files defined in the repository at the root level.
Setup connected app in Anypoint platform
It is a required step before moving on with this blog. Here, connected app credentials will be used to authenticate and deploy Mule applications to CloudHub 2.0 in Runtime Manager.
Step 1: Create a trial Anypoint Platform account.

Step 2: Once logged in, Go to Access Management -> Click on Connected Apps -> Create App.

Step 3: Provide all the required scopes listed below, which are needed for deploying apps to CloudHub 2.0. Once you’ve done that, copy the client ID and secret.

Step 4: Get the organization Id.
Navigate to Access management -> Business Group -> Click on name and copy the business group ID.

Setup the Bitbucket repository
Step 1: Create a free trial Bitbucket account using your email and provide your username on the sign-up page.


Step 2: Once logged in, create a workspace by providing a new workspace name.


Step 3: Create a workspace variable that will be accessible to all the repositories inside this workspace. These variables will be directly passed at runtime when deploying the applications.
To create variables, please follow the instructions below.
Settings -> Workspace Settings -> Workspace variables.

Step 4: Create a project.
Click on Project on main page -> click on create project.

Step 5: Once the project is created, click on the Create Repository button.


Step 6: Once a repository is created, create two branches with the names ‘develop’ and ‘feature/pipeline-demo.’ The ‘develop’ branch will originate from the ‘master’ branch, and the ‘feature’ branch will originate from the ‘develop’ branch. The ‘Feature/Pipeline-Demo’ branch will be responsible for running MUnit test suites, building the JAR and publishing the asset to exchange. The ‘develop’ branch will run the test suites, build the JAR, and deploy it to the Sandbox environment in Runtime Manager.
Click on branches -> Create Branch

Step 7: Enable Multi-Factor Authentication (MFA) for your trial account. This is a required step to enable the pipeline for the repository. Be sure to validate the process through the email received once it is done from the Bitbucket UI.
Setting -> Personal Bitbucket settings -> Click on two step verification.


Step 8: Follow the below path to enable pipeline for this repository. It is a required step to enable a pipeline for repository if CICD setup needs to be done.
Click on repository -> repository settings -> settings -> Enable pipeline.

Step 9: Setup deployment variables at repository level. It will be same as of Runtime Manager environment names in real project scenarios.
Repository Setting -> Click on deployments -> Set environment name.

Setup a Sample Mule application
Step 1: Create a hello-world Mule application to log the payload.

Step 2: Add mule-maven-plugin configuration for cloudhub 2 deployment in pom file. All externalized values will directly come at Runtime from the pipeline file using workspace variables.
<plugin>
<groupId>org.mule.tools.maven</groupId>
<artifactId>mule-maven-plugin</artifactId>
<version>${mule.maven.plugin.version}</version>
<extensions>true</extensions>
<configuration>
<cloudhub2Deployment>
<uri>https://anypoint.mulesoft.com</uri>
<provider>MC</provider>
<environment>Sandbox</environment>
<target>Cloudhub-US-East-2</target>
<muleVersion>4.4.0</muleVersion>
<businessGroupId>${BG_ID}</businessGroupId>
<connectedAppClientId>${CONTD_APP_CLIENT_ID}</connectedAppClientId>
<connectedAppClientSecret>${CONTD_APP_CLIENT_SECRET}</connectedAppClientSecret>
<connectedAppGrantType>client_credentials</connectedAppGrantType>
<applicationName>hello-world</applicationName>
<replicas>1</replicas>
<vCores>0.1</vCores>
<deploymentSettings>
<http>
<inbound>
<lastMileSecurity>false</lastMileSecurity>
</inbound>
</http>
</deploymentSettings>
<properties />
</cloudhub2Deployment>
</configuration>
</plugin>
Step 3: Add distribution management in pom.xml file.
<distributionManagement>
<repository>
<id>anypoint-exchange-v3</id>
<name>Corporate Repository</name>
<url>https://maven.anypoint.mulesoft.com/api/v3/organizations/${BG_ID}/maven</url>
<layout>default</layout>
</repository>
</distributionManagement>
Step 4: Create a .maven folder at a level same as of src folder and add a setting.xml file inside it with below content.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<settings>
<servers>
<server>
<id>anypoint-exchange-v3</id>
<username>~~~Client~~~</username>
<password>${CONTD_APP_CLIENT_ID}~?~${CONTD_APP_CLIENT_SECRET}</password>
</server>
</servers>
</settings>
Step 5: Add the Bitbucket-pipelines.yml file in the project at root level same as the pom file.
image: maven:3.8.4-openjdk-8
pipelines:
branches:
feature/*:
- step:
name: Mulesoft Build and Deploy
caches:
- maven
script:
- echo "Building the Mulesoft App"
- mvn clean -B deploy -DBG_ID=$BG_ID -s .maven/settings.xml
develop:
- step:
name: "Deploy to Sandbox Clodhub"
caches:
- maven
deployment: Sandbox
script:
- echo "deploying the app to Cloudhub"
- mvn clean -B deploy -DmuleDeploy -DCONTD_APP_CLIENT_ID=$CONTD_APP_CLIENT_ID -DCONTD_APP_CLIENT_SECRET=$CONTD_APP_CLIENT_SECRET -DBG_ID=$BG_ID -s .maven/settings.xml
Step 6: Clone the feature/pipeline-demo branch from the Bitbucket repository in local and place all the required project files in it.

Final Project Structure

Points to validate prior pushing code to remote repository
- Mule Maven plugin version should be 3.8.2 or latest version.
- Keep increasing the pom version every time, prior pushing the code to remote Bitbucket repository.
Validate the working of the Bitbucket CICD pipeline
Step 1: Push all the initial code to the remote branch from your local environment. This action will trigger the pipeline, which will build the project and publish the asset to the Exchange.


Step 2: In Bitbucket UI, click on pipeline, after pushing the pipeline should get triggered, to publish the asset to exchange.

Step 3: Once it is successful, check the asset on exchange, it will be visible there.


Step 4: Create a pull request from the ‘feature/pipeline-demo’ branch to the ‘develop’ branch and merge it. This will deploy the code to the Sandbox environment in the Runtime Manager.

Step 5: Once the pipeline is completed successfully, go to the runtime manager, and check the latest deployed application in the Sandbox environment.


Step 6: Test it from the postman, to validate the working of the Mule application.

.