top of page
Writer's pictureAlex Martinez

Part 3: CI/CD pipeline with MuleSoft and GitHub Actions - MUnit testing



 

Other posts from this series:

 

In this post:

 


In the previous article, we learned how to add a decryption key to your CI/CD pipeline to be able to decrypt your encrypted properties at runtime. If you haven’t been following the series or you’re not familiar with GitHub Actions, we recommend you start from the first article to understand how we are setting up all the configurations we need.


In this post, we’ll see the steps to create a pipeline with GitHub Actions that will run the MUnit tests you have set up in your Mule project.


ℹ️ Note: We will talk about coverage percentage in the next article. We’ll only focus on creating the basic testing configuration in this post.



Prerequisites


You should already have all the setup in your Mule application for running the MUnits. In summary, this is what you should already have:

  • The required dependencies, plugins, or properties in your pom.xml to be able to run MUnits for your Mule project.

  • The working MUnit tests under src/test/munit.

  • Enterprise Nexus credentials to run the MUnits in the CI/CD pipeline. For more information see this question. Note that we didn’t need these credentials before but we need them to be able to run the tests because of some dependencies.


⚠️ Important: Make sure your MUnit tests work locally (from Anypoint Studio) before attempting to create the pipeline. Otherwise, you might encounter some issues.
ℹ️ Note: You do not need Nexus credentials to run the MUnits from Studio, only to run them with Maven.


Set up your credentials


In your GitHub repository, go to the Settings tab (make sure you are signed in to see it). Now go to Secrets and variables > Actions. Here you will be able to set up your repository secrets.


In the previous articles, we added the following actions secrets:

  • ANYPOINT_PLATFORM_PASSWORD

  • ANYPOINT_PLATFORM_USERNAME

  • DECRYPTION_KEY


For this post, we’re going to add two more secrets for the nexus credentials:

  • NEXUS_USERNAME

  • NEXUS_PASSWORD


Click on New repository secret. In the Name field, write the name of the secret. In the Secret field, write the actual value of your key (your Nexus username/password). Click Add secret.


Please make sure your Nexus credentials are valid before continuing. To verify this, go to this site and sign in.



You should be able to see the welcome page after logging in.




Set up your build.yml


In the last posts, we set up and modified our build.yml file under .github/workflows. This time, we are going to be using the same base file, just adding a few modifications to include the new configuration.


You can copy and paste the following code into this file.



As you can see, we added a new test job that will run the MUnits before continuing with the rest of the jobs (build/deploy). We have seen the first steps before with the other two jobs, but the last step is different from what we’ve seen before. We also added the line needs: test under the build job so the jobs run synchronously. Finally, we added -DskipMunitTests to the two maven commands from build and deploy. Since we are running the tests on the first step, there’s no need to re-run them after that.


The nexus_username and nexus_password are being passed as environment variables implicitly. While the KEY variable is being used in the maven command. Let’s take a closer look at the maven command.


mvn test --settings .maven/settings.xml -Dsecure.key="$KEY"

Since we are using a specific configuration in our settings.xml to include the Nexus repository/credentials, we will be adding this file directly to the project (without hardcoding the credentials!!). We’ll see this configuration next.


⚠️ Another important change in this command is that this time we are sending the Mule property directly. In the previous post, we saw the decryption key property was called secure.key. If your property within the Mule project is named differently, please update this command appropriately.



Set up your settings.xml


As we just mentioned, we hadn’t created a settings.xml in the project before. Mainly because there was no need to do so. This time, since we’re using enterprise Nexus credentials to run the MUnit tests with Maven, we do need to set up specific credentials, profiles, and repositories.


To start, create a new .maven folder at the root of your project. Inside this folder, create a settings.xml file. For example, this is how my folder structure looks like:


github-actions/
  - .github/
  - .maven/
    - settings.xml
  - src/
  - mule-artifact.json
  - pom.xml

Now copy and paste the following file. There’s no need to modify anything (just make sure the name is settings.xml).



As you can see, the Nexus username and password credentials match the environment variables we are sending through the build.yml file.


🗒️ Docs: For more information, see Configuring Maven to Work with Mule.


Modify your pom.xml


This time there’s nothing to add to your pom.xml file. The MUnit dependencies and needed configuration should be added automatically by MuleSoft once you add the tests to your code. However, here’s my project’s pom in case you want to compare it with yours:




Run the pipeline


That’s it! Once you’re done with the changes, simply push a new change to the main branch and this will trigger the pipeline.




More resources


You can check out my GitHub profile for more CI/CD repos:


I hope this was helpful!


Don't forget to subscribe so you don't miss any future content.







2,521 views0 comments

Comments


bottom of page