Sub topic 10
Delaying a deployment with a wait timer
This chapter assumes you can read workflow YAML, know how needs orders jobs, and have already created an environment and attached a job to it with the environment: key.
What the wait timer does
A wait timer is a protection rule that holds a job for a fixed number of minutes after the job would otherwise start. The job targets an environment, and the timer starts at that moment. Once the delay ends, the job continues, provided the environment's other rules also pass.
The value is a whole number of minutes from 0 to 43,200 (30 days). A value of 0 means no delay.
Common reasons to use one:
- A cooling-off period after staging, so monitoring has time to show a problem.
- A window in which someone can cancel the run before production changes.
- A deliberate gap between deployments to the same target.
Configure it
In the repository, open Settings, then Environments, then select the environment. Tick Wait timer, enter the minutes, and save.
You can also set it through the API. The wait_timer field is the same one used in the API chapter's example:
gh api --method PUT repos/OWNER/REPO/environments/production \
-F wait_timer=10A PUT to this endpoint replaces the environment's configuration. Fields you leave out, such as reviewers, can be reset. Send the full set of settings every time.
How it combines with other rules
Rules on one environment are all enforced. The timer and required reviewers do not replace each other.
The timer and the approval request run in parallel. If the timer is 10 minutes and a reviewer approves after 2, the job still waits for the remaining 8. If the reviewer approves after 30 minutes, the job runs right after the approval.
A workflow that uses it
Nothing in the workflow file refers to the timer. The delay comes only from the environment's settings.
name: Release
on:
push:
branches: [main]
jobs:
deploy-staging:
runs-on: ubuntu-latest
environment: staging
steps:
- run: ./deploy.sh staging
deploy-production:
needs: deploy-staging
runs-on: ubuntu-latest
environment: production
steps:
- run: ./deploy.sh productionWith a 10-minute timer on production, the run shows deploy-production as waiting after deploy-staging succeeds.
Common errors
- Expecting a pause in the YAML. Adding
sleepor atimeout-minutesvalue is not the same thing. Those consume runner time. A wait timer does not. - Forgetting the limit on cost. Time spent waiting for protection rules does not use runner minutes, but the whole workflow run must still finish within its overall limit.
- Treating it as a safety control by itself. A timer only delays. Without reviewers or branch rules, the deployment still happens.
- Overwriting settings with the API. See the
PUTnote above.
Exercise
A production environment has a 15-minute wait timer and one required reviewer. The reviewer approves 5 minutes after the job starts waiting. When does the job run, and why?
Show answer
It runs 15 minutes after it started waiting. Both rules must be satisfied, and the timer runs in parallel with the approval request. The approval arrives early, but the job still has to wait out the remaining 10 minutes of the timer.