Deploying MinMinGuard using Travis CI

On 2016/09/14 at 16:34

I just learned and applied Travis CI to do continuous integration for MinMinGuard. For now, I am just using travis to automatically build release apk. The settings for travis is fairly easy to setup by just following the official tutorial. Following is my .travis.yml:

language: android
android:
  components:
  - platform-tools
  - tools
  - build-tools-23.0.3
  - android-23
  - extra-google-google_play_services
  - extra-google-m2repository
  - extra-android-m2repository
  - addon-google_apis-google-19
jdk:
- oraclejdk8
script:
- '[ "${TRAVIS_PULL_REQUEST}" == "false" ] && ./gradlew assembleRelease || ./gradlew assembleDebug'
before_deploy:
- mv app/build/outputs/apk/app-release.apk app/build/outputs/apk/MinMinGuard.apk
deploy:
  provider: releases
  api_key:
    secure: [my secure github oauth key]
  file: app/build/outputs/apk/MinMinGuard.apk
  skip_cleanup: true
  on:
    tags: true

Because I only want travis to build the release apk for me, the script section needs to change to ./gradlew assembleRelease (The default one is ./gradlew build connectedCheck which is not what I want). The most important part is the deploy section and it can be automatically generated by using travis setup releases command (travis command line tool can be get from ruby gem). Then, whenever I push a new tag onto github, Travis CI will build the release apk and deploy it to the github release page automatically.

The only hassle is to sign the apk without revealing my keystore password. After some searching, I changed my gradle file to the following:

android {
  signingConfigs {
    release {
      keyAlias 'minminguard'
      keyPassword System.getenv("KEYSTORE_PASS")
      storeFile file('minminguard.keystore')
      storePassword System.getenv("KEY_PASS")
    }
    debug {
      storeFile file("debug.keystore")
      storePassword = "android"
      keyAlias = "androiddebugkey"
      keyPassword = "android"
    }
  }
  buildTypes {
    release {
      zipAlignEnabled true
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      signingConfig signingConfigs.release
    }
    debug {
      debuggable true
      jniDebuggable true
      signingConfig signingConfigs.debug
    }
  }
  // blablah
}

When building on the Travis CI platform, the keystore and key password will be passed through the environment variables. The environment variables setting on Travis CI can be done in the setting panel easily:

Travis environment variable setting

The last thing to do is using a different script for building pull requests. Because building upon pull requests can not access the environment varibles, I have to run ./gradlew assembleDebug to use debug keystore instead. I use the following script to differentiate the two scenarios:

[[ "${TRAVIS_PULL_REQUEST}" == "false" ]] && ./gradlew assembleRelease || ./gradlew assembleDebug

When Travis CI builds pull requests, the $TRAVIS_PULL_REQUEST variable will be set to true. I can simply check the value of $TRAVIS_PULL_REQUEST to determine which script to run.

References

Comments