运行 android jenkins 构建期间的模拟器

Running android emulator during jenkins build

我已经安装了 Jenkins 并在构建期间使用模拟器 运行ning 配置了一个构建作业。但是当我执行作业时,我收到错误消息,指出 jenkins 用户没有 运行 kvm 的权限。我该如何解决它。

$ android list target
[android] Using Android SDK: /opt/android/sdk
$ adb start-server
* daemon not running. starting it now on port 5973 *
* daemon started successfully *
$ adb start-server
[android] Starting Android emulator
[android] Erasing existing emulator data...
$ emulator -ports 5971,5972 -prop persist.sys.language=en -prop persist.sys.country=US -avd hudson_en-US_160_1024x768_android-15_x86 -no-snapshot-load -no-snapshot-save -wipe-data
emulator: ERROR: x86 emulation currently requires hardware acceleration!
Please ensure KVM is properly installed and usable.
CPU acceleration status: This user doesn't have permissions to use KVM (/dev/kvm).
[android] Emulator did not appear to start; giving up
$ adb disconnect localhost:5972
[android] Stopping Android emulator
$ adb kill-server

权限

$ ls -l /dev/kvm
crw-rw----+ 1 root root 10, 232 May  6 13:46 /dev/kvm

$ groups jenkins
jenkins : jenkins

我已经解决了这个问题。

  1. 安装 Qemu-KVM 和 cpu-checker:
    sudo apt install qemu-kvm cpu-checker
  2. 检查 KVM 是否可用(作为 jenkins 用户):
    $ kvm-ok
    INFO: /dev/kvm exists
    KVM acceleration can be used
  3. 创建组 kvm 并将 jenkins 添加到该组:
    addgroup kvm
    usermod -a -G kvm jenkins
  4. 更改 /dev/kvm 的群组所有权:
    chgrp kvm /dev/kvm
  5. 创建 udev 规则:
    $ sudo nano /etc/udev/rules.d/60-qemu-kvm.rules
    KERNEL=="kvm", GROUP="kvm", MODE="0660"
  6. 重启

在我的 CI (gitlab) 上 Ubuntu

usermod -a -G kvm gitlab-runner

或与詹金斯

usermod -a -G kvm jenkins