Deploy MuleSoft Application To CloudHub with Travis CI

Author: Jitendra Bafna

Introduction

Travis CI is a continuous integration and deployment tool and it can easily build the server which is hosted on GitHub, where projects can be easily built, tested and deployed.

Travis CI is capable of building pull requests. Travis CI is available at https://travis-ci.org/.

For open source projects or public repositories on Github, Travis CI is free.

travis-ci.org supports only public repositories. To support Github private repositories, you can use https://travis-ci.com/

Prerequisite

You need to make sure that you have a GitHub account and this GitHub account can be used to login into travis-ci.

Setting Up Travis CI

For setting up, you can navigate to https://travis-ci.org/ and use GitHub account to login to Travis CI.

Now, it will navigate you to Authorize Travis-CI screen and click on Authorize travis-ci

Once travis-ci has been authorized, it will navigate to TravisCI homepage.

Now, you can go to GitHub ⇒ Settings ⇒ Application, you will see authorized applications under Authorized OAuth Apps.

Adding Repository to Travis CI

For adding repositories to travis ci, you need to go to the home page of travis ci and click on (+) near My Repositories.

Once you click on the (+) button, you will see all public repositories in your GitHub.

Any repository shown in the above list can be activated by clicking the Switch button.

Once you activate the repository in travis-ci, it will create webhooks in GitHub. Navigate to your repository ⇒ Settings ⇒ Webhooks

One important thing to notice, you need to create a .travis.yml file at the root of your project for build to run.

.travis.yml Configuration

Travis requires a .travis.yml configuration file to initiate the build or run.

Minimum requirement in .travis.yml is language

language: java

For our build, we will be using Maven, so the language will be Java.

A list of languages supported by Travis can be found here: https://docs.travis-ci.com/user/languages/

travis-ci.yml can be validated here: https://config.travis-ci.com/explore

Example .travis.yml for Maven will look like this.

language: java
jdk:
  - openjdk8

cache:
  directories:
    - "$HOME/.m2"

For deploying MuleSoft application to cloudhub, you need to configure mule maven plugin in POM.xml

<plugin>
	<groupId>org.mule.tools.maven</groupId>
	<artifactId>mule-maven-plugin</artifactId>
	<version>${mule.maven.plugin.version}</version>
	<extensions>true</extensions>
	<configuration>
		<cloudHubDeployment>
			<server>${server}</server>
			<environment>${environment}</environment>
			<workers>${workers}</workers>
			<workerType>${workerType}</workerType>
			<muleVersion>${muleVersion}</muleVersion>
			<applicationName>${appName}</applicationName>
			<objectStoreV2>true</objectStoreV2>
		</cloudHubDeployment>
	</configuration>
</plugin>

Create file .travis.settings.xml at the root of your project, it will contain username and password for your anypoint platform.

Server attributes in the mule maven plugin will be id in .travis.settings.xml.

<settings>
  <servers>
    <server>
      <id>anypoint-platform</id>
      <username>${env.username}</username>
      <password>${env.password}</password>
    </server>
  </servers>
</settings>

${env.username} and ${env.password} will be read from Travis CI environment variables. This username and password will be AnyPoint Platform username and password.

Declaring Environment Variables at Travis CI

For declaring the variable, you can activate the repository and click on settings.

It will navigate to the build screen. Now click on More Options ⇒ Settings.

It will navigate to the screen where you can declare environment variables.

In the above screen, all environment variables have been declared as required by Mule maven plugin such as workers, workerType, Application Name etc and also username and password required by .travis.settings.xml

Below is the complete .travis.yml, which will perform the automatic deployment to MuleSoft CloudHub, whenever code is pushed to dev branch or any pull request created to dev branch.

language: java
jdk:
 - openjdk8

cache:
  directories:
   - "$HOME/.cache"

jobs:
    - stage: deploy
      name: 'Deploy To Dev Environment'
      script:
       - mvn clean install
       - cp .travis.settings.xml $HOME/.m2/settings.xml
       - mvn deploy -DmuleDeploy -Dserver=$server -Dworkers=$workers -DworkerType=$workerType  -Denvironment=$environment -DmuleVersion=$muleVersion -DappName=$appName
      if: branch = dev

For accessing environment variables, syntax is $variableName.

Travis-CI provided the option to perform trigger manual build.

 It also provided an option to see running build logs, build history, branches.

Configuring Travis CI for Deploying MuleSoft Application – Part I | Github | Travis CI | MuleSoft

https://youtu.be/FPbrZ0fniSA

Configuring Travis CI for Deploying MuleSoft Application – Part II | Postman | Newman | MuleSoft

https://youtu.be/TwEF7uMrALc

These above 2 videos will give complete information on how you can use Travis CI to perform automated deployments and testing using newman and postman for MuleSoft Applications.

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.