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

Zio*_*yte 21 gitlab-ci

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

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

以下.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
Run Code Online (Sandbox Code Playgroud)

失败并显示消息:deploy job: undefined dependency: build

我该如何向 GitLabdeploy阶段解释需求build-incrementalbuild-nightly工件?

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

Ada*_*all 5

对于您的场景,根据“源”,管道有两条单独的路径:“推送”或“计划”。您可以使用变量获取管道的源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
Run Code Online (Sandbox Code Playgroud)

在此路径中,如果源是推送,则构建步骤将在手动输入时运行,否则它将永远不会运行。然后,只要源是推送,部署增量步骤就会自动运行(无需等待其他作业或阶段),否则永远不会运行。最后,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
Run Code Online (Sandbox Code Playgroud)

这与路径的工作方式相同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
Run Code Online (Sandbox Code Playgroud)

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


小智 5

要告诉 Gitlab 您的deploy阶段需要特定作业中的某些工件:尝试dependencies按作业名称命名。您正在deploy定义一个依赖项, build该依赖stage项的名称不是您想要选择工件的作业名称。例子:

deploy:
stage: deploy
script:
    - echo "Deploying..."
    - ./ci/do-deploy
    - echo "done."
tags:
    - yocto
dependencies:
    - build-incremental
when: manual
Run Code Online (Sandbox Code Playgroud)

更多信息和示例在这里

  • 这并没有回答有关如何运行依赖于先前作业的作业的主要问题。它只是建议仅依赖其中之一。 (9认同)