Reputation: 2166
I have written a post-commit hook which requires creating the virtual environment for the python script to run when a commit happens.
Following is my script:
#!/bin/bash
# Teamcity-build trigger
echo "Executing post-commit hook"
BASEDIR=$(dirname $(readlink -f $0))
VENV=venv
ACTIVATE=$VENV/bin/activate
STATUS=false
CIDIR=teamcity
# Username
machine=$(uname -n)
echo "Notifying Teamcity Server to execute a Build on " $machine
# Logic to check if commit is done or merged to a particular branch / master branch
cd $BASEDIR
# Change directory to parent directory, since current directory is `.git/hooks/`
cd ../..
# Go in teamcity directory
cd $CIDIR
# Check if venv folder exists, if it does not then create it
if [ ! -d "$VENV" ]; then
# Control will enter here if $VENV doesn't exist.
virtualenv -p python $VENV
STATUS=true
fi
# Source virtual environment
source $ACTIVATE
# Install required dependencies if not yet installed
if [ "$STATUS" = true ]; then
# if status is true, means need to install all the required libraries
pip install --upgrade pip
pip install -r requirements.txt
fi
# check current git branch
# $(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
BRANCH=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
if [ "$BRANCH" = "rahul" ]; then
# if branch is master, then only proceed.
# Start processing teamcity jobs
python last_dir_processed.py
else
echo "Current Branch $BRANCH, Nothing to execute."
fi
# Deactivate virtual environment
deactivate
On line 43,
BRANCH=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
the script tries to know name of the current branch.
But it gives error as:
fatal: Not a git repository: '.git'
I chacked the folder path with pwd command before that line and it shows .git
folder exists.
Also, When I ran following command manualy, it worked:
git rev-parse --symbolic-full-name --abbrev-ref HEAD
This gave me a current branch name.
EDIT: Following is my directory structure:
--root_dir
--teamcity
-last_dir_processed.py
--.git
--hooks
-post-commit
So, ideally the script should execute properly, but still it gives the error. Not sure what could be the reason.
Upvotes: 2
Views: 509
Reputation: 1324937
Just to be sure, set GIT_DIR
and GIT_WORK_TREE
environment variables in order to be sure any git
command runs in the right context:
cd ../..
GIT_DIR=$(pwd)/.git
GIT_WORK_TREE=$(pwd)
Upvotes: 1