1

I'm trying to run an npm script inside a post-merge Git hook, but the npm script isn't working. The hook is being triggered on a git pull.

Here is what my post-merge file looks like:

#!/bin/sh
/home/jt/nvm/versions/node/v18.12.0/bin/npm run build --prefix /home/jt/project/frontend
touch /home/jt/project/backend/backend/wsgi.py

The post-merge file is correctly located in the hooks directory within the .git directory:

23:44 ~/project/.git/hooks (hosting)$ tree
.
├── applypatch-msg.sample
├── commit-msg.sample
├── fsmonitor-watchman.sample
├── post-merge
├── post-update.sample
├── pre-applypatch.sample
├── pre-commit.sample
├── pre-merge-commit.sample
├── pre-push.sample
├── pre-rebase.sample
├── pre-receive.sample
├── prepare-commit-msg.sample
└── update.sample
0 directories, 13 files
23:44 ~/project/.git/hooks (hosting)$ 

When I run the npn run build command in a shell script, it runs perfectly.

23:38 ~/project (hosting)$ nano test.sh
23:38 ~/project (hosting)$ ls
LICENSE  README.md  backend  frontend  test.sh
23:38 ~/project (hosting)$ source test.sh
> frontend@0.0.0 build
> vite build
vite v3.2.5 building for production...
✓ 241 modules transformed.
../backend/static/assets/react.35ef61ed.svg                   4.03 KiB
../backend/static/assets/map-pin-icon-default.2555d711.svg    0.22 KiB
../backend/static/assets/map-pin-icon-selected.0e940bf1.svg   0.23 KiB
../backend/static/assets/map-pin-icon-home.bc73a24c.svg       0.23 KiB
../backend/static/assets/bg_image.90202fe6.png                1671.34 KiB
../backend/static/index.html                                  1.00 KiB
../backend/static/assets/index.a062f1f9.css                   192.35 KiB / gzip: 27.36 KiB
../backend/static/assets/index.fd1f092a.js                    263.23 KiB / gzip: 87.69 KiB
../backend/static/assets/index.fd1f092a.js.map                1010.80 KiB
23:39 ~/project (hosting)$ 

Why is the npm run build script not working inside the git post-merge hook?

joembarron
  • 141
  • 7

1 Answers1

0

First, add a pwd; env|grep GIT in order to validate:

  • your post-merge script is actually called after a git pull
  • from where it is called
  • with what GIT_DIR/GIT_WORKTREE environment variable.

As in this script, try and unset/reset GIT_DIR.

REPO_DIR="/path/to/your/local/repo"
cd $REPO_DIR || exit
unset GIT_DIR
...
# rest of your script

The OP joepianoblaster adds in the comments:

I added the git post-merge hook in my local repository in the hooks/ folder within the .git/ folder.
The git pull is being called using GitPython from a Django view function.

@api_view(['POST']) 
def update_server(request):     
  repo = git.Repo('/home/jt/project/.git')
  origin = repo.remotes.origin     
  origin.pull()     
  return Response('Updated Python Anywhere successfully', status=200)
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Would the pwd; added to the post-merge hook show up in server logs or directly on the command line? – joembarron Jan 03 '23 at 01:06
  • Command-linr: it is a client side hook – VonC Jan 03 '23 at 01:22
  • I added this into the git post-merge hook but didn't see anything on the command line. I'm using GitPython to actually pull from the remote repository, so I'm thinking that could potentially be the problem. – joembarron Jan 03 '23 at 01:26
  • @joepianoblaster Did you add that in your local repository? or on the server? – VonC Jan 03 '23 at 13:54
  • I added the git post-merge hook in my local repository in the hooks folder within the .git folder. The git pull is being called using GitPython from a Django view function. `@api_view(['POST']) def update_server(request): repo = git.Repo('/home/jt/project/.git') origin = repo.remotes.origin origin.pull() return Response('Updated Python Anywhere successfully', status=200)` – joembarron Jan 05 '23 at 03:41