Azure Pipelines is a cloud service that you can use to automatically build and test your code project and make it available to other users. It works with just about any language or project type.

Azure Pipelines combines continuous integration (CI) and continuous delivery (CD) to constantly and consistently test and build your code and ship it to any target.

What you'll build

In this codelab, you're going to build an Azure pipeline to automate the continuous integration and continuous delivery.

What you'll learn

What you'll need

You can create an instance of each system in Cloud Services

How will you use use this tutorial?

Read it through only Read it and complete the exercises

How would you rate your experience with Git?

Novice Intermediate Proficient

How would you rate your experience with Maven?

Novice Intermediate Proficient

How would you rate your experience with Jenkins pipeline?

Novice Intermediate Proficient

For this Lab we are going to use the Hello World example that is an example that is in Exchange. Please feel free to use this lab with any other project and import it into Studio.

Download the project form Exchange

  1. Open Studio
  2. Press the Exchange Button
  3. Search for Hello World
  4. Import the project into your Studio by pressing the Open button

At the end you should see something similar to this:

Update Module Dependencies

This project may have old dependencies, what we are going to do now is to update them.

  1. On the project name do a right click and select Manage Dependencies -> Manage Modules

  1. Check all the dependencies and press Apply and Close

Login to Azure

First login to Azure DevOps Services

Create a new Organization

Once you login, you will see the following screen

  1. Press the Create new organization button

  1. Create the name of the organization and choose the region where you want to host the code and press Continue

Now you are ready to create a new project

Create a project

We created the Organization, now we need to create a new project.

  1. Complete the field with the project name. Press Create Project when finished.

This is going to be part of the git url. I would recommend to not use spaces. Also notice that we decided to make this project private.

Once you create the project, you will see the following screen

  1. On the left side you will see a Menu with the different components that a Project has.
  2. The central panel, describe the projects and the different options you may have.
  3. This panel shows the statistics
  4. Shows the members of this project. You can invite other developers to this project by pressing the blue Invite button on the upper right corner.

Be careful when you invite a user. By default they are invited with the stakeholder role. They won't have access to any repo unless you change her profile. More information can be found here. You can also go to the appendix to learn how to change the profile.

Generate Credentials

This step is needed if you login to Azure using a corporate user.

  1. Go to Repos. You will see the URL.

  1. Click Generate Git Credentials to get the username and password

Take note of the URL, user and password. You are going to need it in the next section.

Great!!! In the next section we are going to configure the Studio Project to use this repo

In this section we are going to initialize our mule project to the Git repository. We will be using EGit that comes with Studio. But feel free to choose any tool you want.

Create a local repository

  1. To create a local repository, In the Package Explorer, right-click the name of your project --> select Team --> select Share Project

  1. In the Configure Git repository window, Select the checkbox for Use or Create a repository in the parent folder of the project. (Note: Using this method you will make sure that the .git folder is created in your project root and not your parent folder.)

  1. Press Create Repository and finally Click Finish.

  1. In the Anypoint Studio package explorer, we can see the arrow mark and questions mark symbols for our studio files. This icon indicates that we have created a Git repository for the project on the local drive, but we have not yet registered it and are not yet tracking changes to the project.

  1. Now we need to register the local repository and track changes. To do this go to the package explorer, right click to the name of the project --> Team --> Commit

  1. Once we have clicked on the Commit button. We will see the Git Staging tab opened up. Select all the files that we want to check-in in Git then right-click on the selected files and click on Add to index.

  1. Add a commit message to the text box on the right and click on the Commit button. Our changes will be stored in the local git repository.

  1. In the package explorer, we can now notice that there is a yellow barrel icon which indicates that the changes have been committed to our local git repo. Also, there is no more arrow icon next to the project name.

Push Changes into the Remote Repository

Now that we have everything in our local repository. We want to push the changes in the remote one.

  1. In the Package Explorer, right-click the name of your project navigate to Team --> Remote --> Push

  1. Complete the URL of the repository created in Azure and push the changes. Don't forget to configure username and password.

  1. Select the Specification for Push from the Source ref and Destination ref and then Click on Add Spec, then Click on Finish.

  1. You can go back to Azure and see the committed changes.

A complete guide on how to use Git with Studio can be foundhere

To do the deployment we are going to use the maven plugin. Information about the Mule Maven Plugin can be foundhere.

In the next section we will configure the pom.xml file.

Configure POM File

  1. Go to Studio and open the pom.xml file.
  2. Find the section where the plugin is.
<plugin>
    <groupId>org.mule.tools.maven</groupId>
    <artifactId>mule-maven-plugin</artifactId>
    <version>${mule.maven.plugin.version}</version>
    <extensions>true</extensions>
    <configuration>
        <classifier>mule-application</classifier>
    </configuration>
</plugin>
  1. We are going to add the cloudhubDeployment configuration
  2. Add the following configuration in the configuration tag
