首页 > 解决方案 > 如果变量为空,如何跳过 Make 目标?

问题描述

考虑以下生成文件

# If current commit is not part of a PR, the PULL_REQUEST_ID variable will be empty.
PULL_REQUEST_ID := $(shell git ls-remote origin 'pull/*/head' | grep -F -f <(git rev-parse HEAD) | awk -F'/' '{print $3}')

# if PULL_REQUEST_ID is empty, I want 'make deploy-to-staging' to be no-op
.PHONY deploy-to-staging
deploy-to-staging: update-some-files apply-those-files-to-k8s

标签: makefilegnu-make

解决方案


使用条件指令ifneq

deploy-to-staging: update-some-files apply-those-files-to-k8s
ifneq ($(PULL_REQUEST_ID),'')
        git push ...  # or whatever
else
        echo "Refusing to deploy non-pull-request to staging"
endif

推荐阅读