Pushing an application to a bare Git repository on a virtual machine does not put runnable files in the application directory: the bare repository has no working directory. A Git hook can perform that next step automatically. A post-receive hook is a Bash script that Git runs after a push. section 3.3 section 3.3.4
To deploy code with a post-receive hook:
- Create a destination directory for the application on the VM. section 3.3.4
- Create a file named post-receive in the bare repository’s hooks directory and make it executable. Git relies on that filename to run the hook. section 3.3.4
- Put a checkout command in the script, pointing its work tree at the application directory and its Git directory at the bare repository. section 3.3.4
Listing 3.13 Creating the post-receive file
export WORK_TREE=/opt/projects/roadtok8s/py export GIT_DIR=/var/repos/roadtok8s/py.git cat <<EOF > "$GIT_DIR/hooks/post-receive" #!/bin/bash git --work-tree=$WORK_TREE --git-dir=$GIT_DIR checkout HEAD -f EOF chmod +x "$GIT_DIR/hooks/post-receive"
This Python application hook checks out HEAD into /opt/projects/roadtok8s/py and makes the script executable. section 3.3.4
Test the script directly with bash before relying on a push, then push with git push vm main. A separate hook can check out the Node.js application. The hook can also run setup commands after checkout: the Python version installs packages from the checked-out requirements.txt using an absolute path to the virtual environment’s Python executable. Restarting application processes is a deployment goal here, but the shown hooks do not implement it. section 3.3.4 section 3.4.1
Listing 3.19 Updating the Git hook for the Python app
export WORK_TREE=/opt/projects/roadtok8s/py export GIT_DIR=/var/repos/roadtok8s/py.git cat <<EOF > "$GIT_DIR/hooks/post-receive" #!/bin/bash git --work-tree=$WORK_TREE --git-dir=$GIT_DIR checkout HEAD -f # Install the Python Requirements /opt/venv/bin/python -m pip install -r $WORK_TREE/src/requirements.txt EOF
The extended Python hook keeps the checkout command and adds pip installation, so a push updates both checked-out code and its dependencies. section 3.4.1

