多次在文件夹上下文中执行命令

Make execute commands on folder context multiple times

我有一个 makefileproj 根目录下。

文件夹proj为主文件夹,其下有ws-ledtools-ext等文件夹,其中包含docker个文件。 另外,root下还有Makefile需要运行所有的命令。

这是文件夹结构

proj
 - ws-led
  — Dockerfile
 - tools-ext
  — Dockerfile    
- Makefile

我需要的是 cd rot 下的每个文件夹(我们还有很多)和 运行:

docker build <folder name> .

示例:(完全类似于 运行手动执行以下命令)

cd ws-led
docker build -t ws-led .

cd tools-ext
docker build -t tools-ext .

我尝试使用以下方法(也许我在 Makefile 同一级别的所有文件夹上得到 运行 而不是 repo 参数)

喜欢(CURDIR)…

all: pre docker-build
.PHONY: pre docker-build

repos := ws-led tools-ext

pre:
    $(patsubst %,docker-build,$(repos))

docker-build:pre
    cd $*; docker build -t $* . >&2 | tee docker-build

使用此即时通讯时出现错误:

invalid argument "." for "-t, --tag" flag: invalid reference format

知道这里出了什么问题吗?或者我可以做得更好?

因为我有很多 repos/folders 我想用 make 来处理它

有不止一种方法。

您可以使用 bash for 循环:

docker-build:
    for dir in $(repos); do cd $$dir; docker build -t $$dir . >&2 | tee docker-build; done

或使用模式规则(或在本例中为静态模式规则):

REPO_BUILDS := $(addsuffix -build, $(repos))

docker-build: $(REPO_BUILDS)

.PHONY: $(REPO_BUILDS)
$(REPO_BUILDS): %-build:
    cd $*; docker build -t $* . >&2 | tee docker-build