Mule 4 Continuous Integration Using Azure DevOps
An integration engineer gives a tutorial on creating an Azure project and integrating different branches together using Mule 4.
Join the DZone community and get the full member experience.Join For Free
Once we started developing applications in MuleSoft and storing our code in source control platforms like GitHub, Bitbucket, GitLab, or Azure, just to mention the most common ones, we needed to look into automating the process to deploy our applications either to CloudHub or an on-premise server.
In this post, I will try to explain how a MuleSoft application can be automatically deployed into CloudHub or an on-premise server from Azure DevOps as our main CI platform and source control platform.
Create a Project in Azure
The first step is to setup our project in Azure DevOps. For this, you need a Microsoft account, which you can set up here: https://dev.azure.com/.
Then we can create a new project, provide a name and a description, as well as set the privacy; by default, it comes set as "private."
Once this is created, we can go into the main page and locate the "Repos" section. Once we see the page, we can use the information to send our code directly from our local environment.
But just before actually pushing any code, we can create a pretty simple endpoint that we can hit later to verify everything is working. So, in Anypoint, create a pretty simple RAML definition:
Then we can generate the flows in our project: right-click on the RAML file > Mule > Generate Flows from Local REST API.
Then, once we have our flow generated, we can proceed to create our
test.yaml file in order to identify any value we want to attach to our environment.
It will look just like this:
Then we can create a
global.xml configuration file in which to keep our configurations. We can add a Configuration Property config as shown below:
Then we can create a Global property, in this case called
env, which will allow us to know in which environment we are working.
I know, it's a lot, but let's try to do it right. Just before our final check to verify everything is working, let's add an environment variable called
env in our Run Configuration.
Then let's run the project. If all went okay, we'll see our project deployed!
So if it's all good, then we can start pushing our first version of this working code into our Azure repo. Let's do that next.
Generating a Personal Access Token
Let's go back into the Azure DevOps main project page:
And then let's create a personal access token:
Click on 'Create a new token.' You'll see a bunch of options; select the actions and permissions you consider best depending of your needs.
Once created, you'll have a new token which becomes your project password, so keep it safe!
Now we can push our code. If the terminal asks you for a password, use the token you just generated and your code should be on the master branch:
All right, so we have our code already in Azure. Now, let's create a new branch for our repository. Let's call it test, based on the master, and let's create a developer branch called emoran.
It's important to mention that during this example we will review the build on the emoran branch and we will deploy to CloudHub in the test branch.
Create an Artifact
Before we create our pipeline, we need to make sure we have the right information in our
pom.xml files. This artifact will allow us to get the Distribution Management piece we need to set in our project. For this, we will need to go into our main project page and locate the artifact option on the left, then we need to click on "Connect to feed." Next, select Maven and it will show the information we need to put into our
Preparing Our settings.xml and pom.xml Files
Now, the next task will be to prepare our files in order to make sure we can build and deploy our application into CloudHub.
Here's how my settings file looks:
And here's the
Basically, we are specifying the location of our repository for source control purposes and then specifying where we want to deploy our application with the cloudHubDeployment configuration in the
We can verify everything works using the following command:
Create a Pipeline
Now we can create our pipeline configuration, find the Pipelines options, and select "Create Pipeline."
Select the Azure Repos Git option and select your repo from your project.
In the next step, we can select Maven and then a template YAML file will be generated (we will change this eventually).
When you finish the new YAML file will it appear as part of your code. We need to modify the script, in this case, leaving it as shown below:
It is important at this step to set the variables we will use during deployment. For example, credentials to login to nexus repositories, Anypoint Platform, and our personal tokens we created in Azure.
Once we have created our pipeline we can select the option Variables:
Finally, let's create as many as we need, according to what we need from our YAML file:
So now the last piece of the puzzle is basically to send a change from the source control to Azure on the first step (sending a change on emoran) branch. For example:
We'll see our pipeline running the emoran branch:
When it finishes, we'll see this brach had a clean install build:
Now everything happens in source control, so if we want to deploy our application to Cloudhub, then we need to promote our changes from the emoran branch to the test branch.
Now, let's create a pull request from Azure, from the emoran branch to the test branch:
Once we click Create, then we need to complete the pull request and merge request in order to start the pipeline.
When the pipeline that we ran for the test branch finishes, we can see that the deployment happened this time:
So now we can go to Anypoint Platform and see our app deploying:
Finally, you can set as many branches as you need and try out other processes.
I hope this helps! You can find full code here.
I hope to hear from you in the comments!
Opinions expressed by DZone contributors are their own.