0

I have a Makefile that I am using to build and tag a docker image. I want to tag the built image with name and version info extracted from a file ("pyproject.toml"). My extraction mechanism is pretty simple, and works when run manually on the command line:

$ grep -e "^name\s*=\s*" pyproject.toml | cut -d = -f 2 | xargs
audio_program_generator
$ grep -e "^version\s*=\s*" pyproject.toml | cut -d = -f 2 | xargs
1.7.1.0

However, when I try to do the same using variables in a Makefile, they expand to nothing.

My pyproject.toml file looks like this:

[tool.poetry]
name = "audio_program_generator"
version = "1.7.1.0"

Relevant section of Makefile:

NAME:=$(grep -e "^name\s*=\s*" pyproject.toml | cut -d = -f 2 | xargs)
VERSION:=$(grep -e "^version\s*=\s*" pyproject.toml | cut -d = -f 2 | xargs)

build-apg:
    docker build --target apg --tag $(NAME):$(VERSION) .

Expected output

$ make build-apg
docker build --target apg --tag audio_program_generator:1.7.1.0 .

Actual output:

$ make build-apg
docker build --target apg --tag : .
invalid argument ":" for "-t, --tag" flag: invalid reference format
See 'docker build --help'.
make: *** [build-apg] Error 125

What am I doing wrong?

Jeff Wright
  • 1,014
  • 1
  • 11
  • 17
  • 4
    Makefiles have to use `$(shell command ...)`. Also, that's a strange use of `xargs`; try changing your regex instead. Or even better - is there a (python?) command-line tool that parses the .toml file, akin to `jq` for JSON? – o11c Nov 14 '21 at 02:24
  • Not sure if there's a specific duplicate, but in general https://stackoverflow.com/questions/2019989/how-to-assign-the-output-of-a-command-to-a-makefile-variable – o11c Nov 14 '21 at 02:27
  • Also, TIL that `!=` exists in Makefiles to simplify this common pattern. – o11c Nov 14 '21 at 02:28
  • Yes, that was it, thank you! That is new syntax to me. If you turn this reply into an answer I would be happy to accept it. – Jeff Wright Nov 14 '21 at 04:36

0 Answers0