简体   繁体   English

Gitlab CI/CD 在“清理项目目录和基于文件的变量”时失败并显示“错误:作业失败:退出代码 1”

[英]Gitlab CI/CD fails while "Cleaning up project directory and file based variables" with "ERROR: Job failed: exit code 1"

My gitlab pipeline which has been running for nearly six months is now failing unexpectedly.我已经运行近六个月的 gitlab 管道现在意外失败。

Every line prior runs successfully and then this happens:之前的每一行都成功运行,然后发生这种情况:

Setting up curl (7.52.1-5+deb9u16) ...
$ curl -s https://deb.nodesource.com/setup_12.x | bash
Cleaning up project directory and file based variables 
ERROR: Job failed: exit code 1

I cannot for the life of me figure out what has changed.我一辈子都弄不明白发生了什么变化。 I thought it might be relatedthis issue but I'm not having any.network issues, timeouts, etc.我认为这可能与此问题有关,但我没有遇到任何网络问题、超时等问题。

Mildly obfuscated version of my.gitlab-ci.yml. my.gitlab-ci.yml 的轻度混淆版本。 Obviously I'm using.gitlab-ci.yml to configure my pipelines and I'm also using the shared GitLab runners.显然,我正在使用 .gitlab-ci.yml 来配置我的管道,并且我还在使用共享的 GitLab 跑步者。


image: python:3.6-stretch

variables:
    ACCESS_KEY_ID: **********
    SECRET_ACCESS_KEY: **********

before_script:
  - apt-get update
  - apt-get install -y curl
  - curl -s https://deb.nodesource.com/setup_12.x | bash
  - apt-get install -y nodejs
  - apt-get install -y npm
  - npm install -g serverless
  - pip install  --upgrade awscli
  - python --version
  - nodejs --version

stages:
  - deploy

deploy:
  stage: deploy

  only:
  - master   # We will run the CD only when something is going to change in master branch.

  script:
    - npm install   # Archive the code repository.
    - pip install -r requirements.txt

    - cd services/service1/
    - sls deploy -v --stage production
    - cd ../../

    - cd services/service2/
    - sls deploy -v --stage production
    - cd ../../

    - cd services/service3/
    - sls deploy -v --stage production
    - cd ../../


  environment:
    name: master

That second to last line ( Cleaning up project directory and file based variables ) is always present in a CI/CD job, pass or fail.倒数第二行( Cleaning up project directory and file based variables )始终存在于 CI/CD 作业中,通过或失败。

What's likely happening is the last command, curl -s https://deb.nodesource.com/setup_12.x | bash可能发生的是最后一条命令curl -s https://deb.nodesource.com/setup_12.x | bash curl -s https://deb.nodesource.com/setup_12.x | bash is failing. curl -s https://deb.nodesource.com/setup_12.x | bash失败。 Unfortunately, since you're downloading a remove file and piping it into bash, it's quite possible that your pipeline starts randomly failing, because that script isn't guaranteed to be the same every time .不幸的是,由于您正在下载一个删除文件并将其通过管道传送到 bash,因此您的管道很可能开始随机失败,因为不能保证该脚本每次都相同

To test it out, I created a clean ubuntu VM, and ran that curl command, and get the following error:为了测试它,我创建了一个干净的 ubuntu VM,并运行了 curl 命令,并得到以下错误: 在此处输入图像描述

Your best bet to fix this long-term is to create a container that has all the dependencies you need for you CI baked in, and store that in your container registry for your GitLab project, then to pull that container each time.长期解决此问题的最佳方法是创建一个容器,其中包含 CI 所需的所有依赖项,并将其存储在 GitLab 项目的容器注册表中,然后每次都拉取该容器。 Not only will that save you CI/CD minutes since you don't have to run installs each time, but it'll prevent this exact issue where your dependencies change underneath you and cause error.这不仅会节省您的 CI/CD 时间,因为您不必每次都运行安装,而且它会防止您的依赖项在您下面发生变化并导致错误的确切问题。 It's also worth noting you should be very careful about passing an externally downloaded script to bash, because that script could change to include anything and your CI would just unknowingly run it.还值得注意的是,将外部下载的脚本传递给 bash 时应该非常小心,因为该脚本可能会更改以包含任何内容,而您的 CI 会在不知不觉中运行它。

I would like to share my case so might help someone else.我想分享我的案例,这样可能会帮助别人。 Base on my experience this error is mostly related to the docker image, as after this stage the pipeline kicks the docker image to start the container.根据我的经验,这个错误主要与 docker 图像有关,因为在此阶段之后,管道将启动 docker 图像以启动容器。

Was getting the same error得到同样的错误

Cleaning up project directory and file based variables

在此处输入图像描述

In my case, I build the desired image build on Mac m1 while the runner was linux .在我的例子中,我在Mac m1 上构建了所需的图像构建,而运行器是 linux

package:
  stage: package
  image:
    name: kaniko:curl
    entrypoint: [""]

So you can run the docker image and if that working on the underlying OS of the runner then it should work.所以你可以运行 docker 图像,如果它在运行器的底层操作系统上工作,那么它应该可以工作。

But in your case I would recommend to move the before stage to pre-build image, as it creates overhead on every pipeline, I do not see issue while running the command with the given docker image但在你的情况下,我建议将前阶段移动到预构建图像,因为它会在每个管道上产生开销,我在使用给定的 docker 图像运行命令时看不到问题

before_script:
  - apt-get update
  - apt-get install -y curl
  - curl -s https://deb.nodesource.com/setup_12.x | bash
  - apt-get install -y nodejs
  - apt-get install -y npm
  - npm install -g serverless
  - pip install  --upgrade awscli

move them pre-build image.移动它们预构建图像。

在此处输入图像描述

btw Its working for me顺便说一句,它对我有用

在此处输入图像描述

stages:
  - deploy

before_script:
  - apt-get update
  - apt-get install -y curl
  - curl -s https://deb.nodesource.com/setup_12.x | bash
  - apt-get install -y nodejs
  - apt-get install -y npm
  - npm install -g serverless
  - pip install  --upgrade awscli
  - python --version
  - nodejs --version



deploy:
  stage: deploy
  tags:
    - kubernetes
  image: python:3.6-stretch
  script: 
    - echo "working"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM