8000 feat: deploy 配置支持通过环境变量设置 by liaoyu · Pull Request #174 · qiniu/builder · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: deploy 配置支持通过环境变量设置 #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 24, 2023

Conversation

liaoyu
Copy link
Collaborator
@liaoyu liaoyu commented Jul 20, 2023

build-config.json 中的 deploy.config 支持以环境变量的形式设置,例如:

{
  "accessKey": "{{process.env.BUILD_DEPLOY_ACCESS_KEY}}",
  "secretKey": "{{process.env.BUILD_DEPLOY_SECRET_KEY}}",
  "bucket": "bucket-sample"
}

表示使用环境变量 BUILD_DEPLOY_ACCESS_KEYBUILD_DEPLOY_SECRET_KEY 的值分别作为 accessKey 与 secretKey,上传到名为 bucket-sample 的 bucket。

@liaoyu liaoyu changed the title chore: deploy 支持使用环境变量 chore: deploy 配置信息支持通过环境变量设置 Jul 20, 2023
@liaoyu liaoyu changed the title chore: deploy 配置信息支持通过环境变量设置 chore: deploy 配置支持通过环境变量设置 Jul 20, 2023
@liaoyu liaoyu changed the title chore: deploy 配置支持通过环境变量设置 feat: deploy 配置支持通过环境变量设置 Jul 20, 2023
build-config.md Outdated
@@ -382,6 +382,8 @@ const apiUrl = "http://foobar.com/api" + 'test'

表示使用 `xxx`、`yyy` 分别作为 AccessKey 与 SecretKey,上传到名为 `zzz` 的 bucket。

你也可以通过设置环境变量 `FEC_BUILDER_ACCESS_KEY`、`FEC_BUILDER_SECRET_KEY`、`FEC_BUILDER_BUCKET` 来配置,两种方式都设置时优先取 build-config.json 的值

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIP: 不说“你”感觉会更专业… 🤣
后面那里也是…

build-config.md Outdated
@@ -382,6 +382,8 @@ const apiUrl = "http://foobar.com/api" + 'test'

表示使用 `xxx`、`yyy` 分别作为 AccessKey 与 SecretKey,上传到名为 `zzz` 的 bucket。

你也可以通过设置环境变量 `FEC_BUILDER_ACCESS_KEY`、`FEC_BUILDER_SECRET_KEY`、`FEC_BUILDER_BUCKET` 来配置,两种方式都设置时优先取 build-config.json 的值

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已有的环境变量是

  • verbose
  • BUILD_ENV
  • BUILD_ROOT

