Gitlab CI/CD: 使用多个when条件

Gitlab CI/CD: use multiple when conditions

我喜欢这个gitlab ci cd配置文件:

image: docker:git

stages:  
  - develop
  - production
default:
  before_script:
    - apk update && apk upgrade && apk add git curl

deploy:
    stage: develop
    script: 
      - echo "Hello World"
  
backup:
    stage: develop    
    when:
      - manual
      - on_success

remove:
    stage: develop    
    when:
      - delayed
      - on_success
    start_in: 30 minutes

在我的例子中,作业 deploy 自动运行,而作业 backup 必须在成功完成作业 deploy 时手动运行。但在我的例子中,这个配置不起作用,我收到错误消息:

在您的中发现错误 .gitlab-ci.yml:

jobs:backup 应该是以下之一:

如何在我的案例中使用多个 when 选项参数?

基本上你不能,因为 when 不需要数组。您可以使用 needs 解决它。但此解决方案仅在您 运行 处于不同阶段的工作时才有效。

image: docker:git

stages:  
  - deploy
  - backup
  - remove

deploy:develop:
  stage: deploy
  script: 
    - exit 1
  
backup:develop:
  stage: backup  
  script:
    - echo "backup"  
  when: manual
  needs: ["deploy:develop"]

remove:develop:
  stage: remove
  script:
    - echo "remove"    
  when: delayed
  needs: ["backup:develop"]
  start_in: 30 minutes