如何获取 运行 一个使用其中的 make 变量创建 azure 服务主体的命令

How do I get make to run a command that uses a make variable in it to create an azure service principal

我的 Makefile 中有以下规则:

setSPRole:
    az role assignment create --assignee $(appId) --scope $(az acr show --name $(acr_name) --query id --output tsv) --role acrpush

在文件的顶部我有 include .env 这样我就可以使用 appId 和 acr_name

然而当我 运行 make setSPRole 我得到的结果是: az role assignment create --assignee "MY_ACTUAL_APP_ID" --scope --role acrpush

如您所见,作用域为空,但 appId 已正确填写。 如何获取范围以使用 az acr show 命令的值?

我尝试过的: 我试过分配值,然后在下一行使用它,如下所示:

setSPRole:
    ACR_REG_ID = az acr show --name $(acr_name) --query id --output tsv; \
    az role assignment create --assignee $(appId) --scope $(ACR_REG_ID) --role acrpush

但这也不起作用。

make 规则中的配方是 shell 脚本。 Make 调用 shell 到 运行 它们。因此,您放入配方中的命令(使用 TAB 缩进)必须是有效的 shell 命令。

知道了这一点,也许您可​​以明白为什么您的第二个示例不起作用:那不是有效的 shell 脚本(如果您在 shell 提示符下键入它,您会得到相同的结果make 运行s 时出现的错误。

然而 make 使用 $ 前缀来扩展它自己的变量(和函数,在 GNU make 中)。因此像 $(foo)${foo} 这样的文本在调用 shell 之前被 make 扩展,并将扩展结果发送到 shell 成为 运行 .

知道了这一点,也许您可​​以明白为什么您的第一个示例不起作用:字符串 $(az acr show ...) 被认为是一个长的 make 变量名,它从未被设置,所以它扩展为空字符串。

在 make 配方中,如果你想隐藏 make 中的 $ 以便它被传递给 shell,你可以通过写两次来转义它:$$(az acr show ...).