简体   繁体   English

在 Docker 中为 Android 运行并行 Gitlab CI 作业

[英]Running parallel Gitlab CI jobs for Android in Docker

I use Gitlab CI to build my Android app using fastlane inside a Docker container.我使用 Gitlab CI 在 Docker 容器内使用 fastlane 构建我的 Android 应用程序。 My app has two "flavors", which I want to build in separate CI jobs.我的应用程序有两种“风格”,我想在单独的 CI 作业中构建它们。 Here is the relevant portion of the .gitlab-ci.yml :这是.gitlab-ci.yml的相关部分:

default:
  image: registry.example.com/group/project:29-android-fastlane-debian
  tags:
    - docker
  before_script:
    - ruby -v # Print out ruby version for debugging
    - bundle install

build_flavor1_debug:
  stage: build
  script:
    - bundle exec fastlane build release:"false" --env flavor1
  artifacts:
    paths:
      - app/build/outputs/bundle/
  rules:
      - if: '$CI_COMMIT_TAG && $CI_COMMIT_REF_PROTECTED == "true"'
        when: never
      - when: on_success

build_flavor2_debug:
  stage: build
  script:
    - bundle exec fastlane build release:"false" --env flavor2
  artifacts:
    paths:
      - app/build/outputs/bundle/
  rules:
      - if: '$CI_COMMIT_TAG && $CI_COMMIT_REF_PROTECTED == "true"'
        when: never
      - when: on_success

(Other jobs in this pipeline do release builds for git tags and upload to the Play Store, but those are not relevant to this issue.) (此管道中的其他作业会发布 git 标签并上传到 Play 商店,但这些与此问题无关。)

Here are the relevant parts of the Fastfile :以下是Fastfile的相关部分:

# All flavors we know about. If one is not specified, run them all
flavors = {
  "flavor1" => "com.example.flavor1.app",
  "flavor2" => "com.example.flavor2.app"
}

# If set to a single flavor, make it the only one in the array
if ENV["FLAVOR_NAME"]
  flavors = flavors.slice(ENV["FLAVOR_NAME"])
end

UI.user_error!("Unknown flavor '#{ENV["FLAVOR_NAME"]}' selected") if flavors.empty?

platform :android do

  desc "Build the application"
  lane :build do |options|
    setup(options)

    flavors.each { |flavor_name, flavor_package|
      build_flavor(
        flavor: flavor_name,
        release: options[:release]
      )
    }
  end

end

Using the --env flag loads the .env file with the appropriate FLAVOR_NAME variable set to make only one flavor run at a time.使用--env标志加载.env文件并设置适当的FLAVOR_NAME变量以使一次仅运行一种风味。

This worked fine when I was running the builds sequentially, but that takes way too long.当我按顺序运行构建时,这工作得很好,但这需要的时间太长了。 I changed the Gitlab Runner configuration to allow running up to 8 simultaneous jobs, and now I get the following error:我更改了 Gitlab Runner 配置以允许同时运行多达 8 个作业,现在我收到以下错误:

The message received from the daemon indicates that the daemon has disappeared.
Build request sent: Build{id=111291aa-90bc-45c3-8fb4-9a271d4663f4, currentDir=/builds/group/project}
Attempting to read last messages from the daemon log...
Daemon pid: 1100
  log file: /root/.gradle/daemon/6.5/daemon-1100.out.log
----- Last  20 lines from daemon log file - daemon-1100.out.log -----

[SNIP] - No useful logs here...

----- End of the daemon log -----
FAILURE: Build failed with an exception.
* What went wrong:
Gradle build daemon disappeared unexpectedly (it may have been killed or may have crashed)

Sometimes this happens on one flavor's job and sometimes on the other.有时这发生在一种风味的工作上,有时发生在另一种风味上。 If I manually rerun the failed job, it always succeeds.如果我手动重新运行失败的作业,它总是会成功。

I believe that what is happening is that the two build jobs are running inside the same Docker container instead of each one having its own.我相信正在发生的事情是两个构建作业在同一个 Docker 容器中运行,而不是每个都有自己的容器。 Whichever one finishes using Gradle first shuts down the Gradle daemon which causes the other one to fail.无论哪一个完成使用 Gradle,都会首先关闭 Gradle 守护进程,从而导致另一个失败。

How can I get these jobs to be able to run in parallel?我怎样才能让这些工作能够并行运行?

Do you have enough resources for simultaneous runs?您是否有足够的资源来同时运行? Especially memory can be a problem, so OS will be killing another containers due to memory pressure.尤其是 memory 可能是一个问题,因此由于 memory 压力,操作系统将杀死另一个容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM