我想定义一个管道来编译、部署到目标并测试我的项目。
这应该以两种不同的方式发生:每次提交时增量(希望快速)构建和安排在晚上的完整构建。
以下
.gitlab-ci.yml
所有标记为“手动”的作业用于测试目的。
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-incremental
或build-nightly
工件吗?
稍后我将不得不了解如何在提交时触发
build-incremental
和使用时间表触发 build-nightly
,但这似乎是一个不同的问题。
对于您的场景,根据“源”,管道中有两条单独的路径:“推送”或“计划”。您可以使用
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
像这样的管道很乏味并且需要一些时间来构建,但是当您有多个路径/方式来构建项目时效果很好。
要告诉 Gitlab 您的
deploy
阶段需要特定作业中的某些工件:尝试按作业名称命名 dependencies
。在 deploy
中,您正在使用 build
定义依赖项,这是一个 stage
名称,而不是您想要选择工件的作业之一。
示例:
deploy:
stage: deploy
script:
- echo "Deploying..."
- ./ci/do-deploy
- echo "done."
tags:
- yocto
dependencies:
- build-incremental
when: manual
更多信息和示例在这里依赖项
实现此目的的最佳方法是使用
needs
有关需求的更多信息
以下示例:
stages:
- 📼 start-main-job
- 📎 start-main-job-2
DEV before-main-job:
stage: 📼 start-main-job
needs: []
when: manual
DEV start-main-job:
stage: 📼 start-main-job
needs: ["DEV before-main-job"]
when: on_success
DEV before-main-job-2:
stage: 📎 start-main-job-2
needs: []
when: manual
DEV start-main-job-2:
stage: 📎 start-main-job-2
needs: ["DEV before-main-job-2"]
when: on_success
示例管道