Android项目Gitlab-CI + Monkey集成(MAC系统)

序言

Gitlab-CI(GitLab Continuous Integration)Gitlab持续集成,只要在仓库根目录下配置.gitlab-ci.yml文件,同时配置gitlab-runner(.gitlab-ci.yml脚本的运行器),那么每一次push都会触发pipeline(.gitlab-ci.yml的所有任务)

gitlab-runner安装

推荐使用homebrew安装:

brew install gitlab-runner

配置gitlab上的CI/CD Pipelines

首先打开你的项目,然后打开Settings —> CI/CD Pipelines

  • 这个里面有配置gitlab-runner的一些参数;然后打开你的终端,我们需要注册一个gitlab-runner,一个runner对应一个项目:
gitlab-runner register
  • 会让你指定一个URL,这个就是CI/CD Pipelines中的第2条:
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
输入上面的URL地址,然后回车
  • 然后还会让你配置一个token,就是CI/CD Pipelines中的第3条:
Please enter the gitlab-ci token for this runner:
输入上面的token,然后回车
  • 关联git和runner的tag + 设置tag的描述(一般这个可以不用配置,直接在.gitlab-ci.yml文件中指定触发的分支即可)
Please enter the gitlab-ci tags for this runner (comma separated):
输入一个tag名,然后回车

Please enter the gitlab-ci description for this runner: 
设置tag的描述
  • 选择runner的执行环境
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
这里因为我们本地是有Android环境的,所以直接选择shell即可,如果本地没有Android环境,可以选择docker来配置Android环境
  • 启动gitlab-runner
gitlab-runner install
gitlab-runner start
  • 打开~/.gitlab-runner/config.toml配置文件,可以看到刚才的配置内容:
[[runners]]
  name = "xxx"
  url = "http://xxx.xxx.xxx/ci"
  token = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
  executor = "shell"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]

这是其中一个runner,如果你有很多个项目,那么都需要这样配置一遍,在配置文件中也有很多个这样的。

.gitlab-ci.yml文件

关于这个脚本文件的配置,可以看一下官网的文档:

官网配置文档

中文翻译文档

主要用到的我这边介绍一下:

stages:
  - build_debug

# 构建之前会执行的脚本,这里我们导入环境变量
before_script:
  - export ANDROID_HOME=~/Library/Android/sdk
  - export PATH=$PATH:~/Library/Android/sdk/platform-tools/
  - chmod +x ./gradlew

# 声明一个名叫build_debug的构建任务
build_debug:
  stage: build_debug
  # 构建中,执行一些脚本
  script:
    - ./gradlew --stacktrace assembleDebug
    - adb install -r app/build/outputs/apk/debug/*.apk
  # 指定监听哪一个分支
  only:
    - master
  # 指定成功后应附加到任务的文件和目录的列表
  artifacts:
    paths:
      - app/build/outputs/apk/debug/*.apk

# 构建完成之后执行的脚本
after_script:
  - 这里如果是要配合monkey的话,一般在这个地方执行monkey的脚本

到这里我们就集成好了,当你把推到线上master分支的时候,gitlab-runner就会执行.gitlab-ci.yml文件,然后依次执行里面的任务,在gitlab上你也可以实时的去Pipelines下查看:

490111-35c404929deaecdf.png
VYTGLj.png
490111-9e3c65e4e3ede84c.png
VYTdYV.png

上面的例子中,我是直接打了一个包,这个时候你可以直接在gitlab中下载刚才的产物,或者让测试同事去这个上面下载就可以了。

加入Monkey

monkey是Android SDK自带的一个压力测试工具,使用adb命令来运行monkey脚本,向app发送一些伪随机用户事件流,如按键,滑动,输入等操作,用monkey来对系统进行一个压力测试,更好地帮助我们找到代码中一系列的问题,推荐一个比较好用而且比较全面的第三方库:

自动化工具 fastmonkey - 代号 Maxim

fastmonkey - Github

使用该三方库需要注意的是,需要将framework.jar 和 monkey.jar 传到手机sdcard中,建议是根目录,然后如果需要使用白名单或者黑名单功能的话,需要将白黑名单也一起传到手机sdcard中。

脚本:(基于上述三方库)

adb shell CLASSPATH=/sdcard/monkey.jar:/sdcard/framework.jar exec app_process /system/bin tv.panda.test.monkey.Monkey -p  你的app包名 --running-minutes 3 --throttle 200 --act-blacklist-file /sdcard/blacklist.strings --uiautomatormix -v -v

附赠一个Android-Monkey命令详解:

Android-Monkey命令详解

转载于:https://www.jianshu.com/p/e5c683534eac

猜你喜欢

转载自blog.csdn.net/weixin_33704591/article/details/91171018