gitlab-ci.yml: 作业依赖

gitlab-ci.yml: jobs dependencies

假设有几个阶段:

stages:
    - test
    - build
    - deploy

并且需要将 test 阶段拆分为更小的作业,例如 build-test-imagepytestrun-linters 等。作业 run-testsrun-linters 只有在 build-test-image 有效时才能 运行。

我试过了,但没用:

build-test-image:
  stage: test
  image: ${DOCKER_REGISTRY}/docker:stable
  script:
      - docker build -t ${TEST_CONTAINER_REF} --build-arg ENV=test ./backend
  artifacts:
    when: always
  only:
    - merge_requests

pytest:
  stage: test
  image: ${DOCKER_REGISTRY}/docker:stable
  script:
      - docker run --name ${TEST_CONTAINER_REF} -e SECRET_KEY=${TEST_SECRET_KEY} ${TEST_CONTAINER_REF} runtests; exit $?
  after_script:
      - docker rm ${TEST_CONTAINER_REF}
  only:
    - merge_requests
  needs:
    - build-test-image

错误:

原则上,如果一个作业声明了一个 needs 语句,那么为了执行该作业,它必须 wait 语句中引用的作业才能完成,even if they are in the same stage

在你的情况下 run-tests 应该等待 build-test-image

如果您使用的是自托管,请检查您的 Gitlab 版本

https://your.domain.com/help

14.2 中添加了您需要的功能