Android continuous integration with Jenkins and Docker

Source: Internet
Author: User
Tags ming docker ps docker hub docker run jenkins docker hockeyapp

About Docker

What Docker, why do we toss Docker instead of installing the various environments needed to run appilication directly on the server?

The problem arises in the same way as why a virtual machine is needed, and I need to have my code run on a clean, non-intrusive machine that ensures consistency in the code test run environment and reduces the impact of other agnostic factors on the code. Docker and VM virtual machines have their own characteristics, such as fast startup speed, you can save Docker as a mirror, upload can be used for others. You can even put your code directly in Docker and deliver it directly to your customers. The customer only needs to install this Docker and can use the application directly after running.

Develop, ship and Run any application, Anywhere

Docker is a platform for developers and sysadmins to develop, ship, and run applications. Docker lets quickly assemble applications from components and eliminates the friction so can come when shipping code . Docker lets you get your code tested and deployed into production as fast as possible.

Provision

My goal is to build a CI server that can run Android. For money-saving purposes, I use the local vagrant to start an Ubuntu virtual machine instead of a real server. The general idea is:

    1. Launch the Ubuntu virtual machine and install Docker on Ubuntu;
    2. Install the official Jenkins Docker image via Docker;
    3. Configure Jenkins in this instance docker and install the Android operating environment;
    4. Build Android CI on Jenkins, allowing Android unit test and integration test to pass smoothly;
    5. Upload this Jenkins image with the Android CI feature;
    6. Local boot B virtual machine, download image, Access B virtual machine address, view Android Jenkins;
1. Launch the Ubuntu virtual machine and install Docker Jenkins

Before installing Docker, you have to have a Linux machine to enter, and if you already have a clean Linux server, go directly to the install Docker step.

Before installing vagrant, you should install the VirtualBox virtual machine on your PC. Please refer to Vagrant Installation Guide after the VirtualBox installation is complete.

Vagrant successfully installed, vagrant init ubuntu/trusty64 can help you quickly install a Ubuntu14.04 virtual machine, the first installation will be slightly slower, because vagrant will help you generate vagrantfile files, and download the image.

Do not start immediately after the

Vagrantfile is generated because we want to access vagrant through localhost and then access the Docker through vagrant. Since Jenkins uses port 8080 by default, if the vagrant 8080 port can be forwarded to Jenkins in Docker container. The localhost:8080 can be forwarded to the Vagrant Ubuntu 8080 Port, and then the vagrant 8080 Port continues forwarding to the 8080 port of Docker, so it can be passed localhost:8080 Visited Jenkins. So the Vagrantfile section is configured as follows to add port forwarding. Because my 8080 port is occupied by other programs, I forward the local 8088 port to the 8080 port of the virtual machine:

 # Create a forwarded port  Mapping which allows access  to  a specific port  # within the machine from a port  on  the host machine. in  The example below, # accessing  "localhost:8080"  would access  port   80  on  the guest machine. #config. Vm.network:forwarded_port, Guest: 80 , Host:  8080  Config.vm.network:forwarded_port, guest: 8080 , Host: 8088   

If you have previously started vagrant up and then changed vagrantfile to make the forward_port effective, you need to vagrant suspend pause vagrant First and then use vagrant reload reload Vagrantfile to make the configuration effective.

Vagrant after Ubuntu initialization is complete, you vagrant up can start the Ubuntu server directly under this Vagrantfile folder.

If it goes well we can go into vagrant ssh Ubuntu and then need to install Docker on this clean Ubuntu machine:

$ sudo apt-get update$ sudo apt-get install Docker. IO$ source/etc/bash_completion. D/docker. IOThe above method installs the Docker version as1.0. 1If you want to use a higher version of Docker1. 3. 3$ echo Deb Http://get. Docker. com/ubuntu Docker main >/etc/apt/sources. List. D/docker. List$ apt-key adv--keyserver PGP. MIT. edu--recv-keys $a1d7869245c8950f966e92d8576a8ba88d21e9$ apt-get update$ apt-get Install-ylxc-docker-1.3. 3

Note: The vagrant destroy Ubuntu machine will be turned off and reformatted, if you just want to simply stop the machine, please usevagrant suspend

Once Docker has successfully installed, start the official Jenkins image directly:

---p8080:8080-v /var/jenkins_home jenkins

After the installation is complete, access http://localhost:8088/ , if you can see the Jenkins interface, stating that Jenkins has been installed successfully.

After entry found no login or registration portal, and then saw the solution on StackOverflow: System Management-Configure Global Security-enable security-jenkins proprietary user database, allow users to register-any user can do anything. Register your frequently used usernames and mailboxes, and then log in to Jenkins directly.

2. Install Android operating environment in Jenkins Docker

Since our Jenkins has been built, this step we need to install the Android environment based on Jenkins, how to install the Android operating environment through the command line in Ubuntu system, the main reference this article.

