How to use if-else condition on gitlabci

Hereunder three syntax options for that kind of statement. From gitlab-ci documentation :

Using shell variable

deploy-dev:
image: testimage
environment: dev
tags:
 - kubectl
script:
 - if [ "$flag" == "true" ]; then MODULE="demo1"; else MODULE="demo2"; fi
 - kubectl apply -f ${MODULE} --record=true

Using shell variable with yaml multiline block

deploy-dev:
image: testimage
environment: dev
tags:
  - kubectl
script:
  - >
    if [ "$flag" == "true" ]; then
      kubectl apply -f demo1 --record=true
    else
      kubectl apply -f demo2 --record=true
    fi

Using gitlab rules

workflow:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
      when: never
    - if: '$CI_PIPELINE_SOURCE == "push"'
      when: never
    - when: always

Using gitlab templates and variables

demo1-deploy-dev:
  extends: .deploy-dev
  only:
    variables: [ $flag == "true" ]
  variables:
    MODULE: demo1
        
demo2-deploy-dev:
  extends: .deploy-dev
  only:
    variables: [ $flag == "false" ]
  variables:
    MODULE: demo2
        
.deploy-dev:
image: testimage
environment: dev
tags:
  - kubectl
script:
  - kubectl apply -f ${MODULE} --record=true

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)