DEV Community

Alex Martinez
Alex Martinez

Posted on • Edited on

Quick reference: CI/CD for a Mule app using a Connected App

This is intended as a short guide. For detailed instructions + video, please see this series. You can also see this repo.

Connected App

In Anypoint Platform, go to Access Management > Connected Apps > Create app. Select App acts on it's own behalf. Add the scopes:

  • Design Center Developer
  • View Environment
  • View Organization
  • Profile
  • Cloudhub Organization Admin
  • Create Applications
  • Delete Applications
  • Download Applications
  • Read Applications
  • Read Servers

Copy ID and Secret.

GitHub Actions secrets

In your GitHub repo, go to Settings > Secrets and variables > Actions > New repository secret and add the following secrets corresponding to your previous ID and Secret.

  • CONNECTED_APP_CLIENT_ID
  • CONNECTED_APP_CLIENT_SECRET

build.yml

In your Mule app's code or repo, create a .github folder with a workflows folder. Inside this, create a build.yml and paste the following:

name: Build and Deploy

on:
  push:
    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 -DskipMunitTests
        - 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
          run: |
            artifactName=$(ls *.jar | head -1)
            mvn deploy -DskipMunitTests -DmuleDeploy \
             -Dmule.artifact=$artifactName \
             -Dclient.id="${{ secrets.CONNECTED_APP_CLIENT_ID }}" \
             -Dclient.secret="${{ secrets.CONNECTED_APP_CLIENT_SECRET }}"
Enter fullscreen mode Exit fullscreen mode

pom.xml

Make sure the config matches your application. For example, muleVersion, applicationName, environment, region, and so on.

<plugin>
  <groupId>org.mule.tools.maven</groupId>
  <artifactId>mule-maven-plugin</artifactId>
  <version>${mule.maven.plugin.version}</version>
  <extensions>true</extensions>
  <!-- Deployment config start -->
  <configuration>
    <cloudHubDeployment>
      <uri>https://anypoint.mulesoft.com</uri>
      <muleVersion>4.4.0</muleVersion>
      <applicationName>mulesoft-mfa-cicd</applicationName>
      <environment>Sandbox</environment>
      <workerType>MICRO</workerType>
      <region>us-east-2</region>
      <workers>1</workers>
      <objectStoreV2>true</objectStoreV2>
      <connectedAppClientId>${client.id}</connectedAppClientId>
      <connectedAppClientSecret>${client.secret}</connectedAppClientSecret>
      <connectedAppGrantType>client_credentials</connectedAppGrantType>
      <properties>
        <env>${env}</env>
      </properties>
    </cloudHubDeployment>
  </configuration>
  <!-- Deployment config end -->
</plugin>
Enter fullscreen mode Exit fullscreen mode

That's it! Commit, push, and watch it run :)

Top comments (0)