<cloudHubDeployment>
    <uri>https://anypoint.mulesoft.com</uri>            (1)
    <muleVersion>4.3.0</muleVersion>                    (2)
    <username>${anypoint.username}</username>           (3)
    <password>${anypoint.password}</password>           (4)
    <environment>${cloudhub.environment}</environment>  (5)
    <applicationName>${cloudhub.app}</applicationName>  (6)
    <workerType>Micro</workerType>                      (7)
</cloudHubDeployment>

These are all parameters that can be hardcoded inside the POM or injected when you do the deployment. The description of each parameter can be found here:

  1. Define anypoint platform (US or EU).
  2. Define Runtime Version
  3. Define anypoint deployment username
  4. Define anypoint deployment password
  5. Define anypoint environment (Sandbox, Production)
  6. Define application name
  7. Define worker type (Micro, Small, Medium)

If you want to test it, you can deploy manually from your computer. Try to execute the following command:

mvn -V -e -DskipTests deploy -DmuleDeploy \
-Danypoint.username=<username> -Danypoint.password=<password> \
-Dcloudhub.app=dev-hello-world -Dcloudhub.environment=Sandbox
  1. Commit the changes into the repo.

Add dependencies

Azure doesn't have a global settings.xml for maven. Everything needs to be in the POM. That's why we are going to add some repositories to the file.

  1. Go to the repository section in the pom.xml file.
  2. Copy the following values:
<repository>
    <id>mulesoft-public</id>
    <url>https://repository.mulesoft.org/nexus/content/repositories/public/</url>
    <layout>default</layout>
</repository> 
  1. In line 15, update the munit.version to 2.2.1.
  1. Commit the changes and let's create a new pipeline.

In this section, we are going to create an Azure pipeline to automate the deployment to cloudhub.

  1. Go to Azure and click in the Pipelines Section.

  1. Press the Create Pipeline button.
  2. Where is your code? Select Azure Repos Git

  1. Select the repo we created before

  1. Choose Maven option

  1. Once you select the configuration pipeline, you will see the yaml file with an example.

  1. Press Save and run

  1. Leave all the values as default and press Save and run. Again

  1. After you clock the button, the pipeline will be executed. Doing a click, you can see the logs being executed.

Azure provides access to the public repositories by default, but if you need access to the Enterprise Repository, you need to follow these steps.

  1. First you need to go to the Project Settings.

  1. Select Service connections

  1. Press Create a service connection

  1. Select Maven in the menu that appear on the right, and press Next.

  1. Configure the details with the following information:

  1. Press Save when finished

Now we need to update the pom.xml file. We will add some libraries needed by the MUnit plugin to run.

  1. Go to your project and open the pom.xml file.
  2. At the end copy the following lines
<pluginRepositories>
    <pluginRepository>
        <id>mulesoft-release</id>
        <name>mulesoft release repository</name>
        <layout>default</layout>
        <url>http://repository.mulesoft.org/releases/</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
    <pluginRepository>
        <id>mule-public</id>
        <url>https://repository.mulesoft.org/nexus/content/repositories/releases</url>
    </pluginRepository>
    <pluginRepository>
        <id>MuleRepository</id>
        <name>MuleRepository</name>
        <url>https://repository.mulesoft.org/nexus-ee/content/repositories/releases-ee/</url>
        <layout>default</layout>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
    <pluginRepository>
        <id>mulesoft-public-plugin</id>
        <name>MuleSoft Public Repository</name>
        <url>https://repository.mulesoft.org/nexus/content/repositories/public/</url>
        <layout>default</layout>
    </pluginRepository>
</pluginRepositories>

Great, you configure the Mulesoft Enterprise Repository in Azure.

In this section we are going to add two more steps to the pipeline.

The first step will build the package. The second one will test the application and the third one will deploy the application to cloudhub.

We need to update the azure-pipelines.yml. This file is in the repo now. You can do a git pull and edit the file in Studio.

In this lab we are going to do in Azure

Build Step

This is the step where the jar is going to be created. This step is created when you create a new pipeline. We are going to make some changes to this step.

  1. Go to the pipelines section and press edit

  1. Add a displayName parameter, so it's easy to identify the step.
- task: Maven@3
  displayName: Build
  inputs:
    mavenPomFile: 'pom.xml'
    mavenOptions: '-Xmx3072m'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.8'
    jdkArchitectureOption: 'x64'
    publishJUnitResults: true
    testResultsFiles: '**/surefire-reports/TEST-*.xml'
    goals: 'package'
  1. Add a maven option parameter to not execute the unit test. step
- task: Maven@3
  displayName: Build
  inputs:
    options: '-DskipTests'
    mavenPomFile: 'pom.xml'
    mavenOptions: '-Xmx3072m'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.8'
    jdkArchitectureOption: 'x64'
    publishJUnitResults: true
    testResultsFiles: '**/surefire-reports/TEST-*.xml'
    goals: 'package'
  1. Remove publishJUnitResult and testResultsFiles parameters.The final version of the task should look like this.
- task: Maven@3
  displayName: Build
  inputs:
    options: '-DskipTests'
    mavenPomFile: 'pom.xml'
    mavenOptions: '-Xmx3072m'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.8'
    jdkArchitectureOption: 'x64'
    goals: 'package'

