在 Gitlab CI 管道中作为作业的一部分触发另一个作业

Trigger another job as a part of job in Gitlab CI Pipeline

我正在为我的项目设置一个 gitlab CI 管道。它有 3 个阶段 - 构建、异步构建和全部构建以及 deployment_mode“开发”。在构建阶段,将创建文件夹、部署 zip。在 build-async 中,所有异步操作(例如将套件复制到 aws s3 存储桶)都会发生,并且 build-all 基本上必须由构建和 build-async 阶段组成。假设gitlab环境变量中已经设置了stage和deployment_mode环境变量。这是示例片段 -

stages:   
  -build    
  -build-async  
  -build-all
  
dev-build:  
  image: python:3.7.4-alpine3.9  
  script:   
    - echo "Hello from dev-build. "  
  stage: build  
  tags:   
    - docker  
    - linux  
  only:  
    variables:  
        - $stage =~ /^build$/ &&  $deployment_mode =~ /^dev$/  

dev-build-async:  
  image: python:3.7.4-alpine3.9  
  script:   
    - echo "Hello from dev-build-async. "  
  stage: build-async  
  tags:   
    - docker  
    - linux  
  only:  
    variables:  
        - $stage =~ /^build-async$/ &&  $deployment_mode =~ /^dev$/  

dev-build-all:  
  image: python:3.7.4-alpine3.9  
  script:   
    - echo "Hello from dev-build-all. "  
  stage: build-all  
  tags:   
    - docker  
    - linux  
  needs: ["dev-build", "dev-build-async"]  
  only:  
    variables:  
        - $stage =~ /^build-all$/ &&  $deployment_mode =~ /^dev$/  

我无法触发作业 dev-build 和 dev-build-async 作为 dev-build-all 的一部分。有谁知道如何同时触发它们?

在这种情况下,当我提供 stage 作为 build-all 和 deployment-mode 作为 dev 时的预期输出是

Hello from dev-build. 
Hello from dev-build-async. 
Hello from dev-build-all. 

dev-build-all在第三阶段,就是这样执行的:

dev-build -> dev-build-async -> dev-build-all

该作业中的

needs: 意味着它仅在两个作业都成功后运行。在这种情况下,这是默认设置,除非您想要工件,否则不需要 needs:

为了从 dev-build-all 触发 dev-builddev-build-async,您应该将这两个作业放在第三个之后的阶段。或者,在两者中使用 needs:。没办法调用前级

示例:

stages:   
  - build-all
  - build    
  - build-async  

dev-build-all:  
  image: python:3.7.4-alpine3.9  
  script:   
    - echo "Hello from dev-build-all. "  
  stage: build-all  
  tags:   
    - docker  
    - linux  
  only:  
    variables:  
        - $stage =~ /^build-all$/ &&  $deployment_mode =~ /^dev$/  

dev-build:  
  image: python:3.7.4-alpine3.9  
  script:   
    - echo "Hello from dev-build. "  
  stage: build  
  tags:   
    - docker  
    - linux
  needs:
    - job: dev-build-all
      artifacts: false
  only:  
    variables:  
      - $stage =~ /^build$/ &&  $deployment_mode =~ /^dev$/  

dev-build-async:  
  image: python:3.7.4-alpine3.9  
  script:   
    - echo "Hello from dev-build-async. "  
  stage: build-async  
  tags:   
    - docker  
    - linux  
  needs:
    - job: dev-build
      artifacts: true
  only:  
    variables:  
        - $stage =~ /^build-async$/ &&  $deployment_mode =~ /^dev$/