我有一个自托管的 GitLab 来存储我的代码。我的 Flutter 项目使用此管道来构建应用程序包:
stages:
- build
cache:
paths:
- $CI_PROJECT_DIR/.pub-cache/
image: ghcr.io/cirruslabs/flutter:3.16.3
workflow:
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
when: always
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
when: always
- when: never
build-android:
stage: build
tags:
- flutter
before_script:
- export PUB_CACHE=$CI_PROJECT_DIR/.pub-cache
- export PATH="$PATH":"$PUB_CACHE/bin"
- cd src
- flutter doctor --android-licenses
script:
- flutter build appbundle --release
artifacts:
paths:
- "<build dir>/*.aab"
expire_in: 1 week
我为这个项目配置了一个runner,专门运行这个构建过程。runner config.toml是这样的:
concurrent = 1
check_interval = 0
shutdown_timeout = 0
[session_server]
session_timeout = 1800
[[runners]]
name = "<name>"
url = "<git url>"
id = <id>
token = "<token>"
token_obtained_at = <date>
token_expires_at = <date>
executor = "docker"
[runners.cache]
MaxUploadedArchiveSize = 0
[runners.docker]
tls_verify = false
image = "docker:stable"
privileged = false
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/cache"]
shm_size = 0
当管道触发并开始构建应用程序包时,我的问题就出现了。这个过程需要很长时间(大约 25-30 分钟),尽管当我在 PC 上构建时,它会很快完成(大约 2-3 分钟)。为什么会这样?我怎样才能让它更快?
PS:两个系统的资源几乎相同!我的电脑是 MacBook Pro m1 2021,运行器是 LXC,具有 10 核 Intel(R) Xeon(R) CPU E5-2690 CPU、16 GB RAM 和 40 GB 硬盘。
答案1
我为运行器安装了一个 S3 服务器,用于在其中缓存文件(或者,您可以使用 AWS S3)。然后通过添加以下行重新配置运行器以使用此 S3 服务器:
[runners.custom_build_dir]
[runners.cache]
Type = "s3"
Shared = true
[runners.cache.s3]
ServerAddress = "<url>"
AccessKey = "<Access Key>"
SecretKey = "<Secret Key>"
BucketName = "<Bucket name>"
然后添加这些路径来.gitlab-ci.yml
缓存它们:
- /根/.gradle
- /选择
现在 Gradle 构建需要接近 2 分钟,但构建后的另一个操作Preparing "Install Android Emulator..."
需要接近 10 分钟的时间。