Monday, July 10, 2017

Gitlab Shared Runner CI with Docker Tutorial - Complete Guide on Building Android Studio Project in Gitlab

After a long break, writing a new post on building Android Studio Gradle project on Gitlab Pipelines with Docker without hassling about server charges or build time. Gitlab Pipelines and Registry is a boon for developers who need CI for their apps and additionally, Gitlab provides private repo. Today let's look at creating a pipeline with an environment where our app is built within Docker when you commit to Gitlab repository.


Gitlab Shared Runner CI with Docker Tutorial - Build Android Studio Project in Gitlab

One of the most hassling things is building the project and moving into production phase by testing the app on several devices. The thing is maintaining a private server to build your production ready apps will cost you high like Travis CI, Circle CI. For startup companies, Gitlab shared runner is more than enough for building the application and delivering it with artifacts on regular basis.


Getting Started:

If you're familiar with Docker basics, It would be fine to continue with this tutorial. If not you can just learn about Docker basics here. Docker is an awesome environment where you virtualize your environment from the Host environment and run your specific test, build, commands. Since you virtualize your docker environment you won't be affected by external dependencies or prone to vulnerabilities while shipping your application. If you don't have Gitlab Account, signup now. It's completely free for individual developers.
  1. Create Gitlab Repo 
  2. Commit your Android Studio project in master branch

Enabling Shared Runner and Pipeline in Gitlab:

After creating the project, visit Settings -> Pipelines in your project repo and make sure Shared Runner is Enabled. You may get up to 4 Shared instance to build your project. Fortunately, Gitlab has collaborated with DigitialOcean to maintain servers for running such builds, test for users. so, you need not worry about scaling, downsizing the server size. 

Gitlab Shared Runner CI with Docker Tutorial - Complete Guide on Building Android Studio Project in Gitlab
Enable Shared Runner for this Project

Build Configuration File

In order to trigger our build, we need to write some configuration file. First, let's build our Docker image using Dockerfile. Gitlab too provides private docker registry for users, where you can build the Docker image in the pipeline and upload it to Gitlab registry. So, this will reduce our build time in halfway because installing our build tools ( Android SDK ) and other tools earlier will reduce the time and concentrate on compiling the source code into APKs.

Our Docker file looks like given below, it's just series of Linux commands where you process it and save it as Docker image. Whenever need you can deploy it as a container and make your desired process run inside the Docker container.

Prerequisites :

  • Create a new branch from existing master branch, remove all your application code. 
  • Create new file named as Dockerfile
  • Checkout docker file snippet below
  • Create new file named .gitlab-ci.yml 
  • Checkout Gitlab-ci file below, customize it as per your repository name
  • Commit all your files, this will automatically trigger a build to create new Docker Image and upload it to Docker registry.
Dockerfile for creating Android environment image is given below,




gitlab-ci.yml file for instructing the pipeline to perform the job is also given below, kindly replace with your own repository registry name, branch name or else build won't be triggered.


once you commit your code, Gitlab automatically triggers an Image Build in the pipeline. if everything goes smooth, your docker image will be uploaded to your Gitlab Docker registry. You can use the Docker image for further building the project without any hassle. Whenever you need to refresh or update your SDK tools, you can simply change and commit it, that will trigger an image building process and it'll be uploaded to your Gitlab Docker registry.

Gitlab Shared Runner CI with Docker Tutorial - Complete Guide on Building Android Studio Project in Gitlab
Building process after each commit

Build your Application:

Switch to your master branch, start writing similar gitlab-ci.yml file to instruct the build machine in order to trigger build and building the artifacts. It's going to be normal Gradle commands to build your application project and generate your APKs, AAR files and even more out of it. you can sign your application here with confidence by adding your Keystore file as Environment variable.

Let's look at our configuration file for building the application,



So, in before script, we added execution permission for gradlew executable file in user level (normally root/guest in terms of Docker). In build script, we simply added our Gradle command to build our application project. Since it's a sample I haven't added release build script here. After building process is over, you can specify your artifact folder, so that Gitlab can detect that folder and make it available for downloading. Normally it contains your project APK files, logs, Gradle profile report and even more if you have used custom tasks in Gradle.

So, commit this code and Gitlab will automatically trigger a build. Now, Docker is launched in a shared runner and then your Gradle scripts will be running to produce APK. It's up to you to have some integration with Gitlab to deliver your APK artifacts into team e-mails, slack channel, chat or even more.

Gitlab Shared Runner CI with Docker Tutorial - Complete Guide on Building Android Studio Project in Gitlab
Application building in Docker Container

Skip triggering build for each commit

you can skip the triggering build by adding an extra commit message, such as [skip ci] or [ci skip]. This will be automatically understood by Gitlab and it won't be triggering build automatically.

I hope this was an awesome tool for Developers who don't need to write the script for building, launch Build server and run, test, build your source code into release files. You don't need to worry about infrastructure, deliver and testing it seamlessly.


Gitlab Shared Runner CI with Docker Tutorial - Complete Guide on Building Android Studio Project in Gitlab
pipeline containing previous Docker Image Build and Artifact build


If you've any doubts, comment below or chat with me in Google+/Fb. Share is care.