Skip to main content
  1. Knowledge Base/
  2. Programming/

Makefile

Table of Contents

My Makefiles
#

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

Run shell commands
#

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

Assign output of shell commands to a variable
#

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

echo
#

Prefixing with @ does not print out the command itself

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

User defined function
#

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

Concat variables
#

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' -

IF
#

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

Wait for something
#

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

Help message
#

.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) .