Test Step

In this step we are going to run the MUnit tests. To execute this step, you need a Nexus user to access the Enterprise Mulesoft Repository. If you don't have one, you can check here to see how to request one. If you aren't a customer and you can't get these credentials, you can skip this step and move to the next one.

As I mentioned before, we are required to configure a private repository. So first go to the appendix to add the Mulesoft Enterprise Repository credentials to the project.

Maven Authentication

Azure doesn't have a global settings.xml where you can configure all the repositories, profiles and credentials. Instead it has a step where you can add the server credentials into the settings.xml installed in the image. That's why we are going to create a Maven Authentication step to add the credentials we created in the appendix.

  1. Stand the cursor on line bejor the goals: 'package' line.

  1. On the right side of the screen you can see a menu with tasks. In the search bar type maven. Choose Maven Authenticate

  1. On the next screen choose MuleRepository from the second combo

  1. Press Add

The step should look like this:

Maven Test

Here we are going to create the maven test task.

  1. Stand the cursor after the maven authenticate step
  2. On the right side of the screen you can see a menu with tasks. In the search bar type maven. Choose Maven.
  3. Configure the following parameters:

Leave the rest as default. Press add when finished.

At the end you should see something like this

Deploy Step

The last step will deploy the package into Cloudhub.

In the pom.xml file we declared 4 variables.

Define Variables

In Azure, we can declare variables to be used in the pipeline. We are going to define one variable to be replaced in the pipeline we deploy.

Define Username Variable

  1. Press the Variables button located on the right top side of the screen

  1. Press New Variable.
  2. Complete with the following:

  1. Press OK.

Define Password Variable

  1. Press the (+) button.
  2. Complete with the following:

  1. Press OK.

Define Environment Variable

  1. Press the (+) button.
  2. Complete with the following:

  1. Press OK

Define Application Name Variable

  1. Press the (+) button.
  2. Complete with the following:

  1. Press OK

Once finished you will see all the variables listed. Press Save to Continue.

Maven Deploy

We declared the variables, now it's time to create the deployment task.

  1. Stand the cursor after the maven test step.
  2. Copy and paste the following code:
- task: Maven@3
  displayName: Deploy
  inputs:
    mavenPomFile: 'pom.xml'
    goals: 'deploy'
    options: '-DskipTests -DmuleDeploy -Danypoint.username=$(mule.username)-Danypoint.password=$(mule.password) -Dcloudhub.environment=$(mule.environment)-Dcloudhub.app=$(mule.app)'
    publishJUnitResults: false
    javaHomeOption: 'JDKVersion'
    mavenVersionOption: 'Default'
    mavenAuthenticateFeed: false
    effectivePomSkip: false
    sonarQubeRunAnalysis: false

At the end you should see something like this

Save the Pipeline

We are going to save the pipeline and this is going to be a new commit.

  1. Press the Save button besides the Variable button.

  1. Add a commit message and description

  1. Press Save to commit the changes.

Congratulations!!! You have the Yaml file with the pipeline.

We created the pipeline, in this section we are going to run it

  1. Press the Run button.

  1. Leave all the values as default and press the Run button again, located at the end of the menu.

After you press run you will see a screen showing the pipeline information.

  1. Go to the Job panel and press the link

When you press the link a live window will appear. It shows the steps and logs of the current step running. Let's wait the process to finish to continue.

  1. Once it's finished you will see a green check mark. You can browse for each step and see the logs.
  2. Go to Cloudhub and check that the app is deployed and running.

  1. You can also see the test report that was created during the run. For that go to the Test Plans

  1. Go to Runs. You will find a list of reports.

  1. Double click on one of them and you will see the detail of that test.

By running the pipeline, we:

In the next section we are going to automate the build process, so each time you push a change, the pipeline starts automatically.

To automate the process is really simple. We just need to update the trigger section in the pipeline file.

  1. Open the pipeline.
  2. Identify the trigger section

  1. Change the file with the following:
trigger:
  batch: true
  branches:
    include:
      - releases/*

With this command all the branches are going to be triggered automatically. Also we are setting the batch property to true, If you have many team members uploading changes often, you may want to reduce the number of runs you start. If you set batch to true, when a pipeline is running, the system waits until the run is completed, then starts another run with all changes that have not yet been built.

  1. Save the pipeline to finish.

If you want to get more information around this subject you can check here

Now we are going to do a change in the project, push the changes and see how the build process starts automatically

  1. First go to Studio and select the logger component
  2. Replace the message field with this one "New Change: " ++ (attributes.requestPath default "NONE")

  1. Commit the change
  2. If you go to Azure, you should see the new job running
  3. Once the deployment is finished, you can make a call to http://<hostname>/helloWorld and check the logs to see the changes.

In this codelab, you learned how to implement a CICD solution using Azure Pipeline. Please take a look at the Resources to learn more about Azure and the Mule Maven Plugin

Resources