2

Inside my makefile, I need to set a variable based on an environment variable called MY_SERVER_ENV.

I tried this:

gulp:=./node_modules/.bin/gulp
ifeq ($(MY_SERVER_ENV), 'prod')
    branch:=production
else
    branch:=deploy
endif


checkvariable:
    @echo $$branch

The output I get :

$ MY_SERVER_ENV=prod make checkvariable

$

The idea is to set the variable branch to production if the environment variable MY_SERVER_ENV equals prod, else to set branch to deploy.

And as you can see echo $$branch displays nothing

  • 1
    It says the error is at exit(1). It's not related to your variable. – Michael Homer Sep 7 '17 at 8:20
  • exit(1) is juste to stop the script, but as you can see the echo branch displays nothing – smarber Sep 7 '17 at 8:22
4

branch should be accessed as $(branch), and using

ifeq ($(MY_SERVER_ENV), 'prod')

will compare the value of $MY_SERVER_ENV with the literal string 'prod' (including the single quotes).

gulp:=./node_modules/.bin/gulp

ifeq "$(MY_SERVER_ENV)" "prod"
    branch:=production
else
    branch:=deploy
endif

help:
        @echo $(branch)

The ifeq can be on the following forms:

ifeq (arg1, arg2)
ifeq 'arg1' 'arg2'
ifeq "arg1" "arg2"
ifeq "arg1" 'arg2'
ifeq 'arg1' "arg2"

Reference: https://ftp.gnu.org/old-gnu/Manuals/make-3.79.1/html_chapter/make_7.html

  • Cool that's it :), any references please? – smarber Sep 7 '17 at 8:28
  • @smarber I changed it slightly after looking up the correct syntax. – Kusalananda Sep 7 '17 at 8:30
  • ifeq ("$(PROJECT_ENV)", "prod") this works for me – smarber Sep 7 '17 at 8:31
  • 1
    @smarber Good, but I think the quotes will be part of the strings compared. Since it's the same quoting on both sides, this does not matter. – Kusalananda Sep 7 '17 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.