Makefile include env file
Solution 1
If you are using gnu make, what should work is to include the envfile
file, then
export the list of vars got from the same file:
#!make
include envfile
export $(shell sed 's/=.*//' envfile)
test:
env
Solution 2
I had a similar problem and landed on this thread. Another possible solution is to just do the make export command without any arguments:
include .env
export
This will export all make variables as environment variables. This behavior may or may not match all use cases, but it worked for me and didn't involve any shell scripting wizardry.
Solution 3
To complete previous anwswer from meuh
, if you would like env file to be dynamic, you could define a setup function.
- create
env/local-dev.env
,env/integ.env
file with some content - define a method to load this
define setup_env $(eval ENV_FILE := env/$(1).env) @echo " - setup env $(ENV_FILE)" $(eval include env/$(1).env) $(eval export sed 's/=.*//' env/$(1).env) endef
Then use it
localDevEnv: ## load env/local-dev.env environment $(call setup_env,local-dev) integEnv: ## load env/integ.env environment $(call setup_env,integ)
Solution 4
Preserves pre existing ENV vars
export $(shell [ ! -n "$(ENVFILE)" ] || cat $(ENVFILE) | grep -v \
--perl-regexp '^('$$(env | sed 's/=.*//'g | tr '\n' '|')')\=')
test:
echo $$FOO
To run
make ENVFILE=envfile test # bar
export FOO=foo
make ENVFILE=envfile test # foo
Solution 5
an example of how you can use .env values in Makefile
#!make
include .env
migrateinit:
migrate create -ext sql -dir mysql/migrations -seq int_schema
migrateup:
migrate -path mysql/migrations -database "mysql://${DB_USER_NAME}:${DB_PASSWORD}@tcp(${DB_HOST}:${DB_PORT})/${DB_NAME}" -verbose up 1
migratedown:
migrate -path mysql/migrations -database "mysql://${DB_USER_NAME}:${DB_PASSWORD}@tcp(${DB_HOST}:${DB_PORT})/${DB_NAME}" -verbose down 1
Related videos on Youtube
Michael Irwin
Updated on September 18, 2022Comments
-
Michael Irwin almost 2 years
I'm trying to include some env vars into a Makefile. The env file looks like:
FOO=bar BAZ=quux
Note there's no leading
export
to each env var. If I add the leadingexport
and justinclude
the env file in the Makefile, everything works as it should. But I need to keep the env vars sans leadingexport
. That prevents me from just usinginclude envfile
in the Makefile.I've also tried doing something like this:
sed '/^#/!s/^/export /' envfile > $(BUILDDIR)/env include $(BUILDDIR)/env
But doing that cause make to throw an error because the env file isn't there for including.
-
darnir over 8 yearsWelcome to Unix&Linux. Please state the question you have clearly. What is it exactly that you're trying to accomplish?
-
Michael Irwin over 8 yearsI'm trying to include a file that contains environment variable pairs into a Makefile so that they are available to the environment in which make is running.
-
-
Dawngerpony over 7 yearsThanks for this answer, it's brilliant! Although my version even works without the
export $(shell...
line. I just need theinclude
line. -
ClintM about 4 yearsIt's worth noting that if you have any
$
in the env file make won't like them and you'll have to escape them.PASS=Pass123$
will just show up asPass123
. This answer explains the caveats pretty well. stackoverflow.com/a/44637188/3384609 -
Beni Cherniavsky-Paskin about 4 yearsreference: gnu.org/software/bash/manual/html_node/…
-
Piotr about 4 yearsWorks like a charm!
-
Ebenezar John Paul over 3 yearsI can't verify this on make 4.2.1. without export after the include.
-
jlucktay almost 3 yearsFor better backwards compatibility, I'd recommend setting the
.EXPORT_ALL_VARIABLES:
special target instead of using theexport
directive, as described here in the manual: gnu.org/software/make/manual/html_node/…