Skip to content

Commit 1b345db

Browse files
liaoyuliaoxiaoyou
andauthored
feat: deploy 配置支持通过环境变量设置 (#174)
* feat: deploy 配置支持通过环境变量设置 --------- Co-authored-by: liaoxiaoyou <[email protected]>
1 parent 6eae30c commit 1b345db

File tree

6 files changed

+72
-14
lines changed

6 files changed

+72
-14
lines changed

build-config.md

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

375375
```json
376376
{
377-
"AccessKey": "xxx",
378-
"SecretKey": "yyy",
377+
"accessKey": "xxx",
378+
"secretKey": "yyy",
379379
"bucket": "zzz"
380380
}
381381
```
382382

383-
表示使用 `xxx`、`yyy` 分别作为 AccessKey 与 SecretKey,上传到名为 `zzz` 的 bucket。
383+
表示使用 `xxx`、`yyy` 分别作为 accessKey 与 secretKey,上传到名为 `zzz` 的 bucket。
384+
385+
也可以通过环境变量的形式来配置,例如:
386+
387+
```json
388+
{
389+
"accessKey": "{{process.env.BUILD_DEPLOY_ACCESS_KEY}}",
390+
"secretKey": "{{process.env.BUILD_DEPLOY_SECRET_KEY}}",
391+
"bucket": "zzz"
392+
}
393+
```
394+
395+
表示使用环境变量 `BUILD_DEPLOY_ACCESS_KEY`、`BUILD_DEPLOY_SECRET_KEY` 的值分别作为 accessKey 与 secretKey,上传到名为 `zzz` 的 bucket。
384396

385397
## **`test`**
386398

npm-shrinkwrap.json

Lines changed: 29 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fec-builder",
3-
"version": "2.7.0",
3+
"version": "2.7.1",
44
"bin": {
55
"fec-builder": "./lib/bin.js"
66
},
@@ -43,6 +43,7 @@
4343
"lodash": "^4.17.20",
4444
"log4js": "^6.3.0",
4545
"mini-css-extract-plugin": "^1.3.9",
46+
"mustache": "^4.2.0",
4647
"postcss-loader": "^4.3.0",
4748
"postcss-preset-env": "~7.2.3",
4849
"qiniu": "^7.3.2",
@@ -65,6 +66,7 @@
6566
"@types/css-minimizer-webpack-plugin": "^1.1.1",
6667
"@types/lodash": "^4.14.165",
6768
"@types/mini-css-extract-plugin": "^1.2.2",
69+
"@types/mustache": "^4.2.2",
6870
"@types/postcss-preset-env": "^7.7.0",
6971
"@types/semver": "^7.3.4",
7072
"@types/walk": "^2.3.0",

preset-configs/config.schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@
187187
"target": { "type": "string", "enum": ["qiniu"], "description": "部署目标" },
188188
"config": {
189189
"type": "object",
190-
"description": "针对当前部署目标的配置信息,如 target 为 `\"qiniu\"` 时,需要提供的 config 形如:\n```json\n{\n \"AccessKey\": \"xxx\",\n \"SecretKey\": \"yyy\",\n \"bucket\": \"zzz\"\n}\n```\n表示使用 `xxx`、`yyy` 分别作为 AccessKeySecretKey,上传到名为 `zzz` 的 bucket。"
190+
"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。"
191191
}
192192
}
193193
},

preset-configs/default.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@
7373
"deploy": {
7474
"target": "qiniu",
7575
"config": {
76-
"AccessKey": "",
77-
"SecretKey": "",
76+
"accessKey": "",
77+
"secretKey": "",
7878
"bucket": ""
7979
}
8080
},

src/upload.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import path from 'path'
77
import walk from 'walk'
88
import qiniu from 'qiniu'
9+
import Mustache from 'mustache'
910
import logger from './utils/logger'
10-
import { findBuildConfig } from './utils/build-conf'
11+
import { Deploy, findBuildConfig } from './utils/build-conf'
1112
import { getDistPath } from './utils/paths'
1213
import { getPathFromUrl } from './utils'
1314

@@ -117,12 +118,28 @@ async function uploadFile(localFile: string, bucket: string, key: string, mac: q
117118
return runWithRetry(putFile, 3)
118119
}
119120

121+
function getDeployConfig(deploy: Deploy) {
122+
const { config } = deploy
123+
const model = {
124+
process: {
125+
env: process.env
126+
}
127+
}
128+
129+
return (Object.keys(config) as Array<keyof Deploy['config']>).reduce((prev, curr) => {
130+
prev[curr] = Mustache.render(config[curr], model)
131+
return prev
132+
}, {} as Deploy['config'])
133+
}
134+
120135
async function upload() {
121136
const buildConfig = await findBuildConfig()
122-
const deployConfig = buildConfig.deploy.config
137+
const { deploy, publicUrl } = buildConfig
123138
const distPath = getDistPath(buildConfig)
124-
const prefix = getPathFromUrl(buildConfig.publicUrl, false)
125-
const mac = new qiniu.auth.digest.Mac(deployConfig.accessKey, deployConfig.secretKey)
139+
const prefix = getPathFromUrl(publicUrl, false)
140+
const { accessKey, secretKey, bucket } = getDeployConfig(deploy)
141+
142+
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
126143
const files = await getAllFiles(distPath)
127144

128145
const concurrentLimit = 50
@@ -135,7 +152,7 @@ async function upload() {
135152
return logger.info(`[IGNORE] ${filePath}`)
136153
}
137154

138-
await uploadFile(filePath, deployConfig.bucket, key, mac)
155+
await uploadFile(filePath, bucket, key, mac)
139156
logger.info(`[UPLOAD] ${filePath} -> ${key}`)
140157
}, concurrentLimit)
141158

0 commit comments

Comments
 (0)