Skip to content

Instantly share code, notes, and snippets.

@farhad-taran
Last active July 4, 2023 12:41
Show Gist options
  • Save farhad-taran/5170b25908492c6e9bfefb719fa91fbd to your computer and use it in GitHub Desktop.
Save farhad-taran/5170b25908492c6e9bfefb719fa91fbd to your computer and use it in GitHub Desktop.
Implementing a code freeze policy and disallowing merges on Fridays and weekends and bank holidays

It might be a good idea to enforce a no merge policy on our main branch so that the system does not experience downtime when staff are not working. this could be late fridays, weekends or on bank holidays.

I have written a small bash script that I can use in our CI jobs, the bash script either succeeds or fails based on wether today is a holiday or a bankholiday. I can then hook this job to github as a required status check. if this status check fails then a merge to main or master wont be possible as that would indidcate that today is a code freeze.

In the following script I am utalizing a public api to get a list of all bank holidays and am checking it against todays date. I am also checking wether today is a friday or a weekend.

The only way to merge a branch is to name it beginning with bug-fix so that critical bugs can be fixed in production.

I am also optimising the execution by exiting early for bug fix branches or weekends before calling the bank holidays api.

#!/bin/bash

# if bug-fix then succeed early
BRANCH_NAME="random-branch"
IS_BUGFIX=0; [[ ${BRANCH_NAME} == bug-fix-* ]] && IS_BUGFIX=1
if [[ $IS_BUGFIX == 1 ]]
then
    exit 0
fi

# if friday or weekend then fail early
IS_WEEKEND=0; [[ $(date +%u) -ge 5 ]] && IS_WEEKEND=1
if [[ $IS_WEEKEND == 1 ]]
then
    exit 1
fi

# if bank holiday in england and wales then fail
HOLIDAYS=$(curl --silent --insecure --request GET https://www.gov.uk/bank-holidays.json | jq '."england-and-wales".events[].date')
TODAY=$(date +%Y-%m-%d)
IS_HOLIDAY=0; [[ " ${HOLIDAYS[@]} " =~ " ${TODAY} " ]] && IS_HOLIDAY=1
if [[ $IS_HOLIDAY == 1 ]]
then
    exit 1
fi

exit 0

CircleCi config:

version: 2.1

jobs:
  code-freeze-check:
    docker:
      - image: cimg/node:17.2.0
    steps:
      - run:
          name: code freeze check
          command: |
              # if bug-fix then succeed early
              BRANCH_NAME=${CIRCLE_BRANCH}
              IS_BUGFIX=0; [[ ${BRANCH_NAME} == bug-fix-* ]] && IS_BUGFIX=1
              if [[ $IS_BUGFIX == 1 ]]
              then
                  exit 0
              fi
              # if friday or weekend then fail early
              IS_WEEKEND=0; [[ $(date +%u) -ge 5 ]] && IS_WEEKEND=1
              if [[ $IS_WEEKEND == 1 ]]
              then
                  echo "Deployment is skipped due to weekend code freeze, use bug-fix-* as branch name to bypass for critical bug fixes"
                  exit 1
              fi
              # if bank holiday in england and wales then fail
              HOLIDAYS=$(curl --silent --insecure --request GET https://www.gov.uk/bank-holidays.json | jq '."england-and-wales".events[].date')
              TODAY=$(date +%Y-%m-%d)
              IS_HOLIDAY=0; [[ " ${HOLIDAYS[@]} " =~ " ${TODAY} " ]] && IS_HOLIDAY=1
              if [[ $IS_HOLIDAY == 1 ]]
              then
                  echo "Deployment is skipped due to bank holiday code freeze, use bug-fix-* as branch name to bypass for critical bug fixes"
                  exit 1
              fi

workflows:

  normal-workflow:
    jobs:
      - code-freeze-check:
          filters:
            branches:
              ignore: main

  nightly: # nightly job ensures that status is adjusted correctly for each day
    jobs:
      - code-freeze-check
    triggers:
      - schedule:
          cron: "0 0 * * *"
          filters:
            branches:
              ignore: main
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment