I'm wondering if and how I can achieve the following in Github. I have a Git repository called Application
. I have a master branch with a folder called Deployments
- this folder needs to be empty at all times, with no files in it.
Developers then create a branch from master and it's typical that developers add files to this Deployments
folder and commit these files to their feature branch. Once developer is done with developing the feature, he will make a pull request to merge feature to master. For now, developer will manually clean up the content of the Deployments
folder before making the PR. I would like to automate this solution to ensure that when the developer makes the pull request, some workflow will get triggered which will either remove the content of the Deployments
folder or will notify the developer that there are files in that folder and merge cannot happen. Is this possible to achieve?
I read about pre-commit
git hooks and I created one as per bellow shell script. It works fine but only partially - if I locally checkout master and will add files to Deployments
folder, when I try to commit this, the commit will be rejected - that's great, however, if I'm on the feature branch I add the files and pushed those to feature branch (what is the expected developer workflow), I can then merge feature to master and files will end up in Deployments
on master what is something I want to prevent.
Here is the pre-commit git hook:
#!/bin/bash
DIR="Deployments"
BRANCH=$(git branch | grep \* | cut -d ' ' -f2)
if [ "${BRANCH}" = "master" ]; then
if [ "$(ls $DIR)" ]; then
echo "The directory: $DIR - is not Empty and cannot be merged to Master"
exit 1
else
echo "The directory: $DIR - is Empty"
fi
else
echo "on another branch: $BRANCH"
fi