I have a web project which I deploy to an ec2 instance simply by pushing new commits. I use the post-recieve git hook remotely to execute a shell-script which 'deploys' the project by checking it out into a production directory. The steps are, run npm install on the express app, npm install on the frontend (a create-react-app app), then run npm run build (which basically utilizes web-pack to build an optimized distribution folder from my node source code).
These steps are expensive and in many cases not needed. E.G. if all I did was update a Node component in srcs/components/ then npm run build should run, but npm install on the server and frontend shouldn't. If all I have done is added a comment to my express app, no scripts should run.
My currently server-side deploy script looks like this:
#!/usr/bin/env bash GIT_WORK_TREE=/home/ec2-user/absiteProd git checkout -f ### TODO: conditional NPM work pm2 restart index My question is then how can I use git (or grep, sed, awk, etc..) to reliably tell me when either /home/ec2-user/absiteProd/frontend/package.json, /home/ec2-user/absiteProd/server/package.json or anything in 'home/ec2-user/absiteProd/frontend/sources` has changed?
Currently I'm having some success with:
if `git log --stat -n 1` | grep --quite frontend/src/* ; then cd home/ec2-user/frontend npm run build fi But since this seems like such a common requirement in app deployment, I feel like there must be a simpler way?