Makefile

September 4, 2021 1 minute, 17 seconds

You can find some of my Makefiles I use for different purposes in my toolbox repository

DOCKER_GID=$(shell getent group docker | cut -d: -f3)

    $(eval NAME=$(shell hostname))
    echo "Hostname is $(NAME)"

Prefixing with @ does not print out the command itself

    @echo "Spinning up the cluster"
    kind create cluster --config kind.yaml

define get_conf
$(shell yq -r .$(1) conf.yml)
endef
NAME:=$(call get_conf,name)

BUILDARGS=--build-arg uid=$(UID) --build-arg gid=$(GID)
BUILDARGS:=$(BUILDARGS) --build-arg docker_gid=$(DOCKER_GID)
BUILDARGS:=$(BUILDARGS) --build-arg name=$(NAME)

Concat string

    EDITOR += -c 'set syntax=yaml' -

Simple if

ifeq ($(MOUNT_AWS_FOLDER), true)
MOUNT:=${MOUNT} -v $$HOME/.aws/:/home/${NAME}/.aws
endif

Bash oneliner

    @if [ "$(TARGET)" == "" ]; then echo "Missing target variable - run make targets for possible values"; exit 1; fi

Search for string

EDITOR:=$(shell type -p nvim || echo vim)
ifneq (,$(findstring vim,$(EDITOR)))
    EDITOR += -c 'set syntax=yaml' -
else
    EDITOR:=less
endif

    @while [[ "$$(kubectl get -n kube-system deployment/sealed-secrets-controller -o json | jq '.status.readyReplicas')" != "1" ]]; do sleep 5; done

.PHONY: help

help: ## This help.
    @awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

.DEFAULT_GOAL := help

This allows something like the following where everything after ## is printed as help message

build: ## Build the container
    @echo 'Building image'
    docker build $(BUILDARGS) -t $(NAME) .