I have multiple stages in gitlab-ci.yml. I want to limit for example deploy to production stage that can be executed only between 8am and 16pm. Is this "if" statement in stage or it can be solved some other way?
2 Answers
You can setup deployment freezes to do this.
For example, you can setup your CI YAML like so using rules:
(you can also check for the freeze in your script, if you want):
deploy_to_production:
stage: deploy
script:
- deploy_to_prod.sh
rules:
- if: $CI_DEPLOY_FREEZE == null # only run when there is no deploy freeze
For the effect you stated, you can then configure the deployment freezes with the following parameters:
Freeze start: 0 16 * * *
# start freeze at 16:00, preventing deployments after 4:00PM
Freeze end: 0 8 * * *
# end this freeze at 8AM (allowing deployments after 8AM)
Timezone: <your timezone>
There is no particular way to apply this to a stage you'll have to setup rules:
(or equivalent script steps) for each job you want to not run during the freeze.

- 29,298
- 3
- 45
- 86
Note that in complement of the deploy freeze, GitLab 16.0 (May 2023) now brings:
Display message when deploy freeze is active
GitLab now shows you a message on the Environments page when a deploy freeze is in effect. This helps ensure your team is aware of when freezes occur, and when deployments are not allowed.
See Documentation and Issue.
That way, your team is aware of the restriction you have put in place through cron syntax:

- 1,262,500
- 529
- 4,410
- 5,250