How to use if else condition inside the gitlab-CI.
I have below code:
deploy-dev: image: testimage environment: dev tags: - kubectl script: - kubectl apply -f demo1 --record=true - kubectl apply -f demo2 --record=true
Now I want to add a condition something like this
script: - (if [ "$flag" == "true" ]; then kubectl apply -f demo1 --record=true; else kubectl apply -f demo2 --record=true);
Could someone provide the correct syntax for the same? Is there any documentation for the conditions (if-else, for loop) in gitlabci?
Hereunder three syntax options for that kind of statement. From gitlab-ci documentation :
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
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
workflow: rules: - if: '$CI_PIPELINE_SOURCE == "schedule"' when: never - if: '$CI_PIPELINE_SOURCE == "push"' when: never - when: always
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
Note that with GitLab 13.3 (August 2020), there is an improvement to the if-else rule syntax:
CI/CD rules:if support logical expressions with parentheses
If you use the
rules
keyword withif
clauses, it’s now even more powerful, with support for bracketed expressions evaluated by the pipeline processor.You can use more complex and efficient AND (
&&
) / OR (||
) expressions, making your pipelines rules more logical, powerful, and easier to manage.See Documentation and Issue.
And, with GitLab 13.8 (January 2021)
Support variables for pipeline rules
Previously, the
rules
keyword was limited in scope and only determined if a job should be included or excluded from pipelines. In this release, you can now decide if certain conditions are met and subsequently override variables in jobs, providing you with more flexibility when configuring your pipelines.See Documentation and Issue.
With GitLab 13.12 (May 2021):
Support variables in CI/CD pipeline 'workflow:rules'
Previously, the
rules
keyword was limited in scope and only determined if a job should be included or excluded from pipelines. In 13.8, we added the ability to use thevariables
keyword withrules
to set variable values in a job based on which rule matched.In this release we’ve extended this ability to
workflow: rules
, so you can set variable values for the whole pipeline if certain conditions match.
This helps you make your pipelines even more flexible.See Documentation and Issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With