如何定义一个GitLab CI作业依赖于一个或另一个以前的作业?



我想定义一个管道来编译、部署到目标和测试我的项目。

这应该以两种不同的方式发生:在每次提交时进行增量(希望是快速的)构建,以及在晚上安排一次完整的构建。

以下.gitlab-ci.yml的所有作业都标有";manual"用于测试。

stages:
- build
- deploy
- test
variables:
BUILD_ARTIFACTS_DIR: "artifacts"
build-incremental:
timeout: 5h 
stage: build
script:
- echo "Building"
- ./ci/do-prep
- echo "done."
artifacts:
paths:
- $BUILD_ARTIFACTS_DIR/
variables:
BUILD_TOP_DIR: "/workspace/builds"
tags:
- yocto
when: manual
build-nightly:
timeout: 5h
stage: build
script:
- echo "Building"
- ./ci/do-prep
- echo "done."
artifacts:
paths:
- $BUILD_ARTIFACTS_DIR/
tags:
- yocto
when: manual
deploy:
stage: deploy
script:
- echo "Deploying..."
- ./ci/do-deploy
- echo "done."
tags:
- yocto
dependencies:
- build
when: manual
test:
stage: test
script:
- echo "Testing..."
- ./ci/do-test
- echo "done."
tags:
- yocto
dependencies:
- deploy
when: manual

此操作失败,消息为:deploy job: undefined dependency: build.

我如何向GitLab解释deploy阶段需要build-incrementalbuild-nightly工件?

以后我将不得不理解如何触发build-incremental在提交和build-nightly使用时间表,但这似乎是一个不同的问题。

告诉Gitlab您的deploy阶段需要来自特定作业的某些工件:尝试按作业名称命名dependencies。在deploy中,您定义了与build的依赖关系,这是stage的名称,而不是您想要选择工件的作业之一。例子:

deploy:
stage: deploy
script:
- echo "Deploying..."
- ./ci/do-deploy
- echo "done."
tags:
- yocto
dependencies:
- build-incremental
when: manual

更多信息和示例在这里依赖

在您的场景中,根据"源",管道中有两个独立的路径:"推送"或"调度"。您可以使用CI_PIPELINE_SOURCE变量获得管道的源代码。我首先分别构建这些路径,然后将它们组合起来:

# First source: push events
stages:
build
deploy
test
variables:
BUILD_ARTIFACTS_DIR: "artifacts"
build-incremental:
timeout: 5h 
stage: build
script:
- echo "Building"
- ./ci/do-prep
- echo "done."
artifacts:
paths:
- $BUILD_ARTIFACTS_DIR/
variables:
BUILD_TOP_DIR: "/workspace/builds"
tags:
- yocto
rules:
- if: $CI_PIPELINE_SOURCE == 'push'
when: manual
- when: never
deploy-incremental:
stage: deploy
script:
- echo "Deploying..."
- ./ci/do-deploy
- echo "done."
tags:
- yocto
needs: ['build-incremental']
rules:
- if $CI_PIPELINE_SOURCE == 'push'
when: always
- when: never
test-incremental:
stage: test
script:
- echo "Testing..."
- ./ci/do-test
- echo "done."
tags:
- yocto
needs: ['deploy-incremental']
rules:
- if: $CI_PIPELINE_SOURCE == 'push'
when: always
- when: never

在此路径中,如果源是推送,则构建步骤将在手动输入上运行,否则它将永远不会运行。然后,只要源是推送,部署增量步骤将自动运行(无需等待其他作业或阶段),否则它将永远不会运行。最后,test-incremental作业将自动运行,而不需要等待其他作业或阶段,如果它是上面的push

现在我们可以构建schedule路径:
# Scheduled path:
stages:
build
deploy
test
variables:
BUILD_ARTIFACTS_DIR: "artifacts"
build-schedule:
timeout: 5h 
stage: build
script:
- echo "Building"
- ./ci/do-prep
- echo "done."
artifacts:
paths:
- $BUILD_ARTIFACTS_DIR/
variables:
BUILD_TOP_DIR: "/workspace/builds"
tags:
- yocto
rules:
- if: $CI_PIPELINE_SOURCE === 'schedule'
when: manual
- when: never
deploy-schedule:
stage: deploy
script:
- echo "Deploying..."
- ./ci/do-deploy
- echo "done."
tags:
- yocto
needs: ['build-schedule']
rules:
- if $CI_PIPELINE_SOURCE == 'schedule'
when: always
- when: never
test-schedule:
stage: test
script:
- echo "Testing..."
- ./ci/do-test
- echo "done."
tags:
- yocto
needs: ['deploy-schedule']
rules:
- if: $CI_PIPELINE_SOURCE == 'schedule'
when: always
- when: never

这与push路径的工作方式相同,但我们检查源是否为schedule

现在我们可以组合这两条路径:

Combined result:
stages:
build
deploy
test
variables:
BUILD_ARTIFACTS_DIR: "artifacts"
build-incremental:
timeout: 5h 
stage: build
script:
- echo "Building"
- ./ci/do-prep
- echo "done."
artifacts:
paths:
- $BUILD_ARTIFACTS_DIR/
variables:
BUILD_TOP_DIR: "/workspace/builds"
tags:
- yocto
rules:
- if: $CI_PIPELINE_SOURCE == 'push'
when: manual
- when: never
build-schedule:
timeout: 5h 
stage: build
script:
- echo "Building"
- ./ci/do-prep
- echo "done."
artifacts:
paths:
- $BUILD_ARTIFACTS_DIR/
variables:
BUILD_TOP_DIR: "/workspace/builds"
tags:
- yocto
rules:
- if: $CI_PIPELINE_SOURCE == 'schedule'
when: manual
- when: never
deploy-incremental:
stage: deploy
script:
- echo "Deploying..."
- ./ci/do-deploy
- echo "done."
tags:
- yocto
needs: ['build-incremental']
rules:
- if $CI_PIPELINE_SOURCE == 'push'
when: always
- when: never
deploy-schedule:
stage: deploy
script:
- echo "Deploying..."
- ./ci/do-deploy
- echo "done."
tags:
- yocto
needs: ['build-schedule']
rules:
- if $CI_PIPELINE_SOURCE == 'schedule'
when: always
- when: never
test-incremental:
stage: test
script:
- echo "Testing..."
- ./ci/do-test
- echo "done."
tags:
- yocto
needs: ['deploy-incremental']
rules:
- if: $CI_PIPELINE_SOURCE == 'push'
when: always
- when: never
test-schedule:
stage: test
script:
- echo "Testing..."
- ./ci/do-test
- echo "done."
tags:
- yocto
needs: ['deploy-schedule']
rules:
- if: $CI_PIPELINE_SOURCE == 'schedule'
when: always
- when: never

像这样的管道是乏味的,需要一些时间来构建,但当你有多个路径/方法来构建项目时,效果很好。

相关内容

最新更新