git-hook 在启动 docker compose 时挂起

git-hook hangs when launching docker compose

我为我的 Laravel 应用制作了这个 post-merge git 挂钩:它基本上检查更改的文件并执行匹配命令(运行 迁移、编译资产等等)

#!/bin/bash
set -ex
CHANGED_FILES="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"

changed() {
    echo "$CHANGED_FILES" | grep -q ""
}


if [[ "$USER" == "root" || "$USER" == "ec2-user" ]]; then
    sudo chown -R www-data: htdocs
fi

if changed composer.lock; then
    docker-compose run --rm worker bash -c "composer install"
fi

if changed "migrations\|seeders"; then
    docker-compose run --rm worker php artisan migrate --force --seed
fi

if changed .blade.php$; then
    docker-compose run --rm worker php artisan view:clear
fi

if changed .php$; then
    docker-compose run --rm worker bash -c "php artisan cache:clear && php artisan route:clear"
fi

if changed "yarn.lock$\|.js$\|.scss$"; then
    docker-compose run --rm node bash -c "yarn && yarn prod"
fi

if changed httpd.conf; then
    docker-compose up -d --force-recreate apache
fi

docker-compose restart worker

exit 0

如果我手动启动挂钩 ./.git-hook/post-merge(它位于非常规文件夹中,因此挂钩本身已提交)它会起作用 但是当它被 git 执行时,它总是在创建容器后挂起。 这只发生在 AMI linux 它在我的 mac

上工作正常

输出看起来像这样

$ git pull
remote: Enumerating objects: 21, done.
remote: Counting objects: 100% (21/21), done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 13 (delta 8), reused 0 (delta 0), pack-reused 0
Decompressione degli oggetti in corso: 100% (13/13), 1.44 KiB | 490.00 KiB/s, fatto.
Da bitbucket.org:[my-private-repo]
   ec60a04..def0d31  master     -> origin/master
Aggiornamento di ec60a04..def0d31
Fast-forward
 .git-hooks/post-merge                         |  2 +-
 htdocs/app/Http/Controllers/ApiController.php | 25 ++++++++++++++++++++++---
 2 files changed, 23 insertions(+), 4 deletions(-)
++ git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD
+ CHANGED_FILES='.git-hooks/post-merge
htdocs/app/Http/Controllers/ApiController.php'
+ [[ ec2-user == \r\o\o\t ]]
+ [[ ec2-user == \e\c\-\u\s\e\r ]]
+ sudo chown -R www-data: htdocs
+ changed composer.lock
+ echo '.git-hooks/post-merge
htdocs/app/Http/Controllers/ApiController.php'
+ grep -q composer.lock
+ changed 'migrations\|seeders'
+ echo '.git-hooks/post-merge
htdocs/app/Http/Controllers/ApiController.php'
+ grep -q 'migrations\|seeders'
+ changed '.blade.php$'
+ echo '.git-hooks/post-merge
htdocs/app/Http/Controllers/ApiController.php'
+ grep -q '.blade.php$'
+ changed '.php$'
+ echo '.git-hooks/post-merge
htdocs/app/Http/Controllers/ApiController.php'
+ grep -q '.php$'
+ docker-compose run --rm worker bash -c 'php artisan cache:clear && php artisan route:clear'
Creating www_worker_run ... done

Creating www_worker_run ... done 是我看到的最后一件事,然后输出停止(我等了几分钟然后放弃了)

感谢@fredrik,我获得了更多信息: 挂起的命令是 docker-compose run --rm worker bash -c 'php artisan cache:clear && php artisan route:clear' 但是我可以 运行 它在 2.5 秒内从 shell 开始就好了

$ docker-compose run --rm worker bash -c 'php artisan cache:clear && php artisan route:clear'
Creating www_worker_run ... done
Application cache cleared!
Route cache cleared!

正如@fredrik 指出的那样,问题是 git 没有交互式终端并且 docker-compose 命令失败

-T 选项仅适用于此。

来自 docker run -h:

-T Disable pseudo-tty allocation. By default `docker-compose run` allocates a TTY.