【问题标题】:Makefile: Propagating variables to dependent targetsMakefile:将变量传播到依赖目标
【发布时间】:2017-06-11 21:27:14
【问题描述】:

这是Makefile: run the same command with different arguments on different targets的延续。

代码

COMMIT := $(shell git rev-parse HEAD)

images := base web proxy lb users api

$(images): base
    @echo "Building $@"
    docker build -f Dockerfile.$@ --no-cache=true -t $@:$(COMMIT) . 

build: $(images)
rebuild: $(images) # I want this to run with --no-cache=true

基本上,build 调用所有image 目标(base 是第一个目标),并为每个目标运行docker build--no-cache=true

问题

我想要一个rebuild 目标,它运行所有image 目标与--no-cache=false 而不是--no-cache=true,而不复制代码。我想正确的方法是在rebuildbuild 中设置一个变量,其范围将涵盖像任何images 这样的依赖目标。

我的问题

如何在范围涵盖所有依赖目标的目标中定义变量?

【问题讨论】:

  • 查看my answer to your first question 并完全摆脱特定于目标的变量。
  • 不,特定于目标的变量会自动执行此操作,正如 Artur 的回答所示。 IMO,它们比使用 call 好得多。

标签: makefile


【解决方案1】:

非常相似,就像提到的问题:

images := base web proxy lb users api

$(images):
    @echo $@ docker --no-cache=$(NO_CACHE)

build: NO_CACHE=true
rebuild: NO_CACHE=false

rebuild build: $(images)

您可能想为NO_CACHE 设置一个默认值,以防您想调用make base

【讨论】:

  • 完美,这是一个非常优雅的解决方案。不知道特定于目标的变量可以很好地传播到相关任务。谢谢!
猜你喜欢
  • 1970-01-01
  • 2020-01-19
  • 2021-02-07
  • 1970-01-01
  • 2011-03-23
  • 1970-01-01
  • 2018-04-18
  • 1970-01-01
  • 2013-06-05
相关资源
最近更新 更多