This emulates Vercel for node apps if you are running a remote VPS for instance or any self hosted environment.
On your remote host, VPS, etc, do
git init --bare ~/myrepo.git
Now in your local host, add it to your desired repo origins and push
cd ~/myrepo
git remote add origin username@mydomain.com:~/myrepo.git
git add .
git commit -m "Changes"
git push -u origin master # or main, whichever you use
Back on your remote host, clone the bare repo
git clone myproject.git myproject
This will run every time you push changes. Because we will be running a node project from tmux, we will also add a tmux restarter for every time you push changes. This is an example for a typescript project.
cat << EOF > ~/myrepo.git/hooks/post-receive
#!/bin/bash
unset GIT_DIR
cd ~/myrepo
git pull origin master
tmux \
send-keys \
-t myrepo-session \
C-c \
'cd ~/myrepo' Enter \
'npx tsc --build --verbose >> ~/myrepo.log 2>&1 && node dist/index.js 2>&1 | tee -a ~/myrepo.log' Enter
EOF
To run your project:
cat << EOF > ~/start.sh
#!/bin/bash
tmux new-session -d -s myrepo-session
tmux \
send-keys \
-t myrepo-session \
'cd ~/myrepo' Enter \
'npx tsc --build --verbose >> ~/myrepo.log 2>&1 && node dist/index.js 2>&1 | tee -a ~/myrepo.log' Enter
EOF
chmod +x start.sh
./start.sh
EOF
If you want your project to run even on reboot, etc, create a new service thus
sudo cat << EOF > /etc/systemd/system/start-scripts.service
[Unit]
Description=My Startup Services
# Run after the network is up (optional, remove if no network needed)
After=network.target
[Service]
Type=forking
ExecStart=/home/username/start.sh
User=username
Restart=always
RestartSec=2
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable start-scripts.service
sudo systemctl start start-scripts.service
Just
tail -f ~/myrepo.log
To attach the session tmux attach -t myrepo-session
Once you are done and want to leave the session running Ctr+b, d
To list all running sessions tmux list-sessions
To kill a session tmux kill-session -t myrepo-session, however this will just respawn if you are running a service.