1

我正在使用 Makefile 来运行各种 docker-compose 命令,并且正在尝试捕获在本地计算机上运行的脚本的输出并将该值传递给 Docker 映像。

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

当我运行它时,我可以看到正在分配的变量,但它仍然是错误的。为什么值没有传递到 -e 参数中:

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
4

1 回答 1

1

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
于 2021-02-03T00:38:00.153 回答