Author: Ashutosh Tripathi
Prerequisites:
- Trial GitHub account
- Trial AnyPoint Platform account
- AnyPoint Studio (version 7.14.0 or later)
Why should you set up the CI/CD pipeline?
A CI/CD pipeline is a method that helps developers build, test, and deploy their code easily and efficiently in the software development life cycle phase. It enables developers to minimize human errors and deliver reliable code for frequent code delivery.
Following continuous integration and continuous deployment is considered a best practice from the DevOps perspective.
Cloudhub 2.0
Cloudhub 2.0 is easier to work with, develop, and manage compared to Cloudhub 1.0. Below are some major advantages of using it:
- Ideal Mule clustering for deployments with more than one replica.
- Container-based application deployment allows for better utilization of resource consumption.
- High application availability.
- Enabling high scalability.
- Multiple options are available for V-Cores selection while application deployment.
GitHub Actions
GitHub Actions is a continuous integration and continuous deployment platform that allows developers to automate the build, test, and deployment of code. Developers can write workflows that build and test every pull request to the repository or deploy merged pull requests to the runtime manager in the AnyPoint platform.
Set up a connected app in the AnyPoint platform and obtain the organization ID
- Step 1: Go to Access Management -> Click on Connected Apps -> Create App

- Step 2: Please provide the required scopes listed below. Once created, please copy the client ID and secret for the connected app.

- Step 3: Navigate to access management and click on the organization name to copy the organization ID.

Steps to set up the GitHub Repository
- Step 1: Create a private repository with an appropriate name.

- Step 2: Setup branching names.

- Step 3: Set up repository secrets. These secrets will be utilized in workflow files to pass values directly at runtime during deployment. To create the secret, please follow the instructions below.
Go to Repository Settings -> Secrets and variables -> Actions -> Click on New repository secret

Setup Mule application for CI/CD process
Step 1: Create a sample application to log payload.

- Step 2: Changes in POM.xml
Add the Mule-Maven-plugin for CloudHub 2.0 deployment. All required values will be taken at runtime from workflow files.
<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>${env.environment}</environment>
<target>${env.target}</target>
<muleVersion>4.4.0</muleVersion>
<businessGroupId>${env.ORG_ID}</businessGroupId>
<connectedAppClientId>${env.CONTD_APP_CLIENT_ID}</connectedAppClientId>
<connectedAppClientSecret>${env.CONTD_APP_CLIENT_SECRET}</connectedAppClientSecret>
<connectedAppGrantType>client_credentials</connectedAppGrantType>
<applicationName>cloudhub2-deployment-github-actions-poc</applicationName>
<replicas>${env.replicas}</replicas>
<vCores>${env.cores}</vCores>
<deploymentSettings>
<generateDefaultPublicUrl>true</generateDefaultPublicUrl>
<http>
<inbound>
<lastMileSecurity>false</lastMileSecurity>
</inbound>
</http>
</deploymentSettings>
<properties />
<secureProperties />
</cloudhub2Deployment>
</configuration>
</plugin>
Add Distribution Management.
<distributionManagement>
<repository>
<id>anypoint-exchange-v3</id>
<name>Corporate Repository</name>
<url>https://maven.anypoint.mulesoft.com/api/v3/organizations/${env.ORG_ID}/maven</url>
<layout>default</layout>
</repository>
</distributionManagement>
- Step 3: Create a .maven folder at the same level as the src folder and add a setting.xml file inside the .maven folder.
<settings>
<servers>
<server>
<id>anypoint-exchange-v3</id>
<username>~~~Client~~~</username>
<password>${env.CONTD_APP_CLIENT_ID}~?~${env.CONTD_APP_CLIENT_SECRET}</password>
</server>
</servers>
<pluginGroups>
<pluginGroup>org.mule.tools</pluginGroup>
</pluginGroups>
<profiles>
<profile>
<id>Mule</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>MuleRepository</id>
<name>MuleRepository</name>
<url>https://repository.mulesoft.org/nexus-ee/content/repositories/releases-ee/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
</settings>
- Step 4: Add the workflow files in CloudHub 2.0. It is required to first publish the asset to the exchange. Therefore, in the application, two workflow files are maintained.
Once the code is merged into the feature branch, the pipeline will be triggered to test, build, and publish the asset to the exchange.
Content for build_to_feature.yml file:
name: Build the project
# Controls when the workflow will run
on:
push:
branches:
- 'feature/**'
jobs:
buildAndPublishToExchange:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Build with Maven
env:
CONTD_APP_CLIENT_ID: ${{ secrets.CONTD_APP_CLIENT_ID }}
CONTD_APP_CLIENT_SECRET: ${{ secrets.CONTD_APP_CLIENT_SECRET }}
ORG_ID: ${{ secrets.ORG_ID }}
environment: "Sandbox"
target: "Cloudhub-US-East-2"
replicas: "1"
cores: "0.1"
run: mvn clean -B deploy -s .maven/settings.xml
When the PR will be merged into the dev branch then another pipeline will be triggered to deploy the code to the Sandbox environment.
Content for deploy_to_dev.yml workflow file:
name: Cloudhub build and deploy to Dev
# Controls when the workflow will run
on:
pull_request:
branches: [dev]
types: [closed]
jobs:
buildAndDeploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Build and Deploy with Maven
env:
CONTD_APP_CLIENT_ID: ${{ secrets.CONTD_APP_CLIENT_ID }}
CONTD_APP_CLIENT_SECRET: ${{ secrets.CONTD_APP_CLIENT_SECRET }}
ORG_ID: ${{ secrets.ORG_ID }}
environment: "Sandbox"
target: "Cloudhub-US-East-2"
replicas: "1"
cores: "0.1"
run: mvn clean -B deploy -DmuleDeploy -s .maven/settings.xml
Project File Structure
- Inside .github, create one folder with the name workflows and keep all the workflows files there and the settings.xml file will be inside the .maven folder.

Points to check before pushing the code
- Keep Mule Maven plugin version to 3.8.1.
- Increase the POM version every time before pushing the code to GitHub.
Validate the working of the pipeline
- Step 1: Push the code to the feature branch, and then navigate to the Actions tab to check the pipeline logs.

- Step 2: Once the test, build, and deployment to the exchange are successful, proceed to Anypoint Exchange to check the entry corresponding to the asset.

Step 3: Now, raise the pull request (PR) to merge the code into the dev branch. This will trigger a new workflow to deploy the code to the Sandbox environment. Once the deployment is successful, the application will be visible in the Runtime Manager.

- Step 4: Test it from Postman.
