A step-by-step tutorial for continuous integration with Jenkins on a Red Hat Mobile Native ...

Datetime:2016-08-23 02:29:28          Topic: Continuous Integration           Share

Inpart one of our series, we established the process around creating an Android application using Red Hat’s Mobile Application Platform and adding unit tests. Now, we’ll walk through how to use Jenkins for continuous integration on this app.

Pre-requisites for this tutorial

  1. RHMAP Instances
  2. Jenkins installed on Fedora
  3. Android Studio setup on a development machine.

The following are the topics covered in today’s post:

  1. Setup Jenkins to run Android Unit Test
  2. Install Jenkins plugins
  3. Install Android SDK and Tools
  4. Setup SSH on RHMAP and Jenkins
  5. Create an Android Emulator from command line
  6. Create a Jenkins project to run Android Unit Tests

Setup Jenkins to run Android Unit Test

This tutorial assumes Jenkins and git tools are installed on a Fedora machine.

Install Jenkins plugins

We need to install all the plugins needed to run Android build. From the Jenkins portal go to Manage Jenkins → Manage plugins→ Available and select the following

  1. Android Emulator Plugin
  2. Git plugin
  3. Gradle Plugin

Click Install without restart.

Install Android SDK and Tools

The first step is to download and install the android sdk and tools. Follow these steps:

  1. cd /opt
  2. wget http://dl.google.com/android/android-sdk_r24.4.1-linux.tgz
  3. tar zxvf android-sdk_r24.4.1-linux.tgz
  4. rm android-sdk_r24.4.1-linux.tgz

Now that the android sdk installed, we need to setup the environment variables to point to the sdk. In a terminal either enter

  1. export ANDROID_HOME="/opt/android-sdk-linux"
  2. export PATH="$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$PATH"

Or depending on your terminal shell type you can edit /etc/profile.d/android.sh and add in the above commands.

Next we need to run commands to install different android tools:

  1. Update the android sdk and accept agreements
    1. android update sdk --no-ui
  2. Install the Android SDK build-tools
    1. Find the item number
      1. android list sdk --all
      2. Look for a matching version xx.xx.xx to your build.gradle file
    2. Install the right version build tools
      1. android update sdk -u --all --filter <#item number >
  3. Repeat the same process to install the correct Android SDK, by matching the SDK number in gradle.build in compileSdkVersion
  4. Give Jenkins access to the sdk
    1. sudo chmod -R 755 /opt/android-sdk-linux
  5. We also need to setup Jenkins to point to the sdk. From the jenkins web portal Manage Jenkins→ Configure system→ Android sdk root→ type in /opt/android-sdk-linux and select “Automatically install Android components when required”
  6. Add a specific gradle version to Jenkins configuration
    1. Gradle plugin in Jenkins is not enough, you must also go to:
    2. Jenkins→ Manage Jenkins→ Configure Jenkins→ Configure system.
    3. Under “Gradle Installations” type in name (it appears as version in project configuration), check “Install automatically” and select version. Then you can select that “Gradle version” in project configuration.
  7. If your fedora machine is running on a 64 bit machine you need to install 32bit libraries
    1. yum install libstdc++.i686
    2. yum install zlib.i686
  8. Install JDK and set JAVA_HOME
    1. yum install java-1.8.0-openjdk-devel
    2. export JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.91-0.b14.el7_2.x86_64/

Setup SSH on RHMAP and Jenkins

For Jenkins to be able to perform a git clone of our RHMAP application we need to create and upload the SSH public key to RHMAP. In a terminal on your Fedora machine generate a new key using ssh-keygen and when asked enter /var/lib/jenkins/.ssh/<anyname> as the location of the keys. Now that the keys are generated we need to make sure they are owned by Jenkins and not the user we are logged into Fedora. Run chown Jenkins

/var/lib/jenkins/.ssh/<anyname>.*

Now that we have keys generated, log into RHMAP instance and go to admin→ users →Create and create a new user ID = jenkins with a valid password an email address. Give user Jenkins the right access to read any projects needed

Then logout and login as “Jenkins” user and upload the public key in Settings→ SSH Key Management→ Add New Key. At this point we have SSH git access setup on RHMAP portal but we still need to create an SSH profile in Jenkins. From the Jenkins portal go to Jenkins→ Credentials→ Global Credentials→ Add Credential→ Fill in the form as seen below

Create an Android Emulator from command line

On your fedora machine we need to create an android emulator that can be used by Jenkins. In a terminal run

  1. List the available targets
    1. android list target
  2. Create an avd for one of the targets, this command choses id=17 for abi armeabi-v7a
    1. android create avd -n android-23-emulator -t 17 --abi default/armeabi-v7a

Create a Jenkins project to run Android Unit Tests

Time to create a new Jenkins project that can build and run Android tests. From the Jenkins portal click New Item→ Freestyle project and fill in the fields as shown below

  1. Set Git to use jenkins credentials
  2. Use Gradle Wrapper with Tasks clean, assemble, test, connectedAndroidTest
  3. Install APK file
  4. Archive artifacts
  5. Publish JUnit test result reports for both Android unit test and Instrumented unit tests

Save configuration and Build Now. If the build succeeds from the Status page you should be able to see your artifacts and test results.

JoinRed Hat Developers, a developer program for you to learn, share, and code faster – and get access to Red Hat software for your development.  The developer program and software are both free!




About List