How to make a background bash script pause while an npm build runs?

I’m setting up a new method of deploying my personal react website, which is using vite for bundling. My idea is:

  1. I push un-bundled changes from local to remote repo
  2. a web hook informs my server (which has a listening php script)
  3. the php script triggers a bash script that: 1) runs an npm install, 2) runs an npm run build and 3) runs an rsync to copy the build contents to the web’s live folder

when I run the bash script manually via PuTTY, it works fine. I can watch while vite’s build output scrolls in the terminal, and eventually the rsync is triggered. But when the bash script is run via the php script, it does not seem to wait for the build (and perhaps even the install) to complete. It just copies whatever is in the build folder over to the live site (which is always the result of the last build, because the current build has not completed yet).

Is there a way to make the bash script wait for the install and the build to complete? I’ve tried adding a pause 120 after the install and build statements, and I’ve tried chaining the commands, for example something like

npm install && npm run build && rsync ......

neither accomplishes the goal when the script is run via php, but both work perfectly when I run manually..

The relevant line from the php script that calls the bash script is:

$commandOutput = shell_exec("/bin/bash ./sync.sh --repo=" . $hook["repo"] . " --branch=" . $hook["branch"] . " --subdomain=" . $subdomain);

$hook["repo"] is the name of the repo, $hook["branch"] is the current branch, and $subdomain is the subdomain being worked on (I have multiple set up).

The bash script starts out by picking up the repo, branch, and subdomain and then makes the various git and npm commands. The relevant lines are:

if [[ ${repo} && ${branch} && ${subdomain} ]]; then
  git -C /path/"${repo}"/ checkout "${branch}"
  git -C /path/"${repo}"/ fetch
  git -C /path/"${repo}"/ pull origin "${branch}"
  npm --prefix /path/"${repo}"/ install
  npm --prefix /path/"${repo}"/ run build;
  rsync -auv /path/"${repo}"/htdocs/* /path/"${subdomain}"/htdocs;
else
  echo "repo, branch and subdomain were not provided, I will not do anything.";
fi