Deploying CI server on Linux
Tags: jenkins, ci, tutorial
Published: 2019-09-05 13:50:30

---
title: Deploying CI server on Linux
published: true
description: Jenkins CI server tutorial for developing Android apps
tags: Jenkins, CI, Tutorial
---
Original article [Deploying CI server on Linux](https://indeema.com/blog/deploying-ci-server-on-linux--personal-android-test-machine)

Let's see how to use and deploy Jenkins CI flow for Android apps. Specifically, setting up automated **unit testing** and building projects step-by-step.

Diving into development for Android OS, you'll probably hear about a thing called unit testing. In fact, development for any platform has a thing for unit testing. And that's because of a reason.

**README**: All examples are on Android Studio, Android SDK and Linux (so if you're not into these, some examples may be hard to follow. Or not ;) We also covered [CI server on Mac OS for iOS using GitLab and Fastlane](https://indeema.com/blog/ci-server-on-mac-os-for-ios-using-gitlab-and-fastlane) for iOS devs.

Why care about unit testing?
----------------------------

Unit testing is definitely a great thing to introduce to your project, because you can make sure that:

-   New modules don't affect the existing unit-tested parts of the project.
-   A specific code part works as we expect it to.

In case they don't, failed tests immediately show you where the problem is.

Unit tests are executed on a virtual Java machine, and of course you can run them directly in Android Studio. However, if your team is more than just you, using a continuous integration tool that automatically launches unit tests and generates informative report is a better idea.

You can then export this file as .apk and share via email with your QAs, for example. And here's a solution for such purposes: Jenkins CI server <https://jenkins.io/>

How to install for Jenkins  CI server
-------------------------------------

So, to install Jenkins:

1.  Download the latest stable version at <http://mirrors.jenkins.io/war-stable/latest/jenkins.war>
2.  Open up a terminal and go to the download folder (or a folder to where you moved the jenkins.var file) with a command:\
    `$ cd home/anyfolder/Downloads/`
3.  Run a command:\
    `$ java -jar jenkins.war --httpPort=8080`

This will launch a script that'll create a hidden **.****Jenkins** subfolder in the home folder. All server configurations will be stored in that subfolder. The server will run as long as the terminal with script is open.

To stop the server, either close the terminal window, or press **Ctrl + C.**

![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/1.png)

Setting Jenkins server up
-------------------------

After you run the script, the building of a local Jenkins server starts (using 8080 port).

On the **Getting Started** screen at <http://localhost:8080/>, you'll need to enter  

Initial Administrator Password. For this purpose, copy the provided path to your clipboard.

![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/2.png)

Therefore, to locate this password:

1.  Launch a separate terminal instance and paste that path. For example:\
    `$ cd /home/ruslanstosyk/.jenkins/secrets`\
    This will open the **secrets** folder.
2.  Use the **cat** command that allows reading content of a specific file. In this case, run **cat** command with **initialAdminPassword** filename:\
    `$ cat initialAdminPassword`\
    This will display your password on the console.
3.  Copy and paste the password to the **Administrator** **Password** field and then click **Continue**.
4.  At the next step, select **Install suggested plugins**.\
    ![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/3.png)\
    After you familiarise with Jenkins or require some specific tools only, you may use **Select plugins to install**. However, at first and for basic needs, **Install suggested plugins** option is better.
5.  Wait a few minutes while the installation is in progress.\
    ![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/4.png) 
6.  Create a user for signing into the system and configuring the server and click **Save and Continue.**\
    ![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/5.png)

7.  Sign in to Jenkins using your username and password.\
    ![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/6.png)

Configuring Jenkins integration for your needs
----------------------------------------------

So, after you're all set up with the server inself, you'll need to arm it with plugins that bring most benefits of continuous integration for building Android apps. First, from the start screen, select **Manage Jenkins.**

![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/7.png)

From the menu that appears, select **Manage Plugins** to go to the plugin management page. Here you can add, update, and select plugins you need. We'll add plugins to customize the Jenkins deployment and  adjust it to the required types of tasks. 

![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/8.png)

Go to the **available** inset and find and download the following plugins (or use the links):

-   JUnit\
    <https://wiki.jenkins.io/display/JENKINS/JUnit+Plugin>
-   JaCoCo\
    <https://wiki.jenkins.io/display/JENKINS/JaCoCo+Plugin>
-   EnvInject\
    <https://wiki.jenkins.io/display/JENKINS/EnvInject+Plugin>
-   GitLab Plugin\
    <https://wiki.jenkins.io/display/JENKINS/GitLab+Plugin>

![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/9.png)

After you install all plugins you need and to properly launch them, restart Jenkins tool.

Set up the path to folders that contain Java and Android SDK:

1.  From the start page, to to **Manage Jenkins**, and select **Configure System** option.
2.  Scroll down to the **Global Properties** and select the **Environment Variables** checkbox.
3.  In the **Name** field, paste **JAVA_HOME** and specify the path to you Java folder in the **Value** field.
4.  Click **Add**.
5.  In the name-value pair that appears, paste **ANDROID_HOME** as **Name** and specify the path to your Android folder in the **Value** field.
6.  Click **Save**.

![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/10.png)

Creating a Jenkins CI project for Android
-----------------------------------------

So, finally, we're all set up for creating a new project.

### Bind Jenkins to your project on Git

1.  On the start page click **New Item**.\
    ![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/11.png)

2.  Assign a name for your project. This name must not contain spaces.\
    For example, *testproject, test-project, Test_Project.*\
    ![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/12.png)

3.  Select **Freestyle project** option.\
    This will launch the project configuration page.

4.  Go to **Source Code Management****.**

5.  Select Git checkbox and paste full path to your project.\
    ![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/13.png)

6.  Enter your credentials to let Jenkins clone and launch your project.\
    ![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/14.png)

### Add rules for building the project

Next tab we'll need for Jenkins continuous deployment is **Build Triggers**. Here we can add rules for launching the project's build. There's a variety of options we can set up for starting the build. For example, detecting a specific keyphrase in a commit name such as [ci start] or any other you set up. Yet, to make use of this functionality, you'll have to deploy Jenkins CI on a web server (instead of a local one), because it's implemented using web hooks.

In our case, we used an option that starts build every 15 minutes. This option uses cron  syntax.

![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/15.png)

In the **Build Environment** tab, select **Abort** the build if it's stuck checkbox. This option is quite handy for cases when an unexpected error occurs and you need to free queue and deallocate memory.

![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/16.png)

So, in the **Build** tab, you have to specify the options to run:

1.  From the drop-down menu, select the **Invoke Gradle Script** option.

2.  In the **Invoke Gradle Script** dialog, select **Use Gradle Wrapper** checkbox.\
    ![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/17.png)

3.  Fill the Tasks field with the script that defines the order for running the commands.\
    We used:\
    `clean`\
    `assembleDebug`\
    `test\
    connectedAndroidTest`

4.  Then configure **Post-Build Actions** by selecting the following options from the drop-down:

-   **Publish JUnit test result report**

Using this option, you'll configure Jenkins to save the detailed report on each test run. You'll also get a nice visualization of the report.

**![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/18.png)**

-   Archive the artifacts

This option builds the project after all tests are run. So you'll get an .apk  file ready to be downloaded and installed on a device.\
**![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/19.png)**

5\. Click **Save**.

To launch the project, either run an emulator, or connect some Android device to your computer and then click Build Now.

![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/20.png)

While the project is being cloned from the repo and the build is starting, you can explore the build status and info about success/failure of each stage in the **Console Output.**

### Review the build

After the project is successfully built, you'll see the following logs in the **Console Output:**\
**![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/21.png)** 

From the main page of the build, you can download the .apk file and build logs, review the information about running the tests (success and failures), and more.

![](https://indeema.com/images/articles/Deploying-Jenkins-CI-server-Personal-android-test-machine/22.png)

Started and up!
---------------

So we've made quite a lot of configurations. As a result, we've got a continuous integration using Jenkins for automating a part of build creation and launching tests when developing an Android app.

This article covers only the tip of the iceberg of what you can set up and customize via Jenkins CI for your project/requirements.

Thanks for reading and hope it helps you automate mundane tasks ;) Subscribe for more nifty stuff about continuous integration tools, [IoT](https://indeema.com/blog/internet-of-things-development--5-steps-to-creating-your-project), [Android development](https://indeema.com/blog/kotlin-arrival--the-official-start-of-a-post-java-era), and [more](https://indeema.com/blog/smart-lock--why-sloth-is-a-driver-of-the-iot-progress)!