liveBook · Manning

Post-receive hook

As explained in Road to Kubernetes

A post-receive hook is a Git server-side hook that runs after received references have been updated by a push.

A post-receive hook is a script that Git runs on a server after a push updates its repository. In a VM deployment, it can turn that push into an updated application: check out the code, install dependencies, then restart the running process. The remote here is a bare repository, so the application needs a separate directory from which to run. section 3.3.4 section 3.5.2

Set up the first deployment step:

  • Create a directory for the application, such as /opt/projects/roadtok8s/py. section 3.3.4
  • Create an executable file named post-receive inside the bare repository’s hooks directory. Git requires that exact filename to run this hook. section 3.3.4
  • In the script, use git checkout with an explicit work tree and Git directory to put the pushed code in the application directory. Push from the local computer, then check that the files appeared on the VM. 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"

The initial Python hook shows how WORK_TREE and GIT_DIR point checkout at different directories: one for runnable files, one for repository metadata. section 3.3.4

Checking out files is only the start. Extend the Python hook to install packages from requirements.txt with the virtual environment’s Python executable; extend the Node.js hook to run npm install in its application directory. Absolute paths in the Python command make its locations explicit. You can run either hook with bash to verify the install command before relying on a push. section 3.4.1 section 3.4.2

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 updated Python hook places dependency installation immediately after checkout, so each push refreshes both code and required packages. section 3.4.1

Finish the push-to-deployment path

Once Supervisor manages the applications, add a supervisorctl restart command after dependency installation in each hook. A Python push then checks out code, installs its requirements, and restarts the Python process; the Node.js hook follows the same sequence with npm install and its own process. The restart comes last so the managed application uses the newly deployed code and dependencies. section 3.5.2

This is a practical way to automate a manual VM deployment, but the hook does not replace process management: Supervisor is configured separately to keep the applications running. section 3.5.2

How Road to Kubernetes covers it

How deep it goes
Walks through creating an executable hook, adding checkout and install commands, verifying execution, and appending restarts; does not explore Git hook internals.
Examples
Uses separate Python and Node.js hook scripts that check out applications, install dependencies, and restart Supervisor-managed processes.
What it assumes
Assumes a VM with bare Git repositories and application directories; defines the hook and shows how to create its executable script.
What you can do after
Build it from scratch to update application files and dependencies on each push and restart Supervisor-managed applications.
How it connects
Connects bare repositories and Git checkout to Python virtual environments, npm dependency installation, and Supervisor process management.

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.