0

I have variables set in an env file called by my Makefile. I would like to choose a set variable based on an environment condition. Is there a way to nest two variables to make a new variable?

Example env file

FILE_VERSION_11=11.0.4

Execution

VER=11 make build

Example Makefile (Not working obviously)

.PHONY: all
.PHONY: build
.PHONY: clean
.PHONY: help
.PHONY: test

include env

build: ## Build
build:
  docker image build --pull -t container:file-version-$(VER) \
  --build-arg FILE_VERSION=$(FILE_VERSION_$VER)

Hoping to have the $(FILE_VERSION) variable now be rewritten as $(FILE_VERSION_11) so that 11.0.4 can be passed to the docker build via the FILE_VERSION_11 env variable.

maskeda
  • 183
  • 1
  • 10
  • 1
    Is this what you want? https://www.gnu.org/software/make/manual/html_node/Computed-Names.html – user2249675 Jan 06 '20 at 04:46
  • I saw that and that MIGHT be a solution. I'm trying to figure out how to essentially make `FILE_VERSION_$VER` into a variable that will call `$FILE_VERSION_11` or `$FILE_VERSION_8` from the env file. Just having trouble wrapping my head around the logic. – maskeda Jan 06 '20 at 04:55
  • I think the correct syntax is FILE_VERSION=$(FILE_VERSION_$(VER)) – user2249675 Jan 06 '20 at 05:09
  • That makes `FILE_VERSION=_11` because I screwed up, so I changed it to `FILE_VERSION=FILE_VERSION_$(VAR)` but that still doesn't convert the variable to `$FILE_VERSION_11` and doesn't set the contents of `$FILE_VERSION_11` to `$FILE_VERSION`. Error: `[Warning] One or more build-args [FILE_VERSION] were not consumed` – maskeda Jan 06 '20 at 05:26

1 Answers1

1

Try this:

--build-arg FILE_VERSION=$(FILE_VERSION_$(VER))

And in the Dockerfile you need to specify which args could be passed via --build-arg option:

FROM <image>

ARG FILE_VERSION

RUN echo $FILE_VERSION
funnydman
  • 9,083
  • 4
  • 40
  • 55
  • As I stated in the comments above this didn't work either. Working through some options with someone at work. I will update this thread if a solution becomes available. – maskeda Jan 06 '20 at 19:46