ktg807
ktg807

Reputation: 13

Passing Variables in Makefile

I'm using a Makefile to run various docker-compose commands and I'm trying to capture the output of a script run on my local machine and pass that value to a Docker image.

start-service:
    VERSION=$(shell aws s3 ls s3://redact/downloads/1.2.3/) && \
    docker-compose -f ./compose/docker-compose.yml run \
    -e VERSION=$$(VERSION) \
    connect make run-service

When I run this I can see the variable being assigned but it still errors. Why is the value not getting passed into the -e argument:

VERSION=1.2.3-build342 && \
    docker-compose -f ./compose/docker-compose.yml run --rm \
    -e VERSION?=$(VERSION) \
    connect make run-connect
/bin/sh: VERSION: command not found

Upvotes: 1

Views: 1234

Answers (1)

David Maze
David Maze

Reputation: 159242

You're mixing several different Bourne shell and Make syntaxes here. The Make $$(VERSION) translates to shell $(VERSION), which is command-substitution syntax; GNU Make $(shell ...) generally expands at the wrong time and isn't what you want here.

If you were writing this as an ordinary shell command it would look like

# Set VERSION using $(...) substitution syntax
# Refer to just plain $VERSION
VERSION=$(aws s3 ls s3://redact/downloads/1.2.3/) && ... \
-e VERSION=$VERSION ... \

So when you use this in a Make context, if none of the variables are Make variables (they get set and used in the same command), just double the $ to $$ not escape them.

start-service:
        VERSION=$$(aws s3 ls s3://redact/downloads/1.2.3/) && \
        docker-compose -f ./compose/docker-compose.yml run \
        -e VERSION=$$VERSION \
        connect make run-service

Upvotes: 1

Related Questions