Deploying MuleSoft Application to Cloudhub 2.0 using Azure DevOps - Part 1
Tags: mulesoft, azuredevops, tutorial, devops
Published: 2023-03-01 08:35:09

This article we will talk about how to deploy a MuleSoft Application using Azure DevOps as a CI/CD tool with yaml file as scripts to reuse in any projects.

We will use Azure Repos as git repository to pipeline scripts and the MuleSoft application.

I'll split the full process in 2 articles, this article we will explore the deploy to develop environment and the next article we will explore the deploy to production environment.

## Azure DevOps Project

Create a [new Project](https://learn.microsoft.com/en-us/azure/devops/organizations/projects/create-project?view=azure-devops&tabs=browser) in Azure DevOps with **mulesoft-projects** name

## Importing Script Pipeline

After create your project in Azure DevOps, we need import the [devops-scripts](https://github.com/brunosouzas/devops-scripts.git) to [azure repos](https://learn.microsoft.com/en-us/azure/devops/repos/git/import-git-repository?source=recommendations&view=azure-devops).

Ps. Use the **devops-scripts** as repo name with **main** branch.

## Azure DevOps Configuration

Now we will create the variables, you can navigate to Pipeline menu in your project and click on **Library**.

![Azure DevOps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkwfvz2bwc5mwpr7pooj.png)

### Variable Groups

[Variable Groups](https://docs.microsoft.com/en-us/azure/devops/pipelines/library/variable-groups?view=azure-devops&tabs=yaml) are frequently used to store values that are desirable to make available across the pipelines. Additionally, they can be used to store secrets and credentials that are going to be needed into our build and release pipelines. We are going to create the following variable groups:

- **anypoint-platform-variables** :
  - AnypointClientId - Specifies the Connected App clientID value
  - AnypointClientSecret - Specifies the Connected App secret key.

- **anypoint-platform-\<ENVIRONMENT\>-variables** :
  - chEnv - Specifies the Cloudhub Environment Name
  - muleEnv - Specifies the Mule Environment used in your project

- **\<APPLICATION NAME\>** :
  - encryptKey - encrypt key for production
  - encryptKeyNonProd - encrypt key for non-production

_This variable is used to add common passwords used in all application, if you have to use dynamic properties, we'll talk about in another article how to add dynamic properties with parent-pom._

![Azure DevOps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4e91hyd846waebkn8feh.png)

### Connected App
Click [here](https://docs.mulesoft.com/access-management/connected-apps-developers#create-a-connected-app) to learn how to create a Connected App at Anypoint Platform.

This is the scopes you need add to deploy process

| **Runtime Manager** |
| --- |
| Create Applications |
| Read Applications |
| **General** |
| View Environment |
| View Organization |
| **Exchange** |
| Exchange Contributor |
| Exchange Viewer |


### Secure Files

[Secure Files](https://docs.microsoft.com/en-us/azure/devops/pipelines/library/secure-files?view=azure-devops) can be used to encrypt valuable resources that will be needed during the build and release pipelines.

The Maven **settings.xml** file containing:

  - MuleSoft Enterprise Repository Nexus Credentials.
  - Connected APP to deploy

In devops-script repository has a template into /common folder

Add the **settings.xml** in secure file to be use in deploy process.

![Azure DevOps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ftetb74vpatzjeo4gcs1.png)

### Deployment Steps

The pipeline will be triggered when you do a commit in develop branch, we're testing the code, creating an Artifact to use in Production deploy and do the deploy to develop environment.

The Release Pipeline is a manual process to deploy in production environment using the artifact created in develop steps with approvers.

The diagram below illustrates the concept:


![Azure DevOps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vs36yb4fa0hy7h4ttseo.png)

### Scripts

We have 2 main scripts in our process:

- azure-pipelines.yml
- mulesoft-jobs.yml

The script **azure-pipeline.yml** is in raw folder from MuleSoft application and contains the minimal configuration to start the process.

The script **mulesoft-jobs.yml** is in devops-scripts repository, is the common script with the steps of the deploy process.

## MuleSoft Application

To this example you will import this [example](https://github.com/brunosouzas/mulesoft-hello-world.git).

I project you have 2 important files:

- azure-pipelines.yml
- pom.xml

### Azure-pipelines.yml

```
name: $(Date:yyyyMMdd).$(Rev:r)
resources:
  repositories:
  - repository: devops-scripts
    type: git
    name: mulesoft-projects/devops-scripts
    ref: refs/heads/main
    
trigger:
- develop
- release-\*
    
stages:
- template: mulesoft-jobs.yml@devops-scripts
```

**Name** : This is the code you see every time you run the pipeline
**Resources/Repositories** : The repository used to call the template in stage
**Trigger** : the branches that will trigger
**Stages/Template** : main script yaml from process

### Pom.xml

The configuration to deploy in Cloudhub

```
<cloudhub2Deployment>
…
</cloudhub2Deployment>
```

The config to deploy in Anypoint Exchange

```
<distributionManagement>
…
</distributionManagement>
```

If you need more details about deploy config using maven, click [here](https://docs.mulesoft.com/mule-runtime/4.4/deploy-to-cloudhub-2)

## Creating Pipeline to your Application

Go to Pipelines and click in New Pipeline 

![Azure DevOps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pk3hzfrr9howss7hh5io.png)

Choose Azure Repos Git (YAML)

![Azure DevOps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7fr1b14e6a8ter9d04c.png)

Choose the Mule Application Repository (hello-world)

![Azure DevOps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sn7lgfb3vnotuby3nx50.png)

You can see the azure-pipelines.yml, Click in Save

![Azure DevOps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8qmia3qqdpatpdnqinbq.png)

Now you can Run the Pipeline (use develop branch, the script has a rule to only deploy develop branch, to production we will use the Release Pipelines)

![Azure DevOps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bv43y10995xusn5ohgcx.png)

![Azure DevOps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6o4rgdptu4hf5rke6uvk.png)

Now click in your pipeline to follow the steps and in your first time you need permit the access to Devops-scripts repository and variable group.

![Azure DevOps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vdmmlgebxqqlye1kpn5h.png)

![Azure DevOps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9w15k2fwznufh6lyasl5.png)

## Config Slack/Teams to Azure DevOps
The notification doesn't in scripts, you need config your tools using the links below.

[Slack](https://learn.microsoft.com/en-us/azure/devops/pipelines/integrations/slack?view=azure-devops)
[Microsoft Teams](https://learn.microsoft.com/en-us/azure/devops/pipelines/integrations/microsoft-teams?view=azure-devops)

## Conclusion

This article does not have the complete deployment process, but the idea is to show how to deploy using the yaml file to reuse in other projects. I tried to use a simple idea using yaml scripts and I believe with this you can study and learn more.

In yaml script you will see a maven setup script, this script is due a MuleSoft issue with maven 3.9 in this moment (feb-2023).

**Reference:**
[gitflow-workflow](https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow)
[Azure-DevOps](https://learn.microsoft.com/en-us/azure/devops/?view=azure-devops)

**Code Source**
[devops-scripts](https://github.com/brunosouzas/devops-scripts)
[MuleSoft hello-world](https://github.com/brunosouzas/mulesoft-hello-world)