Docker 构建失败但 Gitlab-CI 报告成功(误报)

Docker Build fails but Gitlab-CI reports succeeds(False positive)

我有一个从 Docker 文件构建我的 Docker 图像的 Gitlab 管道,但是当“docker 构建”命令失败时,管道仍然报告成功。

build:
  stage: build
  script:
    - docker build --no-cache -t $CI_REGISTRY/dockerfile:$CONTAINER_LABEL .

我在我的 Docker 文件中添加了一个错误,这样我就可以引发一个错误:

FROM ubuntu:20.04   
RUN not_a_real_command_that_should_fail

管道运行:

Running with gitlab-runner 14.0.1 (c1edb478)
  on ******* Cy33WtLD
Preparing the "shell" executor
00:00
Using Shell executor...
Preparing environment
00:01
Running on **********...
Getting source from Git repository
00:04
Fetching changes with git depth set to 50...
Reinitialized existing Git repository in C:/gitlab-runner/builds/Cy33WtLD/0/dockerfile/.git/
Checking out 999a815d as fix_pipeline_status...
git-lfs/2.13.2 (GitHub; windows amd64; go 1.14.13; git fc664697)
Skipping Git submodules setup
Executing "step_script" stage of the job script
00:03
$ docker build --no-cache -t $CI_REGISTRY/dockerfile:$CONTAINER_LABEL .
#1 [internal] load build definition from Dockerfile
#1 sha256:c544637cbaca3e93c2a8a8c00efd4f81ee45b1abd410d971af12de8dae21e8ea
#1 transferring dockerfile: 3.04kB done
#1 DONE 0.0s
#2 [internal] load .dockerignore
#2 sha256:ab745a167b371ba5e9380063cb278a7792a5838550b89f02f35d7f6a583fb548
#2 transferring context: 2B done
#2 DONE 0.0s
#3 [internal] load metadata for docker.io/library/ubuntu:20.04
#3 sha256:8e67b796a66f85f06793e026943184e32d365c77929e94d2ac98b34a1e1cb30e
#3 DONE 0.6s
#4 [ 1/17] FROM docker.io/library/ubuntu:20.04@sha256:9d6a8699fb5c9c39cf08a0871bd6219f0400981c570894cd8cbea30d3424a31f
#4 sha256:c8b7f784dc481f981cf0bc39c4d4e60a54a355d96ca108a13ffffa3bfa047067
#4 CACHED
#20 [internal] load build context
#20 sha256:d12ef8e847404a2cc9437d8099f4b73f215c48eb92002e759a5f264989ae3ace
#20 transferring context: 92B 0.0s done
#20 DONE 0.0s
#5 [ 2/17] RUN not_a_real_command_that_should_fail
#5 sha256:724c85340f260555ab116f9064ba3c7a2c16fe0af059ef5226df31545b30ddb6
#5 0.485 /bin/sh: 1: not_a_real_command_that_should_fail: not found
#5 ERROR: executor failed running [/bin/sh -c not_a_real_command_that_should_fail]: exit code: 127
------
 > [ 2/17] RUN not_a_real_command_that_should_fail:
------
executor failed running [/bin/sh -c not_a_real_command_that_should_fail]: exit code: 127
Cleaning up file based variables
00:01
Job succeeded

退出代码是 127,我添加了一个 -after_stage 步骤来打印出 return 代码“echo $?”并得到“真实”的结果。看起来这应该足以让命令触发管道中的故障。 如果重要的话,gitlab-runner 是 Windows 机器上的 shell 执行器。

有什么建议吗?

1 - Gitlab 问题

13.1.1 之前的 gitlab runner 版本存在错误:

https://gitlab.com/gitlab-org/gitlab-runner/-/issues/26347

FF_USE_LEGACY_KUBERNETES_EXECUTION_STRATEGY 设置为 False

时作业始终成功

解决方案似乎正在更新。

2 - 脚本问题

尝试在出现错误时强制退出 1

build:
  stage: build
  script:
    - docker build --no-cache -t $CI_REGISTRY/dockerfile:$CONTAINER_LABEL . || exit 1

编辑 1

默认情况下,Powershell 会继续执行。

您可以设置这些变量:

job:
  stage: build
  variables: 
    ErrorActionPreference: stop 
  script:
    - docker build --no-cache -t $CI_REGISTRY/dockerfile:$CONTAINER_LABEL . 

请在 github 上查看此问题以获取更多信息:

https://gitlab.com/gitlab-org/gitlab-runner/-/issues/4683

将管道移动到 Ubuntu 机器上的 gitlab-runner 后,管道按预期失败。假设 Windows-Powershell 退出代码未正确触发