From c6a1bc49411bbccc5dd9a965019a524b3481d2f5 Mon Sep 17 00:00:00 2001 From: Michele Cereda Date: Thu, 29 Feb 2024 22:14:38 +0100 Subject: [PATCH] chore(kb/make): dump findings from usage --- knowledge base/gnu userland/make.md | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/knowledge base/gnu userland/make.md b/knowledge base/gnu userland/make.md index 4aaf18c..4aa24e1 100644 --- a/knowledge base/gnu userland/make.md +++ b/knowledge base/gnu userland/make.md @@ -1,8 +1,9 @@ # GNU Make 1. [TL;DR](#tldr) +1. [Load .env files in the Makefile](#load-env-files-in-the-makefile) 1. [Further readings](#further-readings) -1. [Sources](#sources) + 1. [Sources](#sources) ## TL;DR @@ -79,15 +80,31 @@ apply: plan ${tf_plan_file} @terraform apply '${tf_plan_file}' ``` +## Load .env files in the Makefile + +Use this at top of a Makefile to export all variables in `.env`: + +```makefile +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.
+`export` without parameters exports all variables set until now. + + + ## Further readings - [Conditional syntax] -## Sources - -All the references in the [further readings] section, plus the following: +### Sources - [Makefile variable initialization and export] +- [How to load and export variables from an .env file in Makefile?] [conditional syntax]: https://www.gnu.org/software/make/manual/html_node/Conditional-Syntax.html - -[further readings]: #further-readings - +[how to load and export variables from an .env file in makefile?]: https://stackoverflow.com/questions/44628206/how-to-load-and-export-variables-from-an-env-file-in-makefile#70663753 [makefile variable initialization and export]: https://stackoverflow.com/questions/2838715/makefile-variable-initialization-and-export