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

标签 bash laravel githooks laravel-artisan

我尝试使用 git hooks post-receive 进行简单的自动部署,我发现了这个 link .那个链接给了我想法并自己修改了它。 当 BRANCH 变量为“master”时,它运行良好。但是当我将其更改为其他分支时,比如说“phase2”,在运行 artisan 命令时它始终保留在“master”分支中。所以我的缓存路由来自master分支。

这是我的接收后 Hook 文件:

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

当我尝试导航到我的目标目录时。它停留在第二阶段分支,但当我尝试“git status”时,有很多变化。这些改变来自master。

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

  • cd 到/项目/路径
  • git pull origin 第 2 阶段
  • php artisan 缓存:清除
  • php artisan 配置:缓存
  • php artisan 路由:缓存
  • php artisan 优化

我唯一认为可以做到的就是通过 GIT HOOKS

最佳答案

经过一番努力并在@VonC的帮助下

我完成我的钩子(Hook)很简单:

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

我还不知道缺点。

关于bash - 使用 git hooks + laravel artisan 命令创建简单的自动部署,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46901295/

相关文章:

git - 禁止删除 git 中的 master 分支

linux - 使用 UMASK 创建权限为 755 的 unix 文件

bash - 不会记住在 while 循环内修改的变量

linux - : not a valid identifier bash export says

git - 找不到 git 更新后 Hook 中的 Bash 命令

Git 钩子(Hook),接收后循环通过提交

bash - 在 bash 中转义双引号未按预期工作

laravel - Laravel Blade 循环中 undefined variable $loop

php - Laravel Eloquent 和查询生成器 "with (nolock)"

php - 使用 Laravel 在分组和平均时查询表