Deploy MuleSoft application to CloudHub using GitHub Actions CI/CD Pipeline

  • January 06, 2022

What is CI/CD?

CI/CD stands for Continuous Integration and Continuous Deployment/Continuous Delivery.

Continuous Integration is the practice where developers work on different git branches and constantly merge the changed code to a central repository.

The code is merged to a working branch, where automated workflows are used to build and test the code.

This ensures all the merged code is well-tested and functioning properly.

Continuous Deployment is the automated process of releasing the merged and tested code from the central repository to the production environment, where users/customers can use it.

What is GitHub Actions?

GitHub Actions helps in automation of tasks within the software development lifecycle. GitHub Actions are event-driven, which means that it can run a series of commands after a specified event has occurred. For example, every time someone pushes a code change to a repository, it can automatically run a command that builds and deploys the code.

Prerequisites

  • GitHub account and knowledge of git. If you don’t have an account, you can create it here: https://github.com/
  • Git installed in the system
  • Anypoint Platform account
  • Anypoint Studio

Setting up the Mule application

Open the project and go to the pom.xml file.

Add the deployment configuration in the Mule-Maven-Plugin like the screenshot below:

<configuration>

<cloudHubDeployment>

<uri>https://anypoint.mulesoft.com</uri>

<muleVersion>4.3.0</muleVersion>

<username>${anypoint.username}</username>

<password>${anypoint.password}</password>

<applicationName>mule-app-cicd-github</applicationName>

<environment>Sandbox</environment>

<workerType>MICRO</workerType>

<region>us-east-2</region>

<workers>1</workers>

<objectStoreV2>true</objectStoreV2>

</cloudHubDeployment>

</configuration>

Important

  • The muleVersion should be the same as that used in the project and should be supported by CloudHub in Runtime Manager.
  • Application name should be unique.
  • objectStorev2 should be set to “true.”

Push the project to the GitHub repository

  1. Sign in to your GitHub account and create a repository.
  2. Now go to the project location in your system and open the Command Prompt there and initialize the git repository using command: git init
  3. Add the files to the git repository using the command: git add.
  4. Commit the changes to the git repository using the command: a.git commit -m “Initial commit”.
  5. Change the branch to main with command: git branch -M main
  6. Add remote origin with command git remote add origin https://github.com//.git a.E.g.: git remote add origin https://github.com/API1619/Demo-app.git
  7. Push the project to the github repository using the command: git push -u origin main

Refresh you git repository and check the uploaded project

Setting up the workflow in GitHub

  1. Click the actions tab in the repository.
  2. On the new page, click the “Set up this workflow button”.
  3. In the workflow file, copy and paste the code from the following link:

https://github.com/API1619/Demo-app/blob/main/.github/workflows/build-and-deploy-on-push.yml

This workflow will first build the MuleSoft project and deploy it to CloudHub. This workflow contains two jobs:

  1. To build the application
  2. To deploy the application to CloudHub

The workflow is triggered by a push on the main branch.

For other branches in GitHub and for pull request trigger, add the branch name as shown following:

Build Job

  • The Java environment for building the application would be set up because MuleSoft is based on Java.
  • In the next step, packaging will create an output jar file which will be used to deploy the application.
  • Every artifact will be stamped with the commit hash, so we can find out which commit is being deployed.
  • In the last step of the build job, the build artifact will be uploaded, which will allow the build and deploy job to share the data between them.
  • You can find the artifact file in your repository in the Actions tab after the job is completed.

Deploy Job

  • The deploy job will first check the repository and will retrieve all the related dependencies.
  • In the second step, it will download the artifact file uploaded by the build job.
  • In the third and final step, it will retrieve the Anypoint platform’s user name and password from the Secrets to deploy the application to CloudHub.

Deploy Job will only execute if the Build job is successfully completed.

Give a suitable name to the file and click the “Start commit” button.

Click the “Commit new file” button.

Adding the Anypoint Platform user name and password as secret

  1. Click on the Settings tab in the repository.
  2. On the settings page, click the Secrets from the left side and then click the “New repository secret” button.
  3. In the new section that opens, keep the Name field as “USERNAME” and in the Value field provide your Anypoint Platform user name and click the “Add secret” button.
  4. Click the “New repository secret” button again and in the new section that opens, keep the Name field as “PASSWORD” and in the Value field provide your Anypoint Platform PASSWORD and click the “Add secret” button.

Now check if both the secrets are available in the “Repository secrets” section.

Deploying Application through Actions

  1. Open your project and make any minor change (to mock a change in the application).
  2. Open the command prompt at the project location and in the command prompt run the command: git pull
  3. Now run: git add
  4. Do a commit with command: git commit -m “
  5. Push the code with the command: git push -u origin main
  6. Go back to your GitHub repository and open the Actions tab, and your job should’ve started.
  7. Click on the job and see the logs.
  8. Click on the Actions tab again and see that both Build and Deploy jobs have a green tick, indicating successful build and deployment. Also check the Artifact after the job is completed.
  9. Sign in to your Anypoint Platform account and go to Runtime Manager. You can see your app has been deployed and started.
  10. Click on your deployed app in Runtime Manager and check the details.

— By Ashish Jha