简体   繁体   中英

npm run build script not working inside Git post-merge hook

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?

First, add a pwd; env|grep GIT 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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM