自动将导出变量添加到Makefile目标

huangapple go评论108阅读模式
英文:

Auto add export variables to Makefile targets

问题

我想要在运行 target 时加载 Makefile 中的所有 export 变量。

目标是当我运行 make start 时,它应该运行类似于 HUB_DIR="some/hub/dir/here" PROJECT_DIR="some/project/dir/here" ... docker-compose -f docker-compose.yml up -d 的内容(... 这里是 Makefile 中的所有 export 变量)。

我已经搜索了很多 Stack Overflow 的答案,但没有找到答案,我也不知道是否可能实现这个目标。

任何帮助将不胜感激!

export HUB_DIR=${shell pwd}
export PROJECT_DIR=${shell cd .. && pwd}
export BE_DIR=${shell cd ../chainmart-be && pwd}
export BE_PRODUCTS_DIR=${shell cd ../chainmart-be-products && pwd}
export PROJECT_NAME=chainmart
export BE_PORT=3000

start:
	docker-compose -f docker-compose.yml up -d

stop:
	docker-compose -f docker-compose.yml down

这是从 docker-compose 返回的错误信息:

WARN[0000] "BE_PRODUCTS_DIR" 变量未设置。默认为空字符串。
WARN[0000] "BE_PRODUCTS_DIR" 变量未设置。默认为空字符串。
WARN[0000] "BE_PRODUCTS_DIR" 变量未设置。默认为空字符串。
WARN[0000] "BE_PRODUCTS_DIR" 变量未设置。默认为空字符串。
WARN[0000] "PROJECT_NAME" 变量未设置。默认为空字符串。
WARN[0000] "PROJECT_NAME" 变量未设置。默认为空字符串。
WARN[0000] "PROJECT_NAME" 变量未设置。默认为空字符串。
WARN[0000] "PROJECT_NAME" 变量未设置。默认为空字符串。
WARN[0000] "BE_DIR" 变量未设置。默认为空字符串。
WARN[0000] "BE_DIR" 变量未设置。默认为空字符串。
WARN[0000] "BE_DIR" 变量未设置。默认为空字符串。
WARN[0000] "BE_DIR" 变量未设置。默认为空字符串。
WARN[0000] "BE_PORT" 变量未设置。默认为空字符串。
WARN[0000] "BE_PORT" 变量未设置。默认为空字符串。
英文:

I want to load all export variables in Makefile when run target

The goal is when I run make start. It should run something like HUB_DIR="some/hub/dir/here" PROJECT_DIR="some/project/dir/here" ... docker-compose -f docker-compose.yml up -d. (... here is all the export in Makefile)

I have already searched many SO answers but didn't find answer and I also don't know if this is even possible to achieve.

Any help would be much appreciated!

export HUB_DIR=${shell pwd}
export PROJECT_DIR=${shell cd .. && pwd}
export BE_DIR=${shell cd ../chainmart-be && pwd}
export BE_PRODUCTS_DIR=${shell cd ../chainmart-be-products && pwd}
export PROJECT_NAME=chainmart
export BE_PORT=3000

start:
	docker-compose -f docker-compose.yml up -d

stop:
	docker-compose -f docker-compose.yml down

Here is the errors returned from docker-compose

WARN[0000] The "BE_PRODUCTS_DIR" variable is not set. Defaulting to a blank string. 
WARN[0000] The "BE_PRODUCTS_DIR" variable is not set. Defaulting to a blank string.
WARN[0000] The "BE_PRODUCTS_DIR" variable is not set. Defaulting to a blank string.
WARN[0000] The "BE_PRODUCTS_DIR" variable is not set. Defaulting to a blank string.
WARN[0000] The "PROJECT_NAME" variable is not set. Defaulting to a blank string.
WARN[0000] The "PROJECT_NAME" variable is not set. Defaulting to a blank string.
WARN[0000] The "PROJECT_NAME" variable is not set. Defaulting to a blank string.
WARN[0000] The "PROJECT_NAME" variable is not set. Defaulting to a blank string.
WARN[0000] The "BE_DIR" variable is not set. Defaulting to a blank string.
WARN[0000] The "BE_DIR" variable is not set. Defaulting to a blank string.
WARN[0000] The "BE_DIR" variable is not set. Defaulting to a blank string.
WARN[0000] The "BE_DIR" variable is not set. Defaulting to a blank string.
WARN[0000] The "BE_PORT" variable is not set. Defaulting to a blank string.
WARN[0000] The "BE_PORT" variable is not set. Defaulting to a blank string.

Edited:
Here is my docker-compose.yml for more information, but it's very long so I cut a few unnecessary services

version: "3.9"

services:

  ...

  be:
    build: 
      dockerfile: ${BE_DIR}/Dockerfile
      context: ${BE_DIR}
      target: dev
    command: npm run start:dev
    image: ${PROJECT_NAME}--be-image
    container_name: ${PROJECT_NAME}--be
    env_file:
      - ${BE_DIR}/.env.dev
    volumes:
      - ${BE_DIR}:/app
    ports:
      - ${BE_PORT}:${BE_PORT}
    networks:
      - chainmart 
    depends_on:
      - kafka
      - be-products

  be-products:
    build: 
      dockerfile: ${BE_PRODUCTS_DIR}/Dockerfile
      context: ${BE_PRODUCTS_DIR}
      target: dev
    command: npm run start:dev
    image: ${PROJECT_NAME}--be-products-image
    container_name: ${PROJECT_NAME}--be-products
    env_file:
      - ${BE_PRODUCTS_DIR}/.env.dev
    volumes:
      - ${BE_PRODUCTS_DIR}:/app
    networks:
      - chainmart 
    depends_on:
      - kafka

  ...

答案1

得分: 1

> 当运行 target 时,我想要加载Makefile中的所有export变量。
>
> 目标是当我运行 make start 时,它应该运行类似以下的内容:
> HUB_DIR="some/hub/dir/here" PROJECT_DIR="some/project/dir/here" ... > docker-compose -f docker-compose.yml up -d(这里的 ... 是Makefile中的所有export)。

你不需要做任何特殊的事情来实现与此等效的功能。export 的功能恰好是请求 make 将指定的变量放入每个配方的每行的环境中。你似乎打算明确地执行这个操作,但这是不必要的并且容易出错。

只有在你想要选择不同的变量传递给不同的命令时,才有必要在命令行上明确指定变量,然后你还应该避免导出那些你想要从任何命令中排除的变量。

我不清楚为什么在你的示例中 docker-compose 抱怨,但我没有理由认为这是因为 make 未能按照其文档的要求运行。

英文:

> I want to load all export variables in Makefile when run target
>
> The goal is when I run make start. It should run something like
> HUB_DIR="some/hub/dir/here" PROJECT_DIR="some/project/dir/here" ...
> docker-compose -f docker-compose.yml up -d
. (... here is all the
> export in Makefile)

You don't need to do anything special to achieve something equivalent to that. The function of export is exactly to request that make put the specified variable in the environment for each line of each recipe. You seem to have in mind to do that explicitly, but that's unnecessary and error-prone.

Only if you wanted to choose different variables to pass on to different commands would it make sense to put the variables on the command line explicitly, and then you would also want to avoid exporting those that you want to exclude from any command.

It's unclear to me why docker-compose is complaining in your example, but I see no reason to think that it's because make is failing to behave according to its documentation.

huangapple
  • 本文由 发表于 2023年7月10日 14:38:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定