Last Updated: February 25, 2016
·
994
· afterdesign

Makefile + value from variable based on string from cli.

This is kinda hacky so I don't know if this will fit anyone but for sure it's going to work for me.

In project(s) I'm working on we are using Makefile with bash to create easy to use deploy/automate/simplify life scripts.
We have so many tools running from cli (phpunit, uglifyjs, casperjs+phantomjs, phing, rsync and more) that Makefile + bash is kinda awesome for doing simple(ish) things.

We are also using many developer environments. Let's call them: development, testing, staging and production. Logic behind deployment on most of them is the same - do some stuff (like minimize JS) and run rsync or something similar.

So in our Makefile we have stuff like:

PRODUCTION_STATIC = 0.0.0.0
TESTING_STATIC = 0.0.0.0

and it would be awesome to deploy it (from jenkins or not) with one command like:
make deploy env=production

The problem is with the Makefile and getting that value from PRODUCTION_STATIC based on string production from $(env) var while running make from cli.

I was trying to do this today and I found one hacky/ugly/nifty (whatever you like) option to do what I want/need.

SHELL := bash

environment := $(shell echo $(env) | tr a-z A-Z)

PRODUCTION_HOST = 0.0.0.0
TESTING_HOST = 1.1.1.1

rsync_or_smth = \
    echo $($(environment)_HOST)

deploy:
    @$(call rsync_or_smth)

And now make deploy env=production is giving me 0.0.0.0. Just what I wanted.

If you have example how this can be done another way just post a comment !