我需要找到一种方法在gitlab管道中缓存jdk安装

问题描述 投票:0回答:1

我正在运行 GitLab 管道来测试和构建 Android 应用程序。

这是我当前的

gitlab-ci.yml

image: fabernovel/android:api-33-v1.7.0

stages:
  - test
  - build
  - beta

cache:
  key:
    files:
      - Gemfile.lock
  paths:
    - vendor/bundle

before_script:
  - apt-get update && apt-get install -y openjdk-17-jdk
  - jenv add /usr/lib/jvm/java-17-openjdk-amd64
  - jenv global 17.0
  - bundle check --path vendor/bundle || bundle install --path vendor/bundle --jobs $(nproc)

test:
  image: fabernovel/android:api-33-v1.7.0
  tags:
    - docker-ansible
  stage: test
  cache:
    key:
      files:
        - gradle/wrapper/gradle-wrapper.properties
    paths:
      - cache/caches/
      - cache/notifications/
      - cache/wrapper/

  script:
    - bundle exec fastlane test

build:
  image: fabernovel/android:api-33-v1.7.0
  tags:
    - docker-ansible
  stage: build
  cache:
    key:
      files:
        - gradle/wrapper/gradle-wrapper.properties
    paths:
      - cache/caches/
      - cache/notifications/
      - cache/wrapper/
  variables:
    SECURE_FILES_DOWNLOAD_PATH: './'
  script:
    - apt update -y && apt install -y curl
    - curl -s https://gitlab.com/gitlab-org/incubation-engineering/mobile-devops/download-secure-files/-/raw/main/installer | bash
    - bundle exec fastlane build
  artifacts:
    paths:
      - app/build/outputs/bundle/release/app-release.aab

beta:
  image: fabernovel/android:api-33-v1.7.0
  tags:
    - docker-ansible
  stage: beta
  script:
    - bundle exec fastlane beta
  when: manual
  allow_failure: true
  only:
    refs:
      - main

目前,它工作正常,但速度很慢,因为每次运行它时都会再次为每个阶段安装jdk。我找不到有效的缓存方法来在下一个构建中跳过该部分。 另外,如果有任何其他方式来加速或缓存,非常欢迎建议。

java android caching gitlab-ci pipeline
1个回答
0
投票

为了扩展评论,使用自定义 Docker 映像来封装作业所需的环境应该会有所帮助。
如果您无法直接访问运行 GitLab 和 GitLab 运行程序的服务器,您可以将自定义 Docker 映像推送到可公开访问的 Docker 注册表,例如 Docker Hub

使用必要的 JDK 安装和您需要的任何其他依赖项创建

Dockerfile

FROM fabernovel/android:api-33-v1.7.0
RUN apt-get update && apt-get install -y openjdk-17-jdk

并在本地构建 Docker 镜像:

docker build -t your-username/your-image-name:your-tag .

然后将自定义docker镜像推送到Docker Hub**:

docker login --username=your-username
docker push your-username/your-image-name:your-tag

修改您的

.gitlab-ci.yml
文件以使用您推送到 Docker Hub 的自定义 Docker 映像:

image: your-username/your-image-name:your-tag

# Rest of your workflow

预装了 JDK 的自定义 Docker 镜像应该会显着加快 GitLab 管道的运行速度,因为它不再需要在每次运行时安装 JDK。

© www.soinside.com 2019 - 2024. All rights reserved.