How to Use Travis CI with Java on GitHub

This repository is a demo of how to use Travis CI in a Java project on GitHub.

Tutorial

1. Build

  • Sign in to your user Github account and fork this repo
  • Go to Travis-ci.com and Sign in with GitHub account

avatar

  • Accept the authorization of Travis CI
  • Click on your profile picture in the top right of your Travis Dashboard, click Settings

avator

  • Select the repo you want to use with Travis CI. If repo cannot be found, click Sync account

avatar

  • Back to Github. Create a .travis.yml file and add the following content(to tell Travis CI what to do). Commit and push the changes to the root directory of your repo
# Building a Java project
language: java

# We can specify a list of JDKs to be used for testing
jdk:
 - openjdk11
  • Back to Travis CI. Click your repo and you will find it start build by Travis CI

avatar

  • If the build is passed, you will see as follows.

avatar

  • Create the badge in README.md(copy the markdown code from Travis CI and paste in the top line of readme.md)

Travis CI => select your repository => click the build status image => select FORMAT as Markdown => Copy the code of RESULT => paste in the top line of readme.md

acatar

  • You will see a badge like this in your repo of Github

acatar

2. Test

This repo also integrates with Codecov to generate reports.

avatar

  • Accept the authorization of Codecov and add your repository
  • Add the following code in the end of .travis.yml file
after_success:
  - bash <(curl -s https://codecov.io/bash)
  • Commit and push the changes to your repo, which will trigger a new build in Travis CI. This is to enable CodeCov's coverage. If a build is successful, the code is submitted for coverage analysis
  • Create the badge in README.md

Codecov =>select your repository => Setting => Bedge => Copy the contect of Markdown => paste in the second line of readme.md

  • You will see the badge like this

acatar

3.Deploy

avatar

  • In the Environment Variables section, add variable named GITHUB_TOKEN and use the generated token from Github in previous step.

avatar

deploy:
  provider: releases
  file_glob: true
  skip_cleanup: true
  api_key: $GITHUB_TOKEN
  file:
    - .travis.yml
  on:
    tags: true
    branch: tag
    repo: <your_github_username>/<your_repo_name>

  • Commit and push to trigger a Travis CI build. If the build is successful, you will see .travis.yml in the Releases section of your repo like this

avatar

4. Trigger a failed build and notify build result

  • Travis CI can notify you about your build results through email, IRC, chat or custom webhooks (https://docs.travis-ci.com/user/notifications/)
  • Add the following to your .travis.yml, which will send the build result to your Email
notifications:
 email:
   - one@example.com(replace by your e-mail)
  • Modify line 61 of trityp.java(src/main/.../trityp.java) as follows
triOut = triOut + 2;

acatar

  • Commit and push to trigger a Travis CI build. As expected, this build will failed and you will receive an Email like this

acatar