Search code examples
gitlabdevopsgitlab-cigitlab-apicicd

Gitlab CI/CD: use multiple when conditions


I have like this gitlab ci cd configuration file:

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

In my case job deploy runs automaticaly and job backup must runs manually only when successfully completed job deploy. But in my case this configuration doesn't works and I get error with message:

Found errors in your .gitlab-ci.yml:

jobs:backup when should be one of:

  • on_success
  • on_failure
  • always
  • manual
  • delayed

How I can use multiple when option arguments in my case?


Solution

  • Basically you can't because when does not expect an array. You can work around it though with needs. But this solution does only work if you run your jobs in different stages.

    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