My Github: github.com/BzCoder

Github. IO /gitlab-ci-c…

Welcome to leave your comments

scenario

In order to optimize the work flow, free the hands of developers and not be disturbed by trivial things like “help me type a test package for the latest” all day long, I have researched something about Continuous integration of Android in these two days, which is hereby recorded. Our project is stored in a private repository on GitHub. Now we have three solutions for continuous integration automation packaging:

  • Travis CI: Github’s son, which is free to use for open source libraries, but charges for continuous integration with Github’s private libraries.
  • Jenkins: Powerful, flexible to use, but Jenkins configuration complexity and deployment costs are relatively high, there are learning costs, but a good solution.
  • GitLab CI: Completely free, an affiliate of GitLab.

Tool set

  • GitHub: The world’s largest code hosting platform, the world’s largest dating platform for programmers.
  • GitLab: Many features are better than GitHub, and many companies have taken it private.
  • Gitlab-ci: Continuous inheritance platform with GitLab platform.
  • Dandelion: domestic well-known APP internal test distribution platform.
  • Dingding: Office software produced by Ali, we mainly use robot function to inform relevant personnel.

The overall process

With a brief overview of the tools, let’s begin our continuous integration journey.

Synchronize code to GitLab

Start by transferring a copy of the GitHub code to GitLab and keep it in sync. Fortunately, GitLab has already taken care of the most important part. [Gitlab] [New Project] [CI/CD for external repo] [GitHub], as long as through authorization, Gitlab can get all the warehouses under your GitHub account. Select Connect to synchronize your GitHub project to GitLab.

Configuration GitLab – CI

Gitlab-ci configuration is actually very simple, we only need to add gitlab-CI continuous integration script file. Gitlab-ci. yml to the root directory of our project, if you want to use it immediately, then you just need to copy and paste the following code, replace the relevant Key values and SDK,JDK version.

image: openjdk:8-jdk

variables:
  ANDROID_COMPILE_SDK: "28"
  ANDROID_BUILD_TOOLS: "28.0.2"
  ANDROID_SDK_TOOLS:   "4333796"

before_script:
  - apt-get --quiet update --yes
  - apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1
  - wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/sdk-tools-linux-${ANDROID_SDK_TOOLS}.zip
  - unzip -d android-sdk-linux android-sdk.zip
  - echo y | android-sdk-linux/tools/bin/sdkmanager "platforms; android-${ANDROID_COMPILE_SDK}" >/dev/null
  - echo y | android-sdk-linux/tools/bin/sdkmanager "platform-tools" >/dev/null
  - echo y | android-sdk-linux/tools/bin/sdkmanager "build-tools;${ANDROID_BUILD_TOOLS}" >/dev/null
  - export ANDROID_HOME=$PWD/android-sdk-linux
  - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
  - chmod +x ./gradlew
  # temporarily disable checking for EPIPE error and use yes to accept all licenses
  - set +o pipefail
  - yes | android-sdk-linux/tools/bin/sdkmanager --licenses
  - set -o pipefail

stages:
  - build

assembleDebug:
  stage: build
  only:
    - master
  script:
    - ./gradlew assembleDebug
    - curl -F "file=@app/build/outputs/apk/debug/app-debug.apk" -F "UKey = dandelion uKey" -F "_API_key = dandelion API_key" https://qiniu-storage.pgyer.com/apiv1/app/upload
    - curl "Nail WebHook address" -XPOST -H 'content-type:application/json' -d '{"msgtype":"text","text":{"content":"@135xxxxxxx@135xxxxxxx@135xxxxxxx" https://www.pgyer.com/xxxx"},"at":{"atMobiles":["135xxxxxxx","135xxxxxxx","135xxxxxxx"],"isAtAll":false}}'
  artifacts:
    paths:
      - app/build/outputs/apk/debug/app-debug.apk
Copy the code

The configuration script

Here to explain the meaning of several commonly used configuration items, after all, our requirements are quick and simple configuration, too much will not be described.

1. Configure the JDK version and SDK version

Modify it based on the actual SDK version of the project.

image: openjdk:8-jdk

variables:
  ANDROID_COMPILE_SDK: "28"
  ANDROID_BUILD_TOOLS: "28.0.2"
  ANDROID_SDK_TOOLS:   "4333796"
Copy the code

2. Pack instructions

Change the packaging command as needed, in this case the Debug package.

./gradlew assembleDebug
Copy the code

3. Upload dandelions

The first parameter is the path to the file you packed. Do not lose the “@” sign. The second and third parameters can be found on dandelion’s official website.

curl -F "file=@app/build/outputs/apk/debug/app-debug.apk" -F "UKey = dandelion uKey"
 -F "_API_key = dandelion API_key" https://qiniu-storage.pgyer.com/apiv1/app/upload
Copy the code

Of course, you can also configure other parameters to meet your requirements.

parameter type instructions
uKey String (Mandatory) User Key
_api_key String (Mandatory) API Key
file File (Mandatory) IpA or APK file to be uploaded
installType Integer (optional) application installation mode. The value is (1,2,3). 1: public, 2: password installation, 3: invitation installation. The default is 1 public
password String (Optional) Set the App installation password. If you do not want to set the password, please pass an empty string or do not pass it.
updateDescription String (Optional) Version update description, please pass empty string, or do not pass.

4. Notification nail nail

curl "Nail WebHook address" -XPOST -H 'content-type:application/json' -d 'Pin message JSON'
Copy the code

Nail the WebHook address can be obtained in group Chat add Bot Custom.

{
  "msgtype": "text"."text": {
    "content": "@ 135 XXXXXXX @ 135 XXXXXXX @ 135 XXXXXXX" XXXX "packaging is complete, the download address: https://www.pgyer.com/xxxx"
  },
  "at": {
    "atMobiles": [
      "135xxxxxxx"."135xxxxxxx"."135xxxxxxx"]."isAtAll": false}}Copy the code
parameter The parameter types Must be instructions
msgtype String is The message type is text
content String is The message content
atMobiles Array no @person’s mobile phone number (add @person’s mobile phone number to content)
isAtAll bool no @owner: true, otherwise: false

At the end of @ is the mobile phone number bound to the pin of the colleague you need to notify. For more usage, please refer to the pin robot documentation.

Of course, you can also set the webhook in the dandelion background directly set the notification nail:

Dandelion app download address, you can view in the background.

 only:
    - master
Copy the code
parameter instructions
only Define a list of Git branches for integration
except Define a list of Git branches without integration

Upload the artifacts file path

  - app/build/outputs/apk/debug/app-debug.apk
Copy the code

You can download the printed package in Gitlab.

Matters needing attention

Once you submit your code to Github, Gitlab-CI will help you pack automatically. You can check the process of packing in [Gitlab] [corresponding Project] [CI/CD].