Git hook – checking branch name

Have you ever faced situation where you made wrong commit message and missed that and been embarrassed because some typos ?

Thankfully there are ways to avoid this. Called Git hooks.

In order to check commit message you can put script – content below into
.git/hooks/prepare-commit-msg

#!/bin/sh

RED='\033[0;31m'
NC='\033[0m' # No Color


get_initials () { array=( $(id -F) ); echo ${array[1]:0:1}${array[0]:0:1}; };

INITIALS=$(get_initials)
BRANCH="$(git rev-parse --abbrev-ref HEAD)"


if ! grep -E "$INITIALS - $BRANCH - " $1; then
    echo "Your commit message should contain initials - branch name - description. Like below"
    printf "${RED}KP - ${BRANCH} - added new fixtures, changes on view${NC}\n"
    exit 1
fi

So if we try to execute following command:

git commit -m "AD - 32 - test of git hook"

We will see

output when on wrong branch / wrong initials

Because my initials are KP and branch i should use is XX.
So when I execute proper command:

git commit -m "KP - XX - added new fixtures, changes on view"

I should see:

KP - XX - added new fixtures, changes on view
[XX 8f80226] KP - XX - added new fixtures, changes on view
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 xx

Docker image templating with Jinja

Why to template

Sometimes there is a need to create Dockerfiles dynamically.
Perhaps it’s necessary as part of your CI/CD flow. So let’s try to create python script which is templating Dockerfile.

What is Jinja

Jinja is templating system widely used for templating HTML pages. Typical use case when we use Jinja is situation we want to create web app and render dynamic html pages with frameworks (like flask).
But nobody said you cannot use Jinja for creating dynamic Dockers or any other text files 🙂

Installation / requirements.txt

Only one non std lib package we just need is jinja2. Of course after you install jinja2 from pypy it will install MarkupSafe as well (because of dependencies).

pip install jinja2

So at the moment of writing this article Jinja2 (August 2019) is at 2.10 version

Jinja2==2.10.1
MarkupSafe==1.1.1

Template

In a template file called Dockerfile_template we will be defining places where we will put content dynamically.

  • Typically each code block is open with “{{” and closed with “}}” inside of brackets you need to put variable name ex. base.
  • In case if we have many elements we need to iterate over using {% for and {% endfor %}
FROM {{ base }}
LABEL maintainer="{{ maintainer }}"

RUN yum install -y  {% for pc in packages %}{{pc}} {% endfor %}

So in our case we will dynamically say

  • what base image to use,
  • who is maintainer of docker file,
  • which additional packages to install in container

How to run script

We need to set couple parameters: base-img, tag, maintainer.
As optional we can specify packages

usage of the script

So lets execute

python create_dockerfile.py --base-img centos -n centos_base -m "Kamil Pazik pazik.kamil@gmail.com" -p tcpdump nmap
execution of the script

The script created Dockerfile

FROM centos
LABEL maintainer="Kamil Pazik pazik.kamil@gmail.com"

RUN yum install -y  tcpdump nmap

Repository