liveBook · Manning

Git hook

As explained in Road to Kubernetes

A Git hook is an executable script that Git runs automatically in response to a repository event or command.

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

How Road to Kubernetes covers it

How deep it goes
Walks through creating, making executable, testing, and extending a post-receive script, but does not implement process restart.
Examples
Demonstrates Python and Node.js checkout directories, direct hook execution, git push vm main, and a Python requirements-installing hook.
What it assumes
Assumes an SSH-accessible virtual machine and Git repositories; teaches hook creation, executable permission, and checkout configuration step by step.
What you can do after
Build it from scratch: create an executable post-receive hook that checks out pushed code and installs Python dependencies.
How it connects
Links bare Git repositories, working trees, Git remotes, Python virtual environments, and pip in virtual-machine deployment.

Also explained in

Compare how 2 books explain git hook →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Road to Kubernetes. The section links point to the parts of the book it draws on.