Skip to content

Commit

Permalink
feat: deploy 配置支持通过环境变量设置 (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
liaoyu authored Jul 24, 2023
1 parent a69b887 commit 7033c5c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 12 deletions.
18 changes: 15 additions & 3 deletions build-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,25 @@ const apiUrl = "http://foobar.com/api" + 'test'

```json
{
"AccessKey": "xxx",
"SecretKey": "yyy",
"accessKey": "xxx",
"secretKey": "yyy",
"bucket": "zzz"
}
```

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

也可以通过环境变量的形式来配置,例如:

```json
{
"accessKey": "{{process.env.BUILD_DEPLOY_ACCESS_KEY}}",
"secretKey": "{{process.env.BUILD_DEPLOY_SECRET_KEY}}",
"bucket": "zzz"
}
```

表示使用环境变量 `BUILD_DEPLOY_ACCESS_KEY``BUILD_DEPLOY_SECRET_KEY` 的值分别作为 accessKey 与 secretKey,上传到名为 `zzz` 的 bucket。

## **`test`**

Expand Down
25 changes: 21 additions & 4 deletions lib/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
const path = require('path')
const walk = require('walk')
const qiniu = require('qiniu')
const Mustache = require('mustache')

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

function getDeployConfig(deploy) {
const { config } = deploy
const model = {
process: {
env: process.env
}
}

return Object.keys(config).reduce((prev, curr) => {
prev[curr] = Mustache.render(config[curr], model)
return prev
}, {})
}

async function upload() {
const buildConfig = await findBuildConfig()
const deployConfig = buildConfig.deploy.config
const { deploy, publicUrl } = buildConfig
const distPath = paths.getDistPath(buildConfig)
const prefix = getPathFromUrl(buildConfig.publicUrl, false)
const mac = new qiniu.auth.digest.Mac(deployConfig.accessKey, deployConfig.secretKey)
const prefix = getPathFromUrl(publicUrl, false)
const { accessKey, secretKey, bucket } = getDeployConfig(deploy)

const mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
const files = await getAllFiles(distPath)

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

await uploadFile(filePath, deployConfig.bucket, key, mac)
await uploadFile(filePath, bucket, key, mac)
logger.info(`[UPLOAD] ${filePath} -> ${key}`)
}, concurrentLimit)

Expand Down
7 changes: 6 additions & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fec-builder",
"version": "1.18.0-beta.3",
"version": "1.18.0",
"bin": {
"fec-builder": "./bin/fec-builder"
},
Expand Down Expand Up @@ -40,6 +40,7 @@
"lodash": "^4.17.15",
"log4js": "^4.1.0",
"mini-css-extract-plugin": "^0.5.0",
"mustache": "^4.2.0",
"optimize-css-assets-webpack-plugin": "^5.0.1",
"postcss-loader": "^2.0.8",
"qiniu": "^7.2.1",
Expand Down
2 changes: 1 addition & 1 deletion preset-configs/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
"target": { "type": "string", "enum": ["qiniu"], "description": "部署目标" },
"config": {
"type": "object",
"description": "针对当前部署目标的配置信息,如 target 为 `\"qiniu\"` 时,需要提供的 config 形如:\n```json\n{\n \"AccessKey\": \"xxx\",\n \"SecretKey\": \"yyy\",\n \"bucket\": \"zzz\"\n}\n```\n表示使用 `xxx`、`yyy` 分别作为 AccessKeySecretKey,上传到名为 `zzz` 的 bucket。"
"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。"
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions preset-configs/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
"deploy": {
"target": "qiniu",
"config": {
"AccessKey": "",
"SecretKey": "",
"accessKey": "",
"secretKey": "",
"bucket": ""
}
},
Expand Down

0 comments on commit 7033c5c

Please sign in to comment.