My first step into the CI world with AWS, Jenkins and Android - Tanin's blog

Tanin's blog

App Development | Productivity

My first step into the CI world with AWS, Jenkins and Android

Posted at — Aug 8, 2019

alt text

A great butler can help you deal with lots of mundane tasks so that you have more time to do what you love and save the world.

So inspired by Batman’s Alfred, I found Jenkins. My goal of getting a butler is simple, to have a computer somewhere running unit tests for my app every time I push changes and report the results to me somehow (like via email).

Background (you may skip this ramble part)

Finding time to work on your side project app is one hurdle. Coping with platform updates is another big one. So I thought I needed help. I needed to up my game and speed up my development process. Then, I heard of CI/CD, Jenkins, DevOps, the idea of having your release process automated with some help from a tool like Jenkins. Finding out what they are wasn’t difficult. But getting one to work for my projects took me much longer than I expected. There are lots of great articles and tutorials out there, but I believe everyone’s journey is different. So I thought it would still be helpful anyway to share my story with others who are thinking about getting a butler to work for them.

Let’s get started

Table of Contents

  1. Creating an AWS ec2 server for Jenkins
  2. Installing tools: Git, Unzip, and android-sdk
  3. Configuring Jenkins
     - set up Environment variables for android-sdk
     -  set up email notification to my gmail account
  4. Generate SSH Key from Jenkins to connect with GitHub
  5. Create the first Jenkins item
  6. A few more setups
  7. Test building a Jenkins item
  8. Set up a GitHub webhook to trigger Jenkins to build when a change is pushed

1. Creating an AWS ec2 server for Jenkins

There are a few steps here but this AWS doc is fairly straight forward. Here are some notes on how I did it

2. Installing tools: Git, Unzip, and android-sdk

In ec2 shell

3. Configure Jenkins

In Jenkins home page, go to Manage Jenkins > Configure System

4. Generate SSH Key from Jenkins to connect with GitHub

In ec2 shell, go to var/lib/jenkins

  1. Logging into jenkins shell sudo su -s /bin/bash jenkins
  2. Generate dsa key pair: ssh-keygen -t dsa, generated key will be saved in .ssh
  3. Go to .ssh
  4. Do cat id_dsa.pub to see the content of the key and copy everything.
  5. Go to your GitHub account and create a SSH key (https://github.com/settings/ssh/new)

5. Create your first Jenkins item

In Jenkins home page, go to New Item

  1. Give an item some name
  2. Choose Freestyle project, then click ok
  3. In Source Code Management, select Git
    1. Repository URL : an ssh link to a GitHub project (the same link you use to clone a project with git clone <link>)
    2. Copy the warning git command e.g. git ls-remote -h git@github.com:landtanin/habit-tracking.git HEAD
    3. Go back to jenkins shell
    4. Paste the git command and run it in jenkins shell
    5. Re-paste your ssh link in Repository URL to make it update and make the error go away
    6. Set Branches to build to ** if you want it to be triggered by pushes from any branch
  4. Ignore Build Triggers and Build Environment for now
  5. In Build section, add Invoke Gradle script
    1. select Use Gradle Wrapper
    2. tasks : This can be any Gradle task (e.g. tasks, build, clean)
    3. I tried tasks first to only test the build environment (leave your GitHub project untouched)
  6. in Post-build Actions, add E-mail Notification to get notified whenever a build fails
    • Put in your Gmail address

6. A few more setups

Before actually running your first Jenkins item, there three more things we need to do (If you don’t, you’ll be prompted with loads of ambiguous errors which I have gone through, you’re welcome.)

  1. In Jenkins shell, create a file: var/lib/jenkins/.android/repositories.cfg
  2. exit from jenkins shell back to ec2 shell, make android-sdk in var/lib/jenkins folder writeable: sudo chmod 777 android-sdk
  3. Install JDK to be able to build a Java project (your android app) sudo yum install java-1.8.0-openjdk-devel

7. Test building a Jenkins item

Here comes the exciting part. In Jenkins home page

  1. Click on your item name to go into its own page
  2. Click Build Now on the left pane
  3. A new history item will pop up in the Build History pane on the lower left pane
    • You can see a console output by clicking on the history item
    • Then, click on Console Output on the left pane

If we have done it right, it shall passes.

8. Set up a GitHub webhook to trigger Jenkins to build when a change is pushed

  1. Grab a webhook url of your ec2

    1. In Jenkins home page, go to Configure System
    2. go to GitHub section
    3. click on ? after the GitHub Servers option to see the github webhook url
    4. the url will look like http://ec2..../github-webhook/
  2. Configure a GitHub project

    1. On your GitHub repo, go to Settings tab
    2. Select Add webhook
    3. Paste the webhook url in Payload URL (make sure to include the last /)
    4. Select application/json in Content type
    5. Leave Secret blank (unless a secret has been created and configured in the Jenkins Configure System -> GitHub plugin section)
    6. Select Let me select individual events and enable
    • Pushes event
    • Pull requests event
    1. Scroll to bottom, make sure Active is checked
    2. Click Add webhook
  3. Configure Build Triggers of the Jenkins task

    1. Go to the created Jenkins task
    2. Select GitHub hook trigger for GITScm polling

That’s it for now. If you have done everything right, the task will be triggered to run all unit tests of your app every time you push or make a pull request.

Next Step

Thanks for reading and happy coding 🎉

Refs