一方面,引入 FEC_BUILDER 前缀会导致第三种命名风格的出现(但我确实觉得这种命名好像更好)
另一方面,是不是加一个 DEPLOY 前缀会好点(类似上面的 BUILD_

另一条路是传参,就能绕过这个问题,但使用上就会稍微麻烦点

@nighca 有建议吗

Copy link
Collaborator
@nighca nighca Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议保持 BUILD_XXX 的格式

fec 是要被扔掉的概念,builder 才是这个工具名的核心;比如这个 repo 从 Front-End-Engineering-Cloud 挪到 qiniu 下边后 repo 的路径里就没有 fec 的概念了,长期来看 npm 包名也改成 @qiniu/builder 才会更合适

另一方面,是不是加一个 DEPLOY 前缀会好点(类似上面的 BUILD_

环境变量前缀中的 buildbuilder 这个概念是对应的,指代 builder 所做的整个事情,而不是指 fec-builder build 这个子命令

另外,这里的 accessKey、secretKey & bucket 都是 target: qiniu 时才有的概念,直接 BUILD_ACCESS_KEY 逻辑上是不对的,比如如果也支持部署到 cloudflare R2 的话,那么 BUILD_ACCESS_KEY 是 qiniu 的 accessKey 还是 cloudflare 的呢?如果 cloudflare 不是通过 ak/sk 签,而是直接通过某个 apiKey 呢?

所以建议这里是 BUILD_DEPLOY_QINIU_ACCESS_KEY=XxxBUILD_DEPLOY_QINIU_SECRET_KEY=YyyBUILD_DEPLOY_QINIU_BUCKET=abc,甚至建议直接:BUILD_DEPLOY_QINIU_CONFIG={"accessKey":"Xxx","secretKey":"Yyy","bucket":"abc"}(如果并不会让环境变量的定义更麻烦的话)

P.S. 另外跟这个 PR 关系不大的,可以逐步把现有代码里跟 fec 有关系的内容也都干掉

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接使用 BUILD_DEPLOY_QINIU_CONFIG 配置环境变量会更简洁,但要留意得写成合法的 json 字符串;或者更进一步内部取 BUILD_DEPLOY_${target.toUpperCase()}_CONFIG 的值?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

但要留意得写成合法的 json 字符串

对,这是配置时额外的负担。这个负担是不是值得,你们来评估就好

或者更进一步内部取 BUILD_DEPLOY_${target.toUpperCase()}_CONFIG 的值?

这个是内部的实现细节了,写死 QINIU 或者用 target 我都 OK

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, 38ce205 ,我按 BUILD_DEPLOY_QINIU_CONFIG 来实现了

build-config.md Outdated
@@ -382,6 +382,8 @@ const apiUrl = "http://foobar.com/api" + 'test'

表示使用 `xxx`、`yyy` 分别作为 AccessKey 与 SecretKey,上传到名为 `zzz` 的 bucket。

你也可以通过设置环境变量 `FEC_BUILDER_ACCESS_KEY`、`FEC_BUILDER_SECRET_KEY`、`FEC_BUILDER_BUCKET` 来配置,两种方式都设置时优先取 build-config.json 的值

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

优先级方面,我也有点不太确定这样是否合理… @nighca 觉得呢

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我没问题

Copy link
Collaborator
@nighca nighca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其他我没问题~

secretKey: string
bucket: string
config?: {
accessKey?: string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accessKeysecretKey 等字段不需要是 optional 的?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

配置文件里 deploy 的配置我偏向下面这样写,bucket 的值会落到配置里,其它填充空字符串

"deploy": {
  "config": {
    "accessKey": "",
    "secretKey": "",
    "bucket": "online-bucket"
  }
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

类型上的 { accessKey?, secretKey? } 对应到值不应该是 { "bucket": "online-bucket" } 嘛?为啥是 { "accessKey": "", "secretKey": "", "bucket": "online-bucket" }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外有个 NIP 的点,我能理解想把 AK/SK 跟 bucket 分开的做法,不过脑补了下,那样可能会有点 implicit(考虑到 builder 有文档,这不算多大问题)

我之前一度想这么做过:允许在 config 文件中直接使用环境变量(比如通过模板语法),类似:

"config": {
  "accessKey": "{{process.env.QINIU_ACCESS_KEY}}",
  "secretKey": "{{process.env.QINIU_SECRET_KEY}}",
  "bucket": "online-bucket"
}

然后 builder 用环境变量去渲染配置中的值来得到最终使用的配置值,比如 {{process.env.QINIU_PD_ACCESS_KEY}} 就会被替换为 process.env.QINIU_ACCESS_KEY 的内容。

这个做法有一些自己的好处,比如可以把 AK/SK 跟 bucket 分开维护,又能看得出来值从哪儿来;以及使用方可以自己定义环境变量的名字,不需要 builder 去定义,也就不需要考虑 #174 (comment) 这边的问题;提出来仅供参考吧

Copy link
Collaborator Author
@liaoyu liaoyu Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, ecf4ae8
我前面考虑的是字段列出来用于提醒开发者要设置这几个值

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{{process.env.QINIU_ACCESS_KEY}} 这种实现形式是优雅很多,之前没想到还有这种方式 😆

我解析的时候正则判断仅有 process.env.xx 这种格式才动态取值,其它情况都直接取值?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我建议是不用判断 process.env. 前缀,因为 {{}} 这样的模板语法已经足以说明它是希望使用动态值进行渲染了(这里假设我们使用 mustache)。不过渲染的时候,我们只需要提供 process.env 用于渲染,以 mustache.js 为例:

const env = process.env
const params = { process: { env } }
const rendered = Mustache.render(template, params)

这里也不需要我们检查是否包含 {{}},因为模板引擎会处理

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

< 57A7 input type="hidden" name="_method" value="put" autocomplete="off" />

done, 18f2f87

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, 18f2f87

这里好像还是有 process.env@liaoyu

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

指的 {{process.env.BUILD_DEPLOY_ACCESS_KEY}} 里的 process.env 吗,带上前缀会更容易理解一些

Copy link
Collaborator
@nighca nighca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
@lzfee0227 lzfee0227 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0