PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 21, 2022

[FIXED] How can I load environment variables from another file in my makefile?

 August 21, 2022     bash, environment-variables, makefile, sh, unix     No comments   

Issue

I want to set environment variables when I run my makefile commands however ideally the variables will be stored in another file.

./Makefile

_init_dev:
    . ./config/dev-env

diff-dev: _init_dev
    cdk diff

deploy-dev: _init_dev
    cdk deploy

./config/dev-env

export HELLO_CDK_DEPLOY_ENVIRONMENT=dev

First I tried to use source to load in the vars however this failed with the message:

make: source: Command not found

I understand this is because make runs sh and not bash.

I then tried to add . in front of my config file to attempt to load the variables that way.

Neither approach has worked. When I run my cdk command I can see from the output the environment variable has not been set.

Can anyone advise on a way to get this to work?


Solution

Every recipe line in a makefile is run in its own shell. Environment variables modify the current shell, but those changes go away when the shell exits. So, it's completely impossible for one recipe to set environment variables that are then visible inside a different recipe.

If you want these variables available you must source them in each recipe line, like this:

diff-dev:
        . ./config/dev-env && cdk diff

deploy-dev:
        . ./config/dev-env && cdk deploy

You can put this into a variable, like:

CDK = . ./config/dev-env && cdk

diff-dev:
        $(CDK) diff

deploy-dev:
        $(CDK) deploy

Alternatively if your dev-env file is a simple-enough format (such as the one you show) that it works as both a makefile AND a shell script, you could include it:

include ./config/dev-env

diff-dev:
        cdk diff

deploy-dev:
        cdk deploy

But this will only work for very limited contents of dev-env (basically simple assignment of variables to static strings).



Answered By - MadScientist
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing