使用自定义 docker-image 会导致构建管道失败

Utilizing custom docker-image causes build pipeline failure

我正在尝试为我在空闲时间做的一个小项目创建一个构建管道。为此,我使用 Spring-Boot 和 Angular。我在本地使用 ./gradlew clean build 构建它。这在我的本地机器上工作得很好,但我 运行 遇到了我无法在 gitlab 上查明的问题。构建是在 gitlab 上完成的,利用它自己的共享 运行ners.

我的 .gitlab-ci.yml 看起来像这样:

    default:
      image: oasisat777/openjdk-and-node:latest

    # If I comment out above line and comment in the line below, everything works fine & dandy
    # image: openjdk:17-jdk-bullseye

    stages:
       - build

    build-job:
       stage: build
       script:
         - whoami
         - java -version
         - npm -v
         - ./gradlew clean compileTestJava compileJava --stacktrace

在上面的示例中,我使用了基于 openjdk:17-jdk-bullseye 的 docker 图像,但扩展为 npm 可用。对应的Dockerfile:

    # goal: build microservices with spring-boot backend and angular frontend in gitlab
    # req'd images: latest stable openjdk and latest node
    # unfortunately there's not openjdk-with-node:latest available, so i have to build it by hand
    # this ought to do the trick, using bullseye as base and then install node additionally
    FROM openjdk:17-jdk-bullseye

    # note: for production use node LTS (even numbers)
    # https://github.com/nodesource/distributions/blob/master/README.md#deb
    RUN curl -fsSL https://deb.nodesource.com/setup_17.x | bash - \
        && apt-get install -y nodejs

    USER root

    CMD ["bash"]

我尝试通过将我的代码添加为卷然后 运行 ./gradlew build 使用生成的对接容器构建我的项目 - 这在我的机器上工作。我的假设是,通过这个我基本上模拟了 gitlab-runner 在开始构建时所做的行为。

    docker run -it -v .:/project
    cd project
    ./gradlew clean build
    Downloading https://services.gradle.org/distributions/gradle-7.4.1-bin.zip
...........10%...........20%...........30%...........40%...........50%...........60%...........70%...........80%...........90%...........100%
    Welcome to Gradle 7.4.1!
    Here are the highlights of this release:
     - Aggregated test and JaCoCo reports
     - Marking additional test source directories as tests in IntelliJ
     - Support for Adoptium JDKs in Java toolchains
    For more details see https://docs.gradle.org/7.4.1/release-notes.html
    Starting a Gradle Daemon (subsequent builds will be faster)
    [...]
    BUILD SUCCESSFUL

这是构建管道的输出:

   $ whoami
   root
   $ java -version
   openjdk version "17.0.2" 2022-01-18
   OpenJDK Runtime Environment (build 17.0.2+8-86)
   OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)
   $ npm -v
   8.5.5
   $ ./gradlew clean compileTestJava compileJava --stacktrace
   Downloading https://services.gradle.org/distributions/gradle-7.4.1-bin.zip
...........10%...........20%...........30%...........40%...........50%...........60%...........70%...........80%...........90%...........100%
   Could not set executable permissions for: /root/.gradle/wrapper/dists/gradle-7.4.1-bin/58kw26xllvsiedyf3nujyarhn/gradle-7.4.1/bin/gradle
   Welcome to Gradle 7.4.1!
   Here are the highlights of this release:
     - Aggregated test and JaCoCo reports
     - Marking additional test source directories as tests in IntelliJ
     - Support for Adoptium JDKs in Java toolchains
    For more details see https://docs.gradle.org/7.4.1/release-notes.html
    Starting a Gradle Daemon (subsequent builds will be faster)
    FAILURE: Build failed with an exception.
    * What went wrong:
    A problem occurred starting process 'Gradle build daemon'
    * Try:
    > Run with --info or --debug option to get more log output.
    > Run with --scan to get full insights.
    * Exception is:
    org.gradle.process.internal.ExecException: A problem occurred starting process 'Gradle build daemon'
    at org.gradle.process.internal.DefaultExecHandle.execExceptionFor(DefaultExecHandle.java:241)
    at org.gradle.process.internal.DefaultExecHandle.setEndStateInfo(DefaultExecHandle.java:218)
    at org.gradle.process.internal.DefaultExecHandle.failed(DefaultExecHandle.java:369)
    at org.gradle.process.internal.ExecHandleRunner.run(ExecHandleRunner.java:87)
    at org.gradle.internal.operations.CurrentBuildOperationPreservingRunnable.run(CurrentBuildOperationPreservingRunnable.java:38)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
    at org.gradle.internal.concurrent.ManagedExecutorImpl.run(ManagedExecutorImpl.java:48)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    at java.base/java.lang.Thread.run(Thread.java:833)
    Caused by: net.rubygrapefruit.platform.NativeException: Could not start '/usr/local/openjdk-17/bin/java'
    at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:27)
    at net.rubygrapefruit.platform.internal.WrapperProcessLauncher.start(WrapperProcessLauncher.java:36)
    at org.gradle.process.internal.ExecHandleRunner.startProcess(ExecHandleRunner.java:98)
    at org.gradle.process.internal.ExecHandleRunner.run(ExecHandleRunner.java:71)
    ... 6 more
    Caused by: java.io.IOException: Cannot run program "/usr/local/openjdk-17/bin/java" (in directory "/root/.gradle/daemon/7.4.1"): error=0, Failed to exec spawn helper: pid: 106, exit value: 1
    at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1143)
    at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1073)
    at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:25)
    ... 9 more
    Caused by: java.io.IOException: error=0, Failed to exec spawn helper: pid: 106, exit value: 1
    at java.base/java.lang.ProcessImpl.forkAndExec(Native Method)
    at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:314)
    at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:244)
    at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1110)
    ... 11 more
   * Get more help at https://help.gradle.org
   Cleaning up project directory and file based variables

现在,我做了以下观察

Could not set executable permissions for: /root/.gradle/wrapper/dists/gradle-7.4.1-bin/58kw26xllvsiedyf3nujyarhn/gradle-7.4.1/bin/gradle

不幸的是,我 运行 没有想法,对于我丢失的任何后续问题或观察,我将不胜感激。这个问题最常见的答案似乎是“确保 gradlew 是可执行的!” - 是的。

在输入这个答案时,我想知道这是否是一个 x86/x64/arm64 相关的问题?我刚刚注意到 OS/ARCH 字段在 docker 集线器上设置为 linux/arm64/v8

它按照 sytech 的建议工作 - 我刚刚使用 gitlab 构建并推送了 docker-image 并将其推送到其容器存储库中。然后我在我的应用程序构建中使用它 - 它按预期工作。

Dockerfile 存储库中的 .gitlab-ci.yml 如下所示:

  variables:
    IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
    # Tell 'docker:dind' to enable TLS (recommended)
    # and generate certificates in the specified directory.
    DOCKER_TLS_CERTDIR: "/certs"
  
  build-push-docker-image-job:
    # Specify a Docker image to run the job in.
    image: docker:latest
    # Specify an additional image 'docker:dind' ("Docker-in-Docker") that
    # will start up the Docker daemon when it is brought up by a runner.
    services:
      - docker:dind
    script:
      - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
      - docker build -t $IMAGE_TAG .
      - docker push $IMAGE_TAG
    only:
      - master

(=> 来源:https://www.shellhacks.com/gitlab-ci-cd-build-docker-image-push-to-registry)

然后将构建发布到我存储库的容器存档中。

在另一个构建中,我只是引用构建:

default:
  image: registry.gitlab.com/<GROUP>/<PROJECT>/<SOME_NAME>:master

开始一个新的构建 - 然后构建终于可以工作了:

BUILD SUCCESSFUL in 3m 7s
11 actionable tasks: 8 executed, 3 up-to-date
Cleaning up project directory and file based variables
00:00
Job succeeded

我怀疑架构是罪魁祸首。