Faster Mule deployments using Gitlab cache

yucelmoran

Edgar Moran

Posted on August 31, 2023

Faster Mule deployments using Gitlab cache

Today I was curious about how we can make our deployments faster using CI processes, we have multiple platforms to handle the CI deployments for example GitHub, GitLab, Bitbucket CircleCI, TravisCI etc.. In this case I’m using GitLab.

I created one application in MulesSoft with one simple scheduler and a logger, really I just want to test the deployment

Image description

the only couple important items to consider is to add the .gitlab-ci.yml file and to setup your build tag in your pom.xml file. Lets see how our .gitlab-ci.yml looks like:

image: maven:3.6.1-jdk-8

variables: 
  MAVEN_OPTS: "-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository"

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - .m2/repository

stages:
  - build 
  - test
  - deploy-staging
  - deploy-production

build:
  stage: build
  script:
    - mvn  -U -V -e -B clean -DskipTests package
  only:
    - merge_requests

test:
  stage: test
  script:
    - mvn -U clean test
  only:
    - merge_requests
  artifacts:
    when: always
    reports:
      junit:
        - target/surefire-reports/TEST-*.xml  

deploy-staging:
  stage: deploy-staging
  script:
    - mvn -U -V -e -B clean -DskipTests deploy -DmuleDeploy
  rules:
    - if: '$CI_COMMIT_BRANCH == "staging"'
Enter fullscreen mode Exit fullscreen mode

As we can see, I’m specifying these lines, with this, I tell Gitlab to cache the dependencies in the .m2 repository and the key there will allow to persist the dependencies in every branch.

variables: 
  MAVEN_OPTS: "-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository"

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - .m2/repository
Enter fullscreen mode Exit fullscreen mode

In my pom.xml this is the setup I have:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.mycompany</groupId>
 <artifactId>gitlab-cache-deployment</artifactId>
 <version>1.0.0</version>
 <packaging>mule-application</packaging>

 <name>gitlab-cache-deployment</name>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  <app.runtime>4.4.0</app.runtime>
  <mule.maven.plugin.version>3.8.2</mule.maven.plugin.version>
 </properties>

 <build> 
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-clean-plugin</artifactId>
    <version>3.2.0</version>
   </plugin>
   <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>
     <cloudHubDeployment>
      <uri>${CLOUDHUB_URI}</uri>
      <muleVersion>4.4.0</muleVersion>
      <connectedAppClientId>${CLIENT_ID}</connectedAppClientId>
      <connectedAppClientSecret>${CLIENT_SECRET}</connectedAppClientSecret>
      <connectedAppGrantType>client_credentials</connectedAppGrantType>
      <environment>Sandbox</environment>
      <applicationName>gitlab-cache-deployment</applicationName>
      <workerType>Micro</workerType>
      <objectStoreV2>true</objectStoreV2>
     </cloudHubDeployment>
    </configuration>
   </plugin>
  </plugins>
 </build>

 <dependencies>
  <dependency>
   <groupId>org.mule.connectors</groupId>
   <artifactId>mule-http-connector</artifactId>
   <version>1.7.3</version>
   <classifier>mule-plugin</classifier>
  </dependency>
  <dependency>
   <groupId>org.mule.connectors</groupId>
   <artifactId>mule-sockets-connector</artifactId>
   <version>1.2.3</version>
   <classifier>mule-plugin</classifier>
  </dependency>
 </dependencies>

 <repositories>
  <repository>
   <id>anypoint-exchange-v3</id>
   <name>Anypoint Exchange</name>
   <url>https://maven.anypoint.mulesoft.com/api/v3/maven</url>
   <layout>default</layout>
  </repository>
  <repository>
   <id>mulesoft-releases</id>
   <name>MuleSoft Releases Repository</name>
   <url>https://repository.mulesoft.org/releases/</url>
   <layout>default</layout>
  </repository>
 </repositories>

 <pluginRepositories>
  <pluginRepository>
   <id>mulesoft-releases</id>
   <name>MuleSoft Releases Repository</name>
   <layout>default</layout>
   <url>https://repository.mulesoft.org/releases/</url>
   <snapshots>
    <enabled>false</enabled>
   </snapshots>
  </pluginRepository>
 </pluginRepositories>

</project>
Enter fullscreen mode Exit fullscreen mode

Now I created couple repositories one for an application using the cache and a second one NO using the cache in the ci yaml file, this way we can validate and check performance between both apps. I created in both repos three branches (master, staging, mydevbranch). In order to verify performance, out pipeline has three stages

build: Only builds the project and verifies is succesful
test: will run the test (MUnit) in the pipeline
deploy: After a PR is approved and merged from Dev branch to Staging will deploy to Anypoint Platform.
The comparison
In the end using the cache improves in terms of minutes the time of running a build, test or deploy

No cache:

Build: took 1 minute, 37 seconds
Test: took 1 minute, 37 seconds
Deploy: Took 3 minutes 42 seconds

Image description

With cache

Build: took 32 seconds
Test: 38 seconds
Deploy: 3 minutes 42 seconds

Image description

As we can see there’s improvement on test and build while deployment seems to be the same, in the end a few minutes gained is better.

Image description

Image description

I will keep investigating if there are better ways to enhance the time of deployment, even some times the time is related to network, and the availability on the platform as well.

Hope this help you in your deployments!

💖 💪 🙅 🚩
yucelmoran
Edgar Moran

Posted on August 31, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related