Files
oam/knowledge base/gnu userland/make.md
2024-03-29 23:46:23 +01:00

3.2 KiB

GNU Make

  1. TL;DR
  2. Load .env files in the Makefile
  3. Further readings
    1. Sources

TL;DR

# assign default values, but allow override
# usage: make override_me=new_value target
override override_me ?= default_value


# Load env files.
# If prefixed with '-' it does not error should any of the files not exist.
include .env
-include .env.local .env.extra


# export variables for all programs to see
# only exported in the commands' sub-shells
export

# export specific variables only
export override_me


# assign shell command output as values
command_output = ${shell command with args}


target_name: target_specific_variable = value
target_name: dash_instead_of_underscore = ${subst _,-,${override_me}}
target_name: previous_target required_file.ext
    ${info output string}
    echoed_command
    @quiet_command
    ${MAKE} next_target

previous_target:
    @quiet_command

next_target:


# conditionals
ifeq "${shell uname}" "Darwin"
expiration_date = ${shell date -v "+365d" "+%FT%TZ"}
else ifeq "${shell uname}" "Linux"
expiration_date = ${shell date -d "+1 year" "+%FT%TZ"}
endif
repository_name = $(shell basename $$(git rev-parse --show-toplevel))

override environment_id ?= dev
override tf_plan_file ?= ${environment_id}.tfplan
override tf_vars_file ?= ${environment_id}.tfvars

-include .env
export

pre-flight:
    ${info validating terraform's configuration…}
    @terraform fmt -recursive
    @terraform validate

ifeq "${destroy}" "1"
tf_destroy_switch = -destroy
endif

plan: pre-flight ${tf_vars_file}
    ${info planning ${environment_id}'s resources…}
    @terraform plan ${tf_destroy_switch} -var-file='${tf_vars_file}' -input=false -out='${tf_plan_file}'

apply: plan ${tf_plan_file}
    ${info applying ${environment_id}'s plan}
    @terraform apply '${tf_plan_file}'

Load .env files in the Makefile

Use one of those at the top of a Makefile to include and make available all variables in .env:

include .env
-include .env.local .env.extra
ifneq (,$(wildcard ./.env))
  include .env
  export
endif

ifneq + wildcard is a typical way to check a file exists.

include .env imports .env into the Makefile variables.
If prefixed with '-' (-include), it does not error nor warning should any of the included files not exist.

export without parameters exports all variables set until now.

Further readings

Sources