3

I can run "npm run prod". I put the same command in a deploy.sh file. Again, I can still run the deploy script using ./deploy.sh, and it works. But the same deploy script could not run when the git hook happens.

"npm i" works both in the terminal, deploy script, and using git hook.

deploy.sh script is as follows:

#!/bin/sh

php artisan down
git pull origin $1
composer install --no-interaction --prefer-dist --optimize-autoloader --no-dev
php artisan migrate --force
php artisan cache:clear
php artisan route:cache
php artisan config:cache
npm ci

# everything works fine, but the line below
npm run production

php artisan up

And the weird thing is, in the terminal, when I run ./deploy.sh everything works, without any problem. but when the deploy script gets called using a GitHub webhook, it throws these errors:

terminal log

log file

So, npm run prod works in terminal, works when I run the deploy script by ./deploy.sh in the terminal, but not working when the git hook calls the laravel app to run the deploy script.

This is how I run the deploy.sh file using laravel:

$process = new Process(['./deploy.sh']);

$process->setWorkingDirectory(base_path());

$process->run(function ($type, $buffer) {
    Log::info($buffer);
});

What I have tried already:

using cross-env: not working. using rm -rf node_modules & npm cache clean --force & npm i: not working.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
behz4d
  • 1,819
  • 5
  • 35
  • 59
  • 1
    Try changing `mix --production` to `node_modules/.bin/mix --production` in `package.json` – cbr Aug 03 '21 at 15:27
  • 1
    Add also [`set -e`](https://stackoverflow.com/q/19622198/996081) on the line after `#!/bin/sh` - that will make the script stop at the first error. It's possible `npm ci` has failed and left the deploy machine without installed npm dependencies, but the script has continued regardless. – cbr Aug 03 '21 at 15:37
  • @cbr npm ci works just fine – behz4d Aug 04 '21 at 07:12
  • @cbr I made both changes, still not working – behz4d Aug 13 '21 at 11:08
  • @behz4d Not sure what is happening, but just wanted to suggest a great tool for deployments, if you haven't tried it already - https://deployer.org/. Please note that this is not linked with the Git Hooks. – bjjn Aug 13 '21 at 14:02
  • 2
    So your script works fine when you try it with CLI, but not when triggered from Github Webhook. Check if the Linux user that will make the command after from webhook, has the same permission as your shell user. Can you check by executing the command/script yourself but from the Linux user github will "use"? Can you test something like "which npm" or "locate npm" to compare with you running those commands in cli? It may be an environmental issue, like path, permissions... As your run from Laravel, PHP User may not have enough permission compared to your shell user. Also try using verbose on npm – Mtxz Aug 13 '21 at 18:28
  • @Mtxz The weird thing is that the hook can run `npm ci` just fine, but not `npm run prod` :/ – behz4d Aug 14 '21 at 06:26
  • 3
    @behz4d true that is a bit strange. Still, npm run production will need access to some assets, packages like node-sass or things like this. I agree that the npm/node path may not be the issue. I would try to run npm run production with verbose to pinpoint the real issue (access denied somewhere, not founds, missing package...) as the only difference to me is the Linux user running the script. Maybe try sudo (for tests purposes only) to see if it's better. – Mtxz Aug 14 '21 at 13:04
  • 3
    As Mtxz said, you running `./deploy.sh` is not the same as the webhook running it as it is a different user with a different environment. The ENOENT error suggests either that you are referencing a file that the webhook user can not access or that you have a relative path somewhere that is invalid in the context of the PWD that the webhook is using. Assuming the webhook is running as user `webhook`, try this: `sudo -u webhook ./deploy.sh` to determine if it's related to the user. `EDIT:` Find where you have `sh` and replace it with `/bin/sh`, the webhook probably is clearing the path variable. – leitning Aug 16 '21 at 17:51
  • @Mtxz the githook is calling my deploy endpoint which runs the deploy.sh file using `Symfony Process`, in this scenario, what is the user that is running the deply.sh file? – behz4d Aug 24 '21 at 12:26
  • @behz4d it could be the user running your PHP server. Usually www-data or nginx-fpm, but can depends your setup. – Mtxz Aug 24 '21 at 21:13

2 Answers2

0

your code is not reproducible on my end. however my hypothesis is the laravels' log:info $buffer's stdout is clashing with master process' logging stdout. disable logging or change logging level and try the above procedure. it should work.

nikhil swami
  • 2,360
  • 5
  • 15
0

Are you running the correct command? You talk about running npm run prod locally but in your deploy.sh file it's npm run production?