CI/CD with GitHub Actions — Deploy a Mule application to CloudHub

Alex Martinez
Another Integration Blog
4 min readFeb 9, 2023

This is super convenient to automate. Especially if you keep your Mule applications in a GitHub repo. This way, you don’t have to manually keep deploying to CloudHub every time there’s a change to your code.

I’ll try to keep this post short so you can refer to it anytime you need and simply Copy+Paste what you need. For a more detailed explanation + video, you can check out this article instead.

Step 1: Set up your GitHub repo

I will be using this repo as an example. Here is where you will keep your Mule application’s code.

Once you have your code in GitHub, create a new folder called .github. Inside this new folder, create another folder called workflows. Here is where you can create the YAML file to keep the actual pipeline’s code. Create a new YAML file. You can name this whatever you want, but we’re gonna name it build.yml.

Copy and paste the following code for this new file. You can also copy and paste from this file.

# This workflow will build a MuleSoft project and deploy to CloudHub

name: Build and Deploy to Sandbox

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout this repo
uses: actions/checkout@v3
- name: Cache dependencies
uses: actions/cache@v3
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- name: Set up JDK 1.8
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 8
- name: Build with Maven
run: mvn -B package --file pom.xml
- name: Stamp artifact file name with commit hash
run: |
artifactName1=$(ls target/*.jar | head -1)
commitHash=$(git rev-parse --short "$GITHUB_SHA")
artifactName2=$(ls target/*.jar | head -1 | sed "s/.jar/-$commitHash.jar/g")
mv $artifactName1 $artifactName2
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: artifacts
path: target/*.jar

deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout this repo
uses: actions/checkout@v3
- name: Cache dependencies
uses: actions/cache@v3
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- uses: actions/download-artifact@v3
with:
name: artifacts
- name: Deploy to Sandbox
env:
USERNAME: ${{ secrets.anypoint_platform_username }}
PASSWORD: ${{ secrets.anypoint_platform_password }}
run: |
artifactName=$(ls *.jar | head -1)
mvn deploy -DmuleDeploy \
-Dmule.artifact=$artifactName \
-Danypoint.username="$USERNAME" \
-Danypoint.password="$PASSWORD"

In summary, every time there’s a push or a pull request merged into the main branch, this pipeline will start running. It is divided into two jobs: build and deploy.

The build job will end up creating the jar file that will be deployed into CH, while the deploy job will take that file and actually perform the deployment using your credentials.

Where do these credentials come from, you ask?? Let’s set them up!

Step 2: Set up your credentials

In the GitHub repository, go to the Settings tab and scroll down to Secrets and variables > Actions.

In the previous screenshot, I already have my two credentials set up under Repository secrets. You shouldn’t have anything listed here. Click on New repository secret to create the two credentials we’ll use: ANYPOINT_PLATFORM_PASSWORD and ANYPOINT_PLATFORM_USERNAME.

In the name field, add ANYPOINT_PLATFORM_USERNAME. In the secret field, the actual username you use to log in to Anypoint Platform. For me it’s amartinez. Repeat the same process for the password.

If you want to add more secrets, like a decryption key, you can do so here too. Just make sure you pass the value in the Deploy to Sandbox step from the deploy job. Check out an example here.

Step 3: Run the pipeline

Once you have set up your credentials and your YAML file for the pipeline, it’s time to run the pipeline.

Simply make a change to the repository and push it to the main branch. This will trigger the pipeline.

You can see all the Actions (pipelines) that have run in your repository by going to the Actions tab.

Here, you can click on any of the previous/current runs to see a detailed view of the jobs, the logs, and the steps.

That’s all! Now every time you make a change to the main branch, your Mule application will be automatically deployed to CloudHub in Anypoint Platform.

As mentioned at the beginning of the post, you can see a more detailed explanation + video in this article.

Go to ProstDev to keep learning MuleSoft + DataWeave tips!

--

--

Alex Martinez
Another Integration Blog

Developer Advocate at MuleSoft | Co-Author of MuleSoft for Salesforce Developers | ProstDev Founder | MuleSoft Ambassadress Alumni