This article uses Azure DevOps as a CI/CD tool and provides a step-by-step guide on how to deploy Mulesoft application in Anypoint cloud with Azure DevOps.
Azure DevOps is one of the leading tools that automate CI/CD’s process. The Azure pipeline combines CICD to consistently test and build the code and sends it to the target environment. It gives the software delivery process in stages like Build, Test, Merge, and Deploy.
Azure Repos will allow you to manage and version control your project code. I have used the same for this proof of concept but we can use some other repos also GITHUB, Bit bucket etc.
Methods of deployment:
- Doing deployment from Create pipeline itself.
To do the deployment from create pipeline we need to configure settings.xml and add <cloudHubDeployment> in pom.xml
- Keeping create and release pipeline separate and doing deployment from release pipeline (best practice to control the deployments to different environments). Here we don’t need to use settings.xml or add < cloudHubDeployment > in pom.xml, we are configuring credentials in variable groups and the deployment is going to be using the Anypoint CLI command, which you will get to know soon in this blog.
Note: I am going to explain method 2 (by creating two different pipelines)
Step 1: Install GITHUB in your local machine and check in a sample code to Azure repo using GIT commands, Go inside your project location, here is an example “sample” is my project in my workspace.
Open command prompt in this location and do GIT initialization, here are the commands to be used to push the code
- Create a new and empty Git repository in the current working directory using the below command. The command only creates the .git repository folder.
- Add the files to your newly created local repository. This stages them for the first commit.
git add .
- Commit the files from your local repository.
git commit -m “First commit”
- Add the URL of the remote repository where your local repository will be pushed.
git remote add origin <remote repository URL>
- Push the changes to GitHub if there is a remote branch called master (or main if that’s what you’re using)
git push origin master
Here is an example with commands. (Commands highlighted in yellow colour). I have already linked my local repo to remote repo, so I didn’t use the command used for linking.
Now login to Azure DevOps using your credentials (https://dev.azure.com/). Create your own project by clicking the new project and keep it public or private. I have kept it private.
In the snapshot, vinohell is the organization name and sample is my project name,
Go into your project, and explore the options. Click Repos on the left side and go into your repository. We can create new branches here by clicking the default on master and also we can take the clone URL from this page too.
Click Clone and we will use the clone URL for linking the local GitHub repository to the Azure repository.
Now go to pipelines in the left side menu and click new pipeline,
After clicking the new pipeline, we can create the pipeline in two ways. One is via YAML which is a modern way and another one is using a classic editor which is highlighted below. We are going to create a pipeline using classic editor only.
After clicking classic editor you will get below the menu, that select project name, repo name and branch name and click continue.
After clicking Continue, we have to select MAVEN as a template. Type MAVEN in the search tab and select MAVEN template.
By default, the MAVEN template will have the below tasks already and keep it as it is and Save (don’t click save and queue now) it. Click the arrow and you will see the save option in the drop-down menu.
Before we start our pipeline creation first we will save the environment-specific variables in variables groups. Under the library section, go to variable groups and create a “Dev env” variable group. Add the environment-specific details by creating new variables. Likewise, create for 2nd environment (UAT env) too.
Dev env variables:
This is how it will look like when you create two variable groups (highlighted in yellow)
Step 2: After creating the pipeline, you have to add 4 jobs here. Already 3 jobs are in the maven template. So for caching alone we need to add a Cache job (cache is for faster build and it will save the dependencies in cache and use it for next build thus it reduces a lot of time).
Add the below jobs using the (+) symbol
Cache settings highlighted below
Click Maven Pom.xml
Under goals add clean package command and in options add -DskipMunitTests $(MAVEN_OPTS)
Since we are not doing testing I skipped the MUnit testing and added caching command
Add Copy files job to tasks by clicking + symbol,
Copy the build .jar file to the target directory, here is the screenshot for it,
Publish the artifacts job, as shown below,
Step 3: Our CI is in manual now, to make the jobs automatic when the code checks in to our branch, in create pipeline we need to enable continuous integration by clicking the Triggers option, below is the screenshot,
Note: Now if new code checks into the project, continuous integration will happen automatically. Till now we have completed the building of jar and the next step is to configure the deployment
Step 4: Create the release pipeline, here are the available options
After clicking the new, Select Azure App service deployment
This how it should look now.
- We will configure the create pipeline name in the release stage highlighted below,
Select the create pipeline name correctly from which you want to download artifact,
Enable the continuous deployment trigger by clicking the small lighting symbol below,
- After setting the pipeline Enable the continuous deployment trigger by clicking the small lighting symbol below in the release stage.
Step 7: Now we have to add environments in the staging layer. Click +Add
Step 8: We have configured the credentials in variables groups and we need to link them to the stages.
Follow the below steps,
To link variable groups we created to stages, first, click variables in the top menu, then click variable groups now click link variable group and you will be able to see the created variable groups from the library section.
Now we are going to link Dev env variable group to the Dev stage. For the select Dev env variable group and you will see the below pop up. In that select stages and select the Dev environment stage.
For deployment, the Dev stage will use the credentials and environment details to deploy the code to the cloud hub. Link the UAT env variable group to the UAT stage using the same steps explained above.
Step 9: Click Dev stage, we will add Bash script job in tasks section by clicking (+) symbol and write any point CLI command there to deploy in cloud hub.
If you see the command I have not hardcoded the environment details, instead, I am fetching it from variables that we linked to each stage.
Step 10: Please configure pre and post-deployment approvals in the below-highlighted options, the same user or different users who have access to the azure organization can have authority to approve pre and post-deployment approvals to the cloud hub.
They will receive mail for the build, pre and post-deployment approvals.
Step 11: Now the application has been deployed to the cloud hub. Please note since I don’t have two environments to deploy, I just used the environment (Sandbox) and credentials in those two stages (Dev and UAT), I just changed the application name alone in variable groups for two environments.
Cloud hub deployment, should look like this
With the knowledge from this article under your belt, you have learned how to create a CI/CD Pipeline using Azure DevOps and deploy mule application to cloud hub. Understanding the practice of continuous integration will make you a valuable member of any team.