Bitbucket CI/CD pipeline for MuleSoft CloudHub deployment

  • April 08, 2021

In this article, we'll set up a continuous integration and continuous delivery/continuous deployment (CI/CD) pipeline using the Bitbucket cloud.

Prerequisites

1. Bitbucket cloud account. A new developer account can be created from the link https://bitbucket.org/

Bitbucket cloud account

2. A Mulesoft Anypoint Platform account with administrator access privileges

3. Three environments created in Anypoint Platform: development, UAT and production

Goal

1. To deploy a Mulesoft application to Anypoint CloudHubusing Bitbucket pipelines

2. To promote the code through three different Mulesoft environments – development, UAT and production.

Branching strategy

Branching Strategy

1. Developer commits code to a new feature branch based on the develop branch.

2. Once development is completed, a Pull Request (PR) is raised to merge feature branch with the develop branch.

3. PR is reviewed by one or more reviewers for peer review.

4. On approval of the PR, feature branch is merged with develop branch.

5. Once code is merged to develop branch, automatic deployment takes place on CloudHub development environment.

6. For promoting the code to UAT environment for testing, a new release branch is created based on the develop branch.

7. Release branch creation automatically deploys the code to CloudHubUAT environment.

8. On receiving UAT Sign Off, code is promoted from UAT to Production environment.

9. Release branch is merged with master branch and the release is tagged in Bitbucket.

10. Release and the feature branches are deleted.

Steps for DEV deployment

1. Login to your cloud Bitbucket account.

2. Go to your workspace and click settings.

Steps for DEV deployment

3. Click Workspace variable and set the variables. These variables will be referred to in the pipeline configuration file. Also, since these variables are set at the workspace level, they can be referred by all repositories and their pipelines.

Steps for DEV deployment

Steps for DEV deployment

Steps for DEV deployment

For the environment specific client_id and client_secret values, log in to your Anypoint platform. Navigate to Access Management > Environment. Select the environment to open the environment details. The screen will display the client_id and Client_secret.

The other two properties – Anypoint_platform_username and Anypoint_platform_password – are the Anypoint platform login credentials that will be used for deployment from Bitbucket.

Access Management

4. Create a new Bitbucket private project in your workspace. A Bitbucket project acts as a collection of repositories for a particular business function. We’ll create a project named digital that will house all the repositories for digital business unit in an organization.

Bitbucket private project

Bitbucket private project

5. Click the ‘+’ sign to add a new repository to your new Bitbucket project.

Bitbucket private project

Create a new repository

6. Enable Bitbucket pipeline for your repository by following the navigation – <REPOSITORY> -> Repository settings -> Pipelines -> Settings

Enable bitbucket pipeline

7. Deployment Settings – Navigate to <REPOSITORY> -> Repository settings->Deployments and add three Bitbucket environments. These environments will be referred in the pipeline configuration file as well as in the Bitbucket dashboard.

Deployment Settings

8. In the repository, ensure that there are two branches – develop and master. If not, then create both these branches by clicking the ‘Create Branch’ button.

Create Branch

9. Create a new feature branch named ‘feature/CICD-Setup’ from the develop branch.

Create a new feature branch

10. Push all the files from your MuleSoft application to this feature branch. Ensure the bitbucket-pipelines.yml file is present in the root directory. This is the file that has the configuration of the pipeline.

Contents of bitbucket-pipelines.yml

Configuration of the pipeline

Configuration of the pipeline

Also, ensure that the pom.xml file of your mule application has the below configuration for mule-maven-plugin and properties versions

Configuration of the pipeline

Configuration of the pipeline

11. A PUSH  to the feature branch automatically triggers a Bitbucket pipeline. Verify this by navigating to <REPOSITORY> > pipelines. Make sure the status of the running pipeline gets to completed.

Bitbucket pipeline

12. Raise a Pull Request to merge the feature branch with develop branch.

Raise a Pull Request

Raise a Pull Request

You can select any of your team members as the reviewer. Click ‘Create Pull Request.’

Once the PR is approved by the reviewer, you can merge it by navigating to <YOUR_REPOSITORY> > Selected PR. Click the ‘Merge’ button.

Merging to develop

13. The above step (merging to develop) automatically triggers a build and deployment to the development environment. Verify this by navigating to <YOUR_REPOSITORY> -> Pipelines.

Deployment to the development environment

Click the pipeline

This pipeline involves two steps:

  • Mulesoft build and test: For building, testing and packaging the JAR file
  • Mulesoft deployment to development: For deployment of JAR from step 1 to CloudHub development environment.

Confirm that the deployment is successful by verifying if the application has been deployed in Anypoint Runtime Manager in CloudHub.

Anypoint Runtime Manager

Steps for user acceptance testing (UAT) and production deployment

1. Per our branching strategy, we need to create a release branch to move code to UAT. Create this release branch in the Bitbucket repository based on the develop branch.

Create release branch in bitbucket repository

2. Immediately after the branch is created, a pipeline will be triggered for the release branch. Verify this by navigating to <REPOSITORY> > Pipelines

Pipeline will be triggered for the release branch

It has three steps:

  • Mulesoft build and test – AUTOMATED: Creates the JAR file
  • Mulesoft deployment to UAT – AUTOMATED: Deploys the JAR file from step 1 to CloudHub UAT environment
  • Mulesoft Deployment to PROD – MANUAL: Deploys the JAR file from step 2 to CloudHub production environment

The pipeline will automatically execute step 1 and 2 in sequence and then pause.

Execute step 1 and 2 in sequence

3. Verify in CloudHub UAT environment if the deployment is successful.

Cloudhub UAT environment

4. After testing is completed in the UAT environment, code can be moved to production. To do this, click the deploy button displayed for step 3 and wait for the status to turn green.

Status to turn green

5. Once the status for step 3 turns green, verify the deployment to production is successful.

Verify if the deployment to production is successful

Post-production housekeeping tasks

1. Merge the release branch with master and, if needed, with develop.

2. Create a tag for the release. Navigation –  <REPOSITORY> -> Commits. Select the latest commit and click the + button next to ‘No tags’. Enter a tag name that will represent the release deployed to production.

Create a tag for the release

3. Delete the feature branches and release branch.

— By Vaibhav Patkar