如何为 Gitlab CI 创建自定义 docker 镜像

如何为 Gitlab CI 创建自定义 docker 镜像

CI已经Gitlab创建了我的 android 应用程序。这是我的示例.gitlab-ci.yml

image: openjdk:8-jdk

variables:
  ANDROID_COMPILE_SDK: "28"
  ANDROID_BUILD_TOOLS: "28.0.2"
  ANDROID_SDK_TOOLS:   "4333796"

before_script:
  - apt-get --quiet update --yes
  - apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1
  - wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/sdk-tools-linux-${ANDROID_SDK_TOOLS}.zip
  - unzip -d android-sdk-linux android-sdk.zip
  - echo y | android-sdk-linux/tools/bin/sdkmanager "platforms;android-${ANDROID_COMPILE_SDK}" >/dev/null
  - echo y | android-sdk-linux/tools/bin/sdkmanager "platform-tools" >/dev/null
  - echo y | android-sdk-linux/tools/bin/sdkmanager "build-tools;${ANDROID_BUILD_TOOLS}" >/dev/null
  - export ANDROID_HOME=$PWD/android-sdk-linux
  - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
  - chmod +x ./gradlew
  # temporarily disable checking for EPIPE error and use yes to accept all licenses
  - set +o pipefail
  - yes | android-sdk-linux/tools/bin/sdkmanager --licenses
  - set -o pipefail

stages:
  - build
  - test

lintDebug:
  stage: build
  script:
    - ./gradlew -Pci --console=plain :app:lintDebug -PbuildDir=lint

assembleDebug:
  stage: build
  script:
    - ./gradlew assembleDebug
  artifacts:
    paths:
    - app/build/outputs/

debugTests:
  stage: test
  script:
    - ./gradlew -Pci --console=plain :app:testDebug

但是每次我们都有一个commit,这个作业下载SDK等等...我怎样才能用这个捕获这个容器?我应该为docker容器 SDK制作吗?commitdocker commit -m "What you did to the image" -a "Author Name" container_id repository/new_image_name

我的跑步者/etc/gitlab-runner/config.toml

concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "MYAPP-CI-ANDROID"
  url = "https://gitlab.com/"
  token = "vNcmGuDwWwz9ZwwFOz-z"
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "openjdk:8-jdk"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]

我应该在 docker hub 上发布我的 docker 镜像吗?或者我们可以用其他方式来做?谢谢你的帮助。

答案1

刚刚也经历了这一切,因为每次下载 SDK 似乎很愚蠢。为此构建自定义 docker 映像非常简单。

FROM openjdk:8-jdk

ARG ANDROID_COMPILE_SDK="29"
ARG ANDROID_BUILD_TOOLS="29.0.1"
ARG ANDROID_SDK_TOOLS="4333796"

# Update and Install some packages
RUN apt-get --quiet update --yes && \
    apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1

# Install Android SDK
RUN wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/sdk-tools-linux-${ANDROID_SDK_TOOLS}.zip && \
    unzip -d android-sdk-linux android-sdk.zip

RUN echo y | android-sdk-linux/tools/bin/sdkmanager "platforms;android-${ANDROID_COMPILE_SDK}" >/dev/null && \
    echo y | android-sdk-linux/tools/bin/sdkmanager "platform-tools" >/dev/null && \
    echo y | android-sdk-linux/tools/bin/sdkmanager "build-tools;${ANDROID_BUILD_TOOLS}" >/dev/null

ENV ANDROID_HOME=$PWD/android-sdk-linux

#RUN chmod +x ./gradlew

RUN yes | android-sdk-linux/tools/bin/sdkmanager --licenses

CMD ["jshell"]
  1. 建造

    docker build -t image_name \
    --build-arg ANDROID_COMPILE_SDK="29" \
    --build-arg ANDROID_BUILD_TOOLS="29.0.1" \
    --build-arg ANDROID_SDK_TOOLS="4333796" .
    
  2. 标记并推送

    docker tag image_name YOUR_DOCKER_REPO/YOUR_DOCKER_IMAGE:latest
    docker login docker.io
    docker push YOUR_DOCKER_REPO/YOUR_DOCKER_IMAGE:latest
    

然后,您将使用该图像而不是 openjdk8。它有效。它非常简单,可以节省我大约 6 分钟的管道时间。您仍然需要chmod +x ./gradlew在您的 中运行before_script。理论上,您可以确保 gradlew 在 git 中可执行。但当您有人(完全)使用它时,尤其是在使用 Windows 时,这可能不太可靠。

相关内容