Git hook to restrict commit/push depending on the time of the day
Platform Notice: Cloud Only - This article only applies to Atlassian products on the cloud platform.
Summary
This article provides an example of how to prevent a user from pushing to Bitbucket Cloud at a certain time of the day. For example, outside business hours.
Solution
While there are no native features to achieve that outcome, users can use local Git hooks to configure it. In the example below we are creating a hook to block pushes outside business hours. You can achieve the same at the commit level by changing the file from pre-push to pre-commit.
Navigate to the local repository folder.
Navigate to the Git hooks folder.
1
$ cd .git/hooks
Create the pre-push file and apply execution permission to it.
1
$ touch pre-push && chmod +x pre-push
Populate the pre-push file with your bash script. Below is a quick example of a script that checks if the push will go within business hours.
1 2 3 4 5 6 7 8 9 10
#!/bin/sh # Get the current time CURRENT_TIME=$(date +%H:%M) # Check if the time is within the allowed range if [[ $CURRENT_TIME <08:00 || $CURRENT_TIME >17:00 ]]; then echo "Push not allowed outside of 8AM-5PM UTC." exit 1 fi
Please note that the above snippet is just an example and you need to review and adapt it to your needs.
An error message similar to the below one will be sent to the user if a push attempt occur outside the allowed hours:
1
2
3
$ git push │
│Push not allowed outside of 8AM-5PM UTC. │
│error: failed to push some refs to 'bitbucket.org:workspace/repository.git'
Reference: Git hooks
Was this helpful?