Author: Vaibhav Patkar
In this article we will set up a CI CD pipeline using the Bitbucket cloud.
1. Bitbucket cloud account. A new developer account can be created from the link – https://bitbucket.org/
2. A Mulesoft Anypoint Platform Account with administrator access privileges
3. Three environments created in Anypoint Platform – development, UAT and Production
1. To deploy a Mulesoft application to Anypoint Cloudhub using Bitbucket pipelines
2. To promote the code through three different Mulesoft environments – development, UAT and Production
Our branching strategy will be based on the diagram below.
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 Cloudhub UAT 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.
3. Click Workspace variable and set the variables as per screen shot. These variables will be referred 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
For the environment specific client_id and client_secret values, login 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. For e.g. for the development environment, the below screenshot displays 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
4. Create a new Bitbucket private project in your workspace. A Bitbucket project acts as a collection of repositories for a particular business function. For e.g we will create a project named digital that will house all the repositories for digital business unit in an organization.
5. Click the ‘+’ sign to add a new repository to your new Bitbucket project
6. Enable bitbucket pipeline for your repository by following the navigation – <REPOSITORY> -> Repository settings -> Pipelines -> Settings
7. Deployment Settings – Navigate to <REPOSITORY> -> Repository settings->Deployments and add three Bitbucket environments as per screenshot below. These environments will be referred in the pipeline configuration file as well as in Bitbucket dashboard
8. In the repository, ensure that there are 2 branches – develop and master. If not, then create both these branches by clicking the ‘Create Branch’ button
9. Create a new feature branch named ‘feature/CICD-Setup’ from the develop branch.
10. Push all the files from your Mulesoft application to this feature branch. Also ensure that 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
Also, ensure that the pom.xml file of your mule application has the below configuration for mule-maven-plugin and properties versions
11. A PUSH to the feature branch automatically triggers a Bitbucket pipeline. Verify this by navigating to <REPOSITORY> -> pipelines. Ensure that the status of the running pipeline gets to completed
12. Raise a Pull Request to merge the feature branch with develop branch.
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. After successful merge, you should be able to see a screen like this –
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
Click the pipeline to see the details
As you can see, this pipeline involves 2 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
14. Confirm that the deployment is successful by verifying if the application has been deployed in Anypoint Runtime Manager in Cloudhub
Steps for UAT and Production deployment
1. As per our branching strategy, for moving code to UAT we need to create a release branch. Create this release branch in bitbucket repository based on the develop branch
2. Immediately after the branch is created, a pipeline will be triggered for the release branch. Verify this by navigating to <REPOSITORY> -> Pipelines
It has 3 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.
3. Verify in Cloudhub UAT environment if the deployment is successful.
4. After testing is completed in the UAT environment, code can be moved to production. For this, the only thing needed, is to click the deploy button displayed for Step 3. Click this button and wait for the status to turn green
5. Once the status for step 3 turns green 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
3. Delete the feature branches and release branch