Azure DevOps CI/CD with Mule Applications and CloudHub
- January 05, 2022
Introduction
The goal is this document is to describe the use case of integrating a MuleSoft application — MuleSoft CloudHub — and Azure CI/CD and the steps needed to create a CI/CD pipeline for deploying a MuleSoft application to CloudHub using Azure DevOps.
What is CI/CD?
CI/CD is the acronym for continuous integration and continuous deployment/delivery.
Continuous integration refers to 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 the code is built and tested using automated workflows. This ensures all merged codes are together, functioning properly and well tested.
Continuous deployment takes the merged and tested code from the central repository, releases it to the production environment using an automated process and makes it available to users/customers.
What is Azure DevOps?
Azure DevOps is one of the leading tools to automate CI/CD processes. The flow diagram below shows code uploaded from our local system to a central repository (like GitHub repository or Azure repository) that’s connected to the Azure CI/CD pipeline, where the automated testing is done and the project is built and then deployed to the target environment. In our case, we deployed the application to the CloudHub.
Steps to create the CI/CD pipeline
Prerequisites
- Knowledge of GitHub or Azure is required because you need either the GitHub or the Azure repository
- Git should be installed in the system
- AnyPoint Platform account
- AnyPoint Studio
- Azure DevOps account
Set up the Mule application
Step 1: Open the project explorer and go to the pom.xml file.
Step 2: Add the deployment configuration in the Mule-Maven-Plugin. Refer the below screenshot:
<configuration>
<cloudHubDeployment>
<uri>https://anypoint.mulesoft.com</uri>
<server>${server}</server>
<muleVersion>${app.runtime}</muleVersion>
<applicationName>${app.name}</applicationName>
<workers>${worker}</workers>
<workerType>${workerType}</workerType>
<environment>${environment}</environment>
<objectStoreV2>true</objectStoreV2>
</cloudHubDeployment>
</configuration>
Important:
<objectStorev2> should be set to “true” .<uri> should be set to “https://anypoint.mulesoft.com”
Push the project to the GitHub Repository
Step 1: Sign in to your GitHub account and create a repository
Step 2: Go to the project directory in your system, open the Command Prompt and initialize the GitHub Repository using the command: git init
Step 3: Add the files to the GitHub Repository using the command: git add
Step 4: Commit the changes to the GitHub Repository using the command: git commit -m “Initial commit”
Step 5: Change the branch to main with the command: git branch -M main
Step 6: Add remote origin with the command: git remote add origin https://github.com/<your GitHub username>/<your repository name>.git
For example, git remote add origin http://github.com/API1619/azure-pipeline-demo.git
Step 7: Push the project to the GitHub Repository using the command: git push -u origin main — then refresh your GitHub Repository and check the uploaded project
Step 8: Create a “dev” branch in the GitHub Repository
Set up the Azure CI/CD Pipeline
Step 1: Sign in to your Azure DevOps account; you can create the account at https://azure.microsoft.com/
Step 2: Create a new project and give the project a suitable name and description
Step 3: Click “Pipelines” on the left-side menu
Step 4: Click “Create Pipeline”
Step 5: From the new page that opens, select GitHub — for the first time, Azure will require you to give access to your GitHub account; proceed and give access
Step 6: Select the repository you created in the GitHub and click “Approve and Install”
Step 7: Select your account
Step 8: On the next screen, select “Maven”
Step 9: Click “Variables” and then “New Variable”
Step 10: Provide the name and value of the fields defined in the deployment configuration in the pom.xml file; add the names and values of all the fields and click “Save”
NOTE:
- Application name should be unique
- App runtime should be the same as that used in the project
- Environment can be changed according to the need
Step 11: Click “Save and Run” and then click “Save”
Step 12: Provide a suitable name and click “Save” and then click on “Pipelines” in the left-side menu; you will see that the pipeline has been created
Prepare the azure-pipelines.yml file
Step 1: Copy and paste the contents of the “azure-pipelines.yaml” file from the link given below: https://github.com/API1619/azure-pipeline-demo/blob/dev/azure-pipelines.yml
Important:
- File name should be “azure-pipelines.yml”
- Here we have set the trigger to work when there’s any change to the “dev” branch
- “azure-pipelines.yml” and “pom.xml” are excluded from the trigger, which means any change in these two files will not trigger the job.
- Cache is done so that it won’t download Maven dependencies every time the job is triggered; this helps save time during deployment
Step 2: Save the “azure-pipelines.yml” file and put it in the project’s root directory — make sure the file is in the root directory
Step 3: Create the “settings.xml” file
<settings>
<servers>
<server>
<id>Anypoint</id>
<username>{Place your Anypoint Platform’s Username here}</username>
<password>{Place your Anypoint Platform’s Password here}</password>
</server>
</servers>
</settings>
Deploy the application to CloudHub
Step 1: Go to the project directory, open a command prompt and change the GitHub branch from “main” to dev” using the command: “git branch -M dev”
Step 2: Add the files to GitHub using the command: “git add”
Step 3: Commit the changes using the command: git commit -m “Yaml file added”
Step 4: Push the changes to the repository using the command: “git push -u origin dev”
Step 5: Go to “Library” in Azure Pipelines
Step 6: Click “Variable Group”
Step 7: Give the Variable Group name as “Build-Variable-Group” and then click “+Add”
Step 8: Give the name as “settingsxml.secureFilePath” and the value as “settings.xml” — click “Save”
Step 9: Click “Library” on the left-side menu and then click “Secure files”
Step 10: Click “+Secure file”
Step 11: Browse the “settings.xml” file that we created and click “OK”
Trigger the Job
Step 1: Go to your application and do some minor changes to mock the code change and save the project
Step 2: Add the files to the GitHub Repository using the command: “git add”
Step 3: Commit the change using the command: git commit -m “<your message>”
Step 4: Push the change to the GitHub Repository using the command: “git push -u origin dev”
Step 5: Go to Azure DevOps and click “Pipelines” — you should see that the pipeline has started
Step 6: Click on the pipeline that has started; on the next screen, click the “Stages” icon
Step 7: To run the job for the first time we need to give permission; click “View”
Step 8: Click “Permit” to give permission
IMPORTANT:
- If after Step 8 you get an error stating “No hosted parallelism has been purchased or granted,” then fill in the form provided with the error to request for free parallelism. This may take 2–3 days.
- After getting the free parallelism, do some minor changes again in the code to mock a code change and then push the code to the GitHub Repository. Check the pipeline the job would have started.
- After the job is finished, open your AnyPoint Platform and go to “Runtime Manager.” You’ll find your application deployed there.
- Click on the application to check the details.
Step 9: Watch the logs as the job gets built and deployed; once the job is successfully finished, go to your AnyPoint Platform and, in Runtime Manager, click your deployed application and check the details
— By Ashish Jha