使用git hooks + laravel artisan命令创建简单的自动部署

时间:2017-10-24 02:33:40

标签: bash laravel githooks artisan

我试图使用git hooks post-receive进行简单的自动部署,我找到了这个link。这个链接给了我一些想法并自己修改了它。 当BRANCH变量为“主”时,它运行良好。但是当我将其更改为其他分支时,让我们说“阶段2”,它在运行工匠命令时始终保持在“主”分支中。所以我的缓存路由来自master分支。

这是我的收件后挂钩文件:

#!/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

当我尝试导航到我的目标目录时。它保持在phase2分支,但是当我尝试“git status”时,有很多变化。这些变化来自大师。

基本上,我想要的是每当我从本地“git push”而无需登录到远程服务器时自动执行部署只是为了运行以下命令:

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

我认为唯一的想法是通过GIT HOOKS

2 个答案:

答案 0 :(得分:1)

如上所述,您可以在结帐后添加:

git reset --hard
git clean -f -d 

但不要忘记你还有另一种选择:multiple wortrees(每个分支一个):你可以更改文件夹(和服务器匹配你的分支的文件夹),而不必担心清理/重新编译,因为上一个分支的文件。

答案 1 :(得分:1)

在@VonC

的帮助下挣扎了很多次

我最终确定我的钩子如下:

#!/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

我还不知道其缺点。