Run your Android CI in Google Cloud Build

Martin Bonnin
Aug 28, 2018 · 4 min read
Setting up docker containers is not as hard as you may think!

1. Fork and clone the tutorial repo

git clone https://github.com/martinbonnin/android-cloud-build-sample
git clone https://github.com/dailymotion/android-cloud-build-sample

2. Create a new Google Cloud project and enable Cloud Build API

3. Create your docker cloud-builder image and upload it to the Google container registry

# If you haven't already, install gcloud from these instructions: 
# https://cloud.google.com/sdk/install
cd android-cloud-build-sample/cloud-builder
gcloud auth login
gcloud config set project cloudbuildsample
gcloud builds submit --config cloudbuild.yaml
# You now have a docker image with gradle and the android SDK
# uploaded to the google container registry
FROM gcr.io/cloud-builders/javac

# Install Dependencies
RUN apt-get update \
&& apt-get install -y wget zip unzip \
&& mkdir -p /opt/android-sdk-linux

ENV ANDROID_HOME /opt/android-sdk-linux

# Download Android SDK tools
RUN wget -q "https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip" -O sdk-tools.zip \
&& unzip -q -d $ANDROID_HOME sdk-tools.zip \
&& rm sdk-tools.zip

ENV PATH $PATH:$ANDROID_HOME/tools/bin

# Install Android SDK components
RUN echo y | sdkmanager --install 'platforms;android-27' "build-tools;27.0.3" "platform-tools"

4. Speedup: cache your .gradle files !

# Install the wrapper script
COPY gradle-build /bin/
# unzip command might fail the first time if cache.zip does not 
# exist. That's okay
unzip -o -q cache.zip
./gradlew $@
status=$?
zip -qr cache.zip .gradle
echo "Exit status is: $status"
exit $status

5. Create the github triggers

steps:
# get the cache from cloud storage
- name: gcr.io/cloud-builders/gsutil
args: ['cp', 'gs://gradle_cache_$PROJECT_ID/cache.zip', 'cache.zip']
# build the app
- name: 'gcr.io/$PROJECT_ID/android-builder'
entrypoint: 'gradle-build'
args: ['-g', '.gradle', 'assemble']
# push the cache to cloud storage
- name: gcr.io/cloud-builders/gsutil
args: ['cp', 'cache.zip', 'gs://gradle_cache_$PROJECT_ID/cache.zip']

6. That’s it, you’re done !

Yay ! All Green !

Wrap up

Pros

Cons

Dailymotion

The home for videos that matter

Thanks to Klemen Sever, Julie Lambert, and Anne-So.

Martin Bonnin

Written by

Dailymotion

The home for videos that matter