Skip to content

Commit 7033c5c

Browse files
authored
feat: deploy 配置支持通过环境变量设置 (#175)
1 parent a69b887 commit 7033c5c

File tree

6 files changed

+47
-12
lines changed

6 files changed

+47
-12
lines changed

build-config.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,25 @@ const apiUrl = "http://foobar.com/api" + 'test'
291291

292292
```json
293293
{
294-
"AccessKey": "xxx",
295-
"SecretKey": "yyy",
294+
"accessKey": "xxx",
295+
"secretKey": "yyy",
296296
"bucket": "zzz"
297297
}
298298
```
299299

300-
表示使用 `xxx``yyy` 分别作为 AccessKey 与 SecretKey,上传到名为 `zzz` 的 bucket。
300+
表示使用 `xxx``yyy` 分别作为 accessKey 与 secretKey,上传到名为 `zzz` 的 bucket。
301+
302+
也可以通过环境变量的形式来配置,例如:
303+
304+
```json
305+
{
306+
"accessKey": "{{process.env.BUILD_DEPLOY_ACCESS_KEY}}",
307+
"secretKey": "{{process.env.BUILD_DEPLOY_SECRET_KEY}}",
308+
"bucket": "zzz"
309+
}
310+
```
311+
312+
表示使用环境变量 `BUILD_DEPLOY_ACCESS_KEY``BUILD_DEPLOY_SECRET_KEY` 的值分别作为 accessKey 与 secretKey,上传到名为 `zzz` 的 bucket。
301313

302314
## **`test`**
303315

lib/upload.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
const path = require('path')
77
const walk = require('walk')
88
const qiniu = require('qiniu')
9+
const Mustache = require('mustache')
910

1011
const paths = require('./utils/paths')
1112
const logger = require('./utils/logger')
@@ -107,12 +108,28 @@ async function uploadFile(localFile, bucket, key, mac) {
107108
return runWithRetry(putFile, 3)
108109
}
109110

111+
function getDeployConfig(deploy) {
112+
const { config } = deploy
113+
const model = {
114+
process: {
115+
env: process.env
116+
}
117+
}
118+
119+
return Object.keys(config).reduce((prev, curr) => {
120+
prev[curr] = Mustache.render(config[curr], model)
121+
return prev
122+
}, {})
123+
}
124+
110125
async function upload() {
111126
const buildConfig = await findBuildConfig()
112-
const deployConfig = buildConfig.deploy.config
127+
const { deploy, publicUrl } = buildConfig
113128
const distPath = paths.getDistPath(buildConfig)
114-
const prefix = getPathFromUrl(buildConfig.publicUrl, false)
115-
const mac = new qiniu.auth.digest.Mac(deployConfig.accessKey, deployConfig.secretKey)
129+
const prefix = getPathFromUrl(publicUrl, false)
130+
const { accessKey, secretKey, bucket } = getDeployConfig(deploy)
131+
132+
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
116133
const files = await getAllFiles(distPath)
117134

118135
const concurrentLimit = 50
@@ -125,7 +142,7 @@ async function upload() {
125142
return logger.info(`[IGNORE] ${filePath}`)
126143
}
127144

128-
await uploadFile(filePath, deployConfig.bucket, key, mac)
145+
await uploadFile(filePath, bucket, key, mac)
129146
logger.info(`[UPLOAD] ${filePath} -> ${key}`)
130147
}, concurrentLimit)
131148

npm-shrinkwrap.json

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fec-builder",
3-
"version": "1.18.0-beta.3",
3+
"version": "1.18.0",
44
"bin": {
55
"fec-builder": "./bin/fec-builder"
66
},
@@ -40,6 +40,7 @@
4040
"lodash": "^4.17.15",
4141
"log4js": "^4.1.0",
4242
"mini-css-extract-plugin": "^0.5.0",
43+
"mustache": "^4.2.0",
4344
"optimize-css-assets-webpack-plugin": "^5.0.1",
4445
"postcss-loader": "^2.0.8",
4546
"qiniu": "^7.2.1",

preset-configs/config.schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
"target": { "type": "string", "enum": ["qiniu"], "description": "部署目标" },
151151
"config": {
152152
"type": "object",
153-
"description": "针对当前部署目标的配置信息,如 target 为 `\"qiniu\"` 时,需要提供的 config 形如:\n```json\n{\n \"AccessKey\": \"xxx\",\n \"SecretKey\": \"yyy\",\n \"bucket\": \"zzz\"\n}\n```\n表示使用 `xxx`、`yyy` 分别作为 AccessKeySecretKey,上传到名为 `zzz` 的 bucket。"
153+
"description": "针对当前部署目标的配置信息,如 target 为 `\"qiniu\"` 时,需要提供的 config 形如:\n```json\n{\n \"accessKey\": \"xxx\",\n \"secretKey\": \"yyy\",\n \"bucket\": \"zzz\"\n}\n```\n表示使用 `xxx`、`yyy` 分别作为 accessKeysecretKey,上传到名为 `zzz` 的 bucket。\n也可以通过环境变量的形式来配置,例如:\n```json\n{\n \"accessKey\": \"{{process.env.BUILD_DEPLOY_ACCESS_KEY}}\",\n \"secretKey\": \"{{process.env.BUILD_DEPLOY_SECRET_KEY}}\",\n \"bucket\": \"zzz\"\n}\n```\n表示使用环境变量 `BUILD_DEPLOY_ACCESS_KEY`、`BUILD_DEPLOY_SECRET_KEY` 的值分别作为 accessKey 与 secretKey,上传到名为 `zzz` 的 bucket。"
154154
}
155155
}
156156
},

preset-configs/default.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
"deploy": {
5555
"target": "qiniu",
5656
"config": {
57-
"AccessKey": "",
58-
"SecretKey": "",
57+
"accessKey": "",
58+
"secretKey": "",
5959
"bucket": ""
6060
}
6161
},

0 commit comments

Comments
 (0)