我有这样的功能:
build:
git_branch="$(`git rev-parse --abbrev-ref HEAD`)"
ifeq("$git_branch", 'development')
tag="development"
else("$git_branch", 'staging')
tag="staging"
else("$git_branch", 'master')
tag="production"
endif
echo "tag is $(tag)"
当我运行make build
时,这是输出
git_branch=""
ifeq("it_branch", 'development')
/bin/sh: -c: line 0: syntax error near unexpected token `"it_branch",'
/bin/sh: -c: line 0: `ifeq("it_branch", 'development')'
make: *** [build] Error 2
所以首先,git分支是空的,不应该。如果我在控制台上运行此命令,我会正确获取分支名称,其次,将变量评估为it_branch
而不是git_branch
?
嗯,我对makefile很新,我浏览了文档,无法捕捉到任何我可能做错的事情
修改的
我改为:
git_branch:= $(shell git rev-parse --abbrev-ref HEAD)
ifeq ($(git_branch), "development")
tag:= "development"
else($(git_branch), "staging")
tag:= "staging"
else($(git_branch), "master")
tag:= "production"
endif
build:
echo "tag is $(tag)"
echo "branch is $(git_branch)"
标签为空,但git_branch是正确的
答案 0 :(得分:1)
如果makefile变量包含多个字符,则必须用括号括起名称而不是引号。使用shell
评估shell命令:
git_branch=$(shell git rev-parse --abbrev-ref HEAD)
ifeq($(git_branch), 'development')
答案 1 :(得分:1)
这样的事情应该有效:
git_branch:= $(shell git rev-parse --abbrev-ref HEAD)
ifeq ($(git_branch), development)
tag:=development
endif
ifeq ($(git_branch), staging)
tag:=staging
endif
ifeq ($(git_branch), master)
tag:=production
endif
build:
echo "tag is $(tag)"
简短说明:您将shell变量与make变量混淆。你的make变量的逻辑不必在配方中,事实上,这并没有多大意义,因为这些东西是由make解释的(当配方被执行时)通过shell启动)。
在make
中,您必须将变量名称括在parantheses中,否则假设它们只有一个字符。