The main processes for installing the Android operating environment are:

    1. Download the Android SDK installation package and set the environment variables.
    2. Download Platform-tools, Android SDK API, Android Build tools.

I install the Android SDK API version here for the 21,build Tools version number 21.1.2.

In order for the Jenkins Android environment to be able to be used next time, we create a new image based on Jenkins by creating a new Dockerfile, with the following steps:

    1. A Docker hub registered account for storing its own repository.
    2. Create a new Dockerfile file and add configuration information.
    3. Run Dockerfile, which executes the series of commands configured in Dockerfile.
    4. Commits the jenkins-android image generated by the dockerfile.
Configure Dockerfile
From Jenkinsmaintainer Ming Gong, [email protected].Comuser Rootrun Apt-getUpdate&&Apt-get - yInstall LIBSTDC++6LIB32Z1 LIB32STDC++6Expectrun wget--Progress=Dot:giga http://dl.google.com/android/android-sdk_r24.1.2-linux.tgzRUN MV Android-sdk_r24. 1. 2-linux.Tgz/opt/RUN cd/opt&&Tar XZVF./android-sdk_r24. 1. 2-linux.Tgzenv android_home/opt/android-SDK-linux/ENV PATH$ANDROID _home/tools:$ANDROID _home/platform-tools:$PATHRUN Echo$PATHRUN Echo"Y" |Android Update SDK- u --Filter platform-toolsAndroid- +RUN Echo"Y" |Android Update SDK- u -- All --Filter5RUN chmod- R 755 $ANDROID _homeRUN Apt-getInstall- yGit-coreRUN Android Update SDK--No-ui

It is android update sdk -u --all --filter 5 mainly used to update build-tools. Run ' Android list Sdk–all ' on a machine with an Android environment to see all the SDK information, find the latest version of Build-tools, and then pass the latest version number available.

After the Dockerfile configuration succeeds, we first inside the vagrant virtual machine, mkdir /var/jenkins_home and give all permissions chmod 777 /var/jenkins_home . /var/jenkins_home is able to map to the Jenkins_home directory in Docker, allowing us to back up Jenkins data directly.

Build Dockerfile

After the preparation is complete, build Dockerfile, first switch to the current directory where Dockerfile resides,

[email protected]sudo docker build -t gongmingqm10/jenkins-android:latest ....Successfully built 184c7dad595a
Push Image

The last output is the new image ID that we generated through dockerfile. After the build is complete we need to commit and push the image onto the repository of the Docker hub.

vagrant@ubuntu-14:~/test$ sudo docker imagesREPOSITORY                     TAG                   IMAGEID            CREATED             VIRTUALSIZEgongmingqm10/jenkins-android       latest                184c7dad595a        11 minutes ago      188.3MB

With Docker images you can see that the latest image has been generated, this step we need to start this image first, then get the container ID, and then push the container.

Start Mirroring First:

Vagrant@ubuntu- -: ~/test$ sudo docker run-t-i-d gongmingqm10/jenkins-android:Latest...vagrant@ubuntu- -: ~/test$ sudo docker PSCONTAINER ID        IMAGE                              COMMAND                CREATED             STATUS              PORTS                               NAMES +FCCE2388FC GONGMINGQM10/jenkins-adnroid:Latest/bin/bash -Seconds ago up  -Seconds determined_lumierevagrant@ubuntu- -: ~/test$ sudo docker commit-m"Add Android Config"-A"Ming Gong"  +FCCE2388FC GONGMINGQM10/jenkins-android:Latest theF1b93aedc4b57426b75c96ce69170016b3c0b0b6283cda5562f8464bbfd2f4vagrant@ubuntu- -: ~/test$ sudo docker push GONGMINGQM10/jenkins-android

For the first push, you may be prompted to enter your own Docker hub user name and password.

Run Jenkins-android
---p8080:8080-v /var/jenkins_home:/var/jenkins_home gongmingqm10/jenkins-android

We use Jenkinsandroid as the name of container and note that the name cannot be duplicated with the container name that was used. After running once, if you have stopped and found that you did not start, you can use to sudo docker ps -a view all the container, and then find the jenkinsandroid name of the container corresponding ID. Start:

sudo docker start f6b88bdad68f
Visit Jenkins

In the first part, we used the official Jenkins repository to launch Jenkins for a visit, and after that we built our own dockerfile, adding some Android operating environments to give Android Test provides the necessary operating environment. Access localhost:8088 can access this Jenkins with an Android environment. Register the user and log in, and this section is complete.

4. Configure Android Build in Jenkins

By visiting localhost:8088 , we can enter the Jenkins console. In order for Jenkins to be able to run Android's unit and functional tests, the main installation is as follows:

git plugin: Clone code from git repo

android emulator plugin: When Android runs a functional test, it can help us build or launch an Android virtual machine

build monitor view: Full screen display of the current build, suitable for projection onto a large screen for everyone to know the build situation in real time

