2

Im trying to make a simple auto deploy using git hooks post-receive and i found this link.That link gave me idea and modified it myself. It is working well when the BRANCH Variable is "master". but when i am changing it to other branch, let say "phase2", it always stays in "master" branch when the time of running the artisan commands. So my cache route came from master branch.

This is my post-receive hook file :

#!/bin/sh
TARGET="/usr/local/apache2/htdocs/project_uat"
GIT_DIR="/home/user0001/www/project_name/.git"

BRANCH="phase2" # I want this changable to phase3,phase4 or what ever branch

while read oldrev newrev ref
do
        # only checking out the master (or whatever branch you would like to deploy)
        if [[ $ref = "refs/heads/${BRANCH}" ]];
        then
                echo "Ref $ref received. Deploying ${BRANCH} branch to UAT..."
                git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f
        else
                echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
        fi
done

cd /usr/local/apache2/htdocs/project_uat
php artisan cache:clear
php artisan config:cache
php artisan route:cache
php artisan optimize

When i try to navigate to my target dir. It stays in phase2 branch but theres is a lot of changes when i try to "git status". These changes came from master.

Basically, all i wanted is to automate the deployment everytime i "git push" from my local without logging in to remote server just to run the following commands:

  • cd to/project/path
  • git pull origin phase2
  • php artisan cache:clear
  • php artisan config:cache
  • php artisan route:cache
  • php artisan optimize

And the only think i can make it is trough GIT HOOKS

Romnick Susa
  • 1,279
  • 13
  • 31
  • Check [https://stackoverflow.com/questions/3885850/git-not-removing-files-when-switching-branch](https://stackoverflow.com/questions/3885850/git-not-removing-files-when-switching-branch) – ljubadr Oct 24 '17 at 03:34

2 Answers2

2

After struggling a lot and with the help of @VonC

I finalize my hook as simple as:

#!/bin/sh
unset $(git rev-parse --local-env-vars)
cd /usr/local/apache2/htdocs/project_uat

env -i git reset --hard
env -i git clean -f -d
env -i git pull
php artisan cache:clear
php artisan config:cache
php artisan route:cache
php artisan optimize

I don`t know the downside yet.

Romnick Susa
  • 1,279
  • 13
  • 31
1

As mentioned, you could add, after a checkout:

git reset --hard
git clean -f -d 

But don't forget you also have another option: multiple wortrees (one per branch): you could change folder (and server the folder matching your branch) without having to worry about cleaning up/re-compiling because of files of a previous branch.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Sorry i am confused, should i put the reset and clear after the `git --work-tree=`? – Romnick Susa Oct 24 '17 at 05:52
  • @RomnickSusa No: it is one solution OR the other, not both: having multiple worktrees means you only have to change folder in order to change branch, you don't have to reset/clean. If you have only one folder where you switch branches, then you need reset/clean. – VonC Oct 24 '17 at 05:55
  • Oh! i get it. thank u, but for now, i am looking for a way to make an automated deployment. So i dont have to log on remotely everytime i push an update – Romnick Susa Oct 24 '17 at 06:02
  • @RomnickSusa Exactly: do your checkout in the right folder (the one for the right branch), and you won't have to log on at all. – VonC Oct 24 '17 at 06:12
  • based on my code above, i dont know why i doesnt checkout to the rigth branch, it is always in master branch. I have edited my post. hope it helps. – Romnick Susa Oct 24 '17 at 06:13
  • @RomnickSusa Can you try and add `${BRANCH}` to your checkout command? – VonC Oct 24 '17 at 06:20