在下面的代码片段中:
IMAGES_TO_DELETE := $(aws ecr list-images --region $(ECR_REGION) --repository-name $(ECR_REPO) --filter "tagStatus=UNTAGGED" --query 'imageIds[*]' --output json)
.PHONY: target1 target2 cleanimage
cleanimage:
${DISPLAYINFO} "Clean untagged image from AWS ECR "
aws ecr batch-delete-image --region $(ECR_REGION) --repository-name $(ECR_REPO) --image-ids "$(IMAGES_TO_DELETE)" || true
${DISPLAYINFO} "Done"
target1:
# do something
target2:
# do something
IMAGES_TO_DELETE
以 JSON 格式给出图像列表。
IMAGES_TO_DELETE
应该在make cleanimage
执行时分配
如何为cleanimage
目标下的变量赋值?
你似乎对make
变量有持续的误解。
目前尚不清楚为什么这需要是一个变量。只需将其内联到目标中:
.PHONY: cleanimage
cleanimage:
${DISPLAYINFO} "Clean untagged image from AWS ECR "
aws ecr batch-delete-image --region $(ECR_REGION) --repository-name $(ECR_REPO) \
--image-ids "$$(aws ecr list-images --region $(ECR_REGION) --repository-name $(ECR_REPO) \
--filter "tagStatus=UNTAGGED" --query 'imageIds[*]' --output json)" || true
${DISPLAYINFO} "Done"
顺便$(aws...)
说一句,这不是一个有效的make
功能;你的意思是$(shell aws ...)
; 但同样,make
如果仅在单个目标中需要它,则根本没有必要对其进行评估。
在您有重复信息的地方,将其重构为变量是有意义的,因此您只需在一个地方更改它。
ECRFLAGS := --region $(ECR_REGION) --repository-name $(ECR_REPO)
.PHONY: cleanimage
cleanimage:
${DISPLAYINFO} "Clean untagged image from AWS ECR "
aws ecr batch-delete-image $(ECRFLAGS) \
--image-ids "$$(aws ecr list-images $(ECRFLAGS) \
--filter "tagStatus=UNTAGGED" --query 'imageIds[*]' --output json)" || true
${DISPLAYINFO} "Done"
请记住,单个美元符号由make
. 如果您想将字面意义上的美元符号传递给 shell,则需要将其加倍。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句