During the construction of the functional test, I wanted to launch the AVD that was created in container directly from the Android emulator Pligin. There are always errors during operation:

$/opt/android-sdk-linux/tools/emulator-no-boot-anim-ports 9731,9732-AVD nexus_5_api_21-no-snapshot-Load-No-snapshot-save-No-windowemulator:error:could not LoadOpengles emulation library:libX11.so. 6: CannotOpenShared Object File:NoSuch fileorDirectoryemulator:WARNING:Could notInitialize Opengles emulation,usingSoftware renderer.emulator:warning:opening AudioOutputfailed$/opt/android-sdk-linux/platform-tools/adbConnectlocalhost9732Connected tolocalhost9732[Android] Waiting forEmulator toFinish booting...$/opt/android-sdk-linux/platform-tools/adb-s localhost:9732Shell Getprop Init.svc.bootanimerror:device offline$/opt/android-sdk-linux/platform-tools/adbConnectlocalhost9732$/opt/android-sdk-linux/platform-tools/adb-s localhost:9732Shell Getprop Init.svc.bootanimerror:device offline$/opt/android-sdk-linux/platform-tools/adbConnectlocalhost9732$/opt/android-sdk-linux/platform-tools/adb-s localhost:9732Shell Getprop Init.svc.bootanimerror:device offline$/opt/android-sdk-linux/platform-tools/adbDisconnectlocalhost9732$/opt/android-sdk-linux/platform-tools/adbConnectlocalhost9732$/opt/android-sdk-linux/platform-tools/adb-s localhost:9732Shell Getprop Init.svc.bootanimerror:device offline$/opt/android-sdk-linux/platform-tools/adbConnectlocalhost9732$/opt/android-sdk-linux/platform-tools/adb-s localhost:9732Shell Getprop Init.svc.bootanimerror:device offline$/opt/android-sdk-linux/platform-tools/adbConnectlocalhost9732$/opt/android-sdk-linux/platform-tools/adb-s localhost:9732Shell Getprop Init.svc.bootanimerror:device offline$/opt/android-sdk-linux/platform-tools/adbDisconnectlocalhost9732$/opt/android-sdk-linux/platform-tools/adbConnectlocalhost9732[Android] Emulator was shut downbeforeIt finished booting$/opt/android-sdk-linux/platform-tools/adbDisconnectlocalhost9732[Android] Stopping Android emulator$/opt/android-sdk-linux/platform-tools/adb kill-serverfinished:not_built

Online check, mainly 64-bit machine running 32-bit emulator caused by some problems. If you can manually start the virtual machine on a machine with a graphical interface, this problem should not exist when running functional test. Skip this question here, there will be a special article to study the problem.

In Jenkins, the build was built primarily for the Android Unit test-android functional test-android Deploy hocky App.

The Android unit test and functional testing basically ensure that the app is in a normal state, while the deploy to hockey app is for continuous integration needs, we want to ensure that our app can be generated in real time, so that our products can be tested and displayed to the customer at any time, or publish a stable version of the hockey App directly.

To get Jenkins to upload the app directly to Hockeyapp, we only need to install it in Jenkins and Hockey App Plugin then provide the API token and simply configure it by registering the hockey app. It is possible to upload and publish our app directly via Jenkins.

5. Android Flavor

Android flavor is for Android to use different resources when building, similar to the Ruby runtime can set different environments.

In App/build.gradle do the following configuration:

android {    defaultConfig{...}    productFlavors {        dev {            "com.tarcle.moment.dev"        }        qa {            "com.tarcle.moment.qa"        }        production {            "com.tarcle.moment"        }    }    signingConfigs {        release {...}    }    buildTypes {        release {...}    }    packageOptions {...}}

With the above configuration, we have added three environment variables to the app, so you can create a new folder directly underneath the app directory dev, qa, production . The configuration of what environment can be used in the current project by selecting a different build variables in the IDE.

Through ./gradlew clean build 任务 , you can app/build/outputs/apk generate debug and release packages in different environments under the same directory. The release package can be used to upload Jenkins to the hockey app.

The following plugins may be used to configure the Jenkins build and hockey apps. For different flavor, we can add build Parameters to the configuration of the deploy HOCKEYAPP, we can choose different types of packages when we build. To publish versions in different environments.

Copy Artifact PluginCopy the generated APK file package directly from the other build.

Android Lint PluginBuild post Lint report.

6. Common Records

Here are some of the common commands we might use for Docker and Android:

android list avdView all current devices.

android create avd -f -a -s 1080x1920 -n Nexus_5_API_21 -t android-21Create a emulator named Nexus_5_api_21. API-21.

emulator -avd Nexus_5_API_21Run the emulator named Nexus_5_api_21 that you have created, and note that the run-time command line is always blocked, so you can add it to the background to run at the end & .

adb devicesList of devices attached.

sudo docker exec -i -t a45953b9f2fe bashEnter the console of the running container

Android continuous integration with Jenkins and Docker

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.