如何将工件传递到另一个阶段?

ygo*_*goe 78 gitlab gitlab-ci

我想将GitLab CI与.gitlab-ci.yml文件一起使用,以使用单独的脚本运行不同的阶段.第一阶段生成一个工具,必须在稍后阶段用于执行测试.我已将生成的工具声明为工件.

现在我如何在后期工作中执行该工具?什么是正确的路径,它周围会有什么文件?

例如,第一阶段构建工件/ bin/TestTool/TestTool.exe,该目录包含其他所需文件(DLL和其他).我的.gitlab-ci.yml文件如下所示:

releasebuild:
  script:
    - chcp 65001
    - build.cmd
  stage: build
  artifacts:
    paths:
      - artifacts/bin/TestTool/

systemtests:
  script:
    - chcp 65001
    - WHAT TO WRITE HERE?
  stage: test
Run Code Online (Sandbox Code Playgroud)

如果相关,则在Windows上运行构建和测试.

use*_*793 80

你必须使用 dependencies

使用此配置测试阶段将下载在构建阶段创建的未跟踪文件:

build:
  stage: build
  artifacts:
    untracked: true
  script:
    - ./Build.ps1

test:
  stage: test
  dependencies: 
    - build
  script:
    - ./Test.ps1
Run Code Online (Sandbox Code Playgroud)

  • 你真的必须使用依赖吗?Gitlab文档说明`请注意,默认情况下会传递所有先前阶段的工件.问题是你什么时候需要使用依赖项. (11认同)
  • 终于搞定了!这里的关键点是依赖项应该与工件一起使用.只有所包含的工件可供后续阶段使用.毋庸置疑,对于正在上传的内容要保守.我会说使用expire_in.否则我们最终会浪费大量的存储空间.这些工件在构建作业/阶段/步骤中上载到gitlab,并在测试中下载. (9认同)
  • 文档令人困惑 - 传递“所有先前阶段”的工件是什么意思?如果我理解正确,如果所有先前的阶段总是通过,那么来自所有作业的所有数据都将可用,我不需要使用“依赖项”标签。我能想到的唯一情况是,如果我不想要这种行为并且只传递来自一个或选定的几个工作的工件。 (5认同)
  • 默认情况下,将传递来自所有先前**阶段**的@Josef工件(不是来自先前的作业) (3认同)
  • @Josef,当您当前工作不需要所有先前阶段的所有工件时。假设您在构建阶段生成了 10 GB 的二进制文件,但您的最后阶段仅发送一些有关成功构建的电子邮件 - 您无需为此作业下载全部 10 GB (3认同)
  • 该文档很好地解决了这一问题:https://docs.gitlab.com/ee/ci/yaml/#dependencies (2认同)

Chu*_*uan 15

Since artifacts from all previous stages are passed by default, we just need to define stages in correct order. Please try the example below, which could help understanding.

image: ubuntu:18.04

stages:
  - build_stage
  - test_stage
  - deploy_stage

build:
  stage: build_stage
  script:
    - echo "building..." >> ./build_result.txt
  artifacts:
    paths:
    - build_result.txt
    expire_in: 1 week

unit_test:
  stage: test_stage
  script:
    - ls
    - cat build_result.txt
    - cp build_result.txt unittest_result.txt
    - echo "unit testing..." >> ./unittest_result.txt
  artifacts:
    paths:
    - unittest_result.txt
    expire_in: 1 week

integration_test:
  stage: test_stage
  script:
    - ls
    - cat build_result.txt
    - cp build_result.txt integration_test_result.txt
    - echo "integration testing..." >> ./integration_test_result.txt
  artifacts:
    paths:
    - integration_test_result.txt
    expire_in: 1 week

deploy:
  stage: deploy_stage
  script:
    - ls
    - cat build_result.txt
    - cat unittest_result.txt
    - cat integration_test_result.txt
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

And in case to pass artifacts between jobs in different stages, we can use dependencies together with artifacts to pass the artifacts, as described from the document.

And one more simpler example:

image: ubuntu:18.04

build:
  stage: build
  script:
    - echo "building..." >> ./result.txt
  artifacts:
    paths:
    - result.txt
    expire_in: 1 week

unit_test:
  stage: test
  script:
    - ls
    - cat result.txt
    - echo "unit testing..." >> ./result.txt
  artifacts:
    paths:
    - result.txt
    expire_in: 1 week

deploy:
  stage: deploy
  script:
    - ls
    - cat result.txt
Run Code Online (Sandbox Code Playgroud)

  • @MichaelOsofsky 您可以使用相同的名称来命名工件,原始工件不会被下一阶段的同名工件覆盖。下一阶段仅下载前一阶段的工件,它是它的副本。我在示例中对它们进行了不同的命名,主要是因为单元测试和集成将并行执行。如果我们删除 .eg 集成测试作业,所有作业将按顺序执行,然后我们可以对所有工件使用相同的名称,而不会造成任何混淆。仅供参考,我用另一个例子更新了答案。 (3认同)
  • 非常清楚的解释,谢谢。如果某个阶段对某个工件的命名与前一阶段的工件同名,那么原始工件是否会被覆盖? (2认同)
  • 根据日志,部署阶段会同时下载构建阶段和测试阶段的result.txt,但后一个阶段会覆盖前一个阶段。 (2认同)
  • 顺便说一句,原始工件没有被触及,并且始终可以从 CI/CD -> Pipelines 下载,然后单击右侧工件的下拉按钮,您将找到所有阶段的所有工件。 (2认同)

k1e*_*ran 12

如果您希望foo/在下一阶段可用并且它在您的中,.gitignore您需要将其列在artifacts创建它的阶段中,或者按照此处的说明使用untracked: true。因为我只想要一个子集,所以我没有\xe2\x80\x99t 使用untracked: true.

\n

以下方法对我有用(dependencies在下一阶段没有指定)

\n
   artifacts:\n     paths:\n       - foo/\n     expire_in: 1 hour\n
Run Code Online (Sandbox Code Playgroud)\n

顺便说一句,关于:expire_in: 1 hour部分: \n我在https://gitlab.com/gitlab-org/gitlab-runner/-/issues/2133
上读到,没有办法让工件在管道结束时自动过期,并且默认保留期长得惊人(默认为 30 天) - 因此需要基于时间的拼凑来摆脱它们 - 请参阅https://docs.gitlab.com/ee/ci/yaml/

\n


ash*_*ium 5

如果您使用该needs:关键字,默认工件管理行为会发生变化。唯一可用的工件来自需求图上的“上游”作业。另外,dependencies:关键字不能与关键字一起使用needs:

要解决问题中的管道片段,needs只需向创建所需工件的作业添加关系即可:

releasebuild:
  script:
    - chcp 65001
    - build.cmd
  stage: build
  artifacts:
    paths:
      - artifacts/bin/TestTool/

systemtests:
  script:
    - chcp 65001
  stage: test  
  needs:
    - job: releasebuild
      artifacts: true
Run Code Online (Sandbox Code Playgroud)

注意:该needs:artifacts:关键字默认为true并且可以省略。当设置为 时false,作业将不会加载上游工件。