In order to deal with the amount of different projects Knowm Inc. is currently managing, both open source and commercial, we jumped at the opportunity to move all of our OSS projects over from an in-house Jenkins CI to a free hosted Travis CI service. Doing so frees us up from having to maintain our Jenkins server, deal with it hanging, updating it, etc. This is just one more step in streamlining project development and management. The only disadvantage by doing this was the loss of our Sonar code quality analytics, but I suspect something of the sort will soon become available as a plugin to Travis CI.
What we needed Travis CI to do though was a bit non-standard, and we wanted to write what we learned here for future reference, but also to help other get their Maven builds deploying snapshots to a repository. This article assumes you’ve already setup your Travis CI account and have followed the standard setup procedures and configurations linking your Travis CI and GitHub accounts and projects. Make sure to enable the Travis CI hook in GitHub too. The example configurations contain stuff relevant to our project XChart, and you’ll have to adapt your configurations to your specific project’s properties.
References
Our initial inquiry on Travis Google group
Our second inquiry on Travis Google group
Travis gem and secure variables
Knowm’s XChart project on GitHub
XChart’s Travis CI page
settings.xml
Simply add another branch to your project called travis
, remove everything from it, and place the following settings.xml
file in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<localRepository/> <interactiveMode/> <usePluginRegistry/> <offline/> <pluginGroups/> <servers> <server> <id>sonatype-nexus-snapshots</id> <username>${env.CI_DEPLOY_USERNAME}</username> <password>${env.CI_DEPLOY_PASSWORD}</password> </server> </servers> <mirrors/> <proxies/> <profiles/> <activeProfiles/> </settings> |
Setup Travis Command Line Tool
You’ll need to run some commands on a machine where ruby is installed and the travis gem is installed.
1 2 3 |
sudo gem install travis --no-rdoc --no-ri travis version |
Generate Encrypted Variables
The artifact repo requires a username and password of course, and we don’t want to have those as plain-text in the above settings.xml
file. Therefore we use the following commands to generate the encrypted keys. Obviously replace timmolter/XChart
with your own Github account name and project name. The username
and password
correspond to the login info you created during Sonatype’s Nexus repository system.
1 2 |
travis encrypt -r timmolter/XChart "CI_DEPLOY_USERNAME=username" travis encrypt -r timmolter/XChart "CI_DEPLOY_PASSWORD=password" |
.travis.yml
Place a file called .travis.yml
in the root of the project on the branch you want to be deployed. You’ll need to modify this .travis.yml
file as shown here. The whitelist specifies that we only want the build to run on the develop
branch. Replace the keys below with your generated codes from the previous step. Note that the script line prevents snapshots being deployed when an update on the develop
branch is due to a pull request. To prevent a snapshot deploy on a pull request the script checks the Travis environment variable TRAVIS_PULL_REQUEST
and if it is a pull request, it runs mvn clean verify
. Otherwise, it runs mvn clean deploy
as we want.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
language: java before_install: "git clone -b travis `git config --get remote.origin.url` target/travis" script: "[ ${TRAVIS_PULL_REQUEST} = 'false' ] && mvn clean deploy --settings target/travis/settings.xml || mvn clean verify --settings target/travis/settings.xml" # whitelist branches: only: - develop env: global: - secure: "R7i6miE4AIbj1o6E8fwWvKwjvygha172QSdKM0Jto/Qq3AyLuWMCthYAxevq\nX+I64lw3exhmEiOHXHjh1lGTnGZ9kXklIU+q4vo9QfdYH7WU47chE0KZfQtd\n64EoFo8KmSD4VQjyiNv7ReLdoqksRAj2dL386fWrBAUWkwhxD4w=" - secure: "UL4rCDPPBrk7cju9f+fNi1qfGWb+q/36wJYUEeuINDa2a4c4oHvdOX0aYOIV\nbBDeaReKMtl6mpjG2uxCZwaz7N3/VADDMrqA+wbTbWKgyWeTACzAz/Z/6w8E\noOAkS8EUCZgDiXeHuirYvQQ47KbzYmf2m4/EUAQm+VIHFbO1ssE=" |
Test It
Now that everything is setup, all you need to do is push a change on the develop
branch to GitHub, and Travis CI will build the Maven project and deploy the artifacts to a snapshot repo automatically!
Other Travis CI Tidbits
- Pushes that have
[ci skip]
anywhere in one of the commit messages will be ignored. - EDIT – this issue has now been fixed. You may need to place a
.travis.yml
file in all your project’s branches to prevent it to try to build those branches – even if your whitelist doesn’t include those branches. They are planning on changing this behavior though. - Embed a Travis CI build badge in REAME.md file:
1 |
[![Build Status](https://travis-ci.org/timmolter/XChart.png?branch=develop)](https://travis-ci.org/timmolter/XChart.png?branch=develop) |
Subscribe To Our Newsletter
Join our low volume mailing list to receive the latest news and updates from our team.