Skip to content

Commit

Permalink
feat: deploy 配置支持通过环境变量设置 (#174)
Browse files Browse the repository at this point in the history
* feat: deploy 配置支持通过环境变量设置

---------

Co-authored-by: liaoxiaoyou <[email protected]>
  • Loading branch information
liaoyu and liaoxiaoyou authored Jul 24, 2023
1 parent 6eae30c commit 1b345db
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 14 deletions.
18 changes: 15 additions & 3 deletions build-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,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
31 changes: 29 additions & 2 deletions npm-shrinkwrap.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fec-builder",
"version": "2.7.0",
"version": "2.7.1",
"bin": {
"fec-builder": "./lib/bin.js"
},
Expand Down Expand Up @@ -43,6 +43,7 @@
"lodash": "^4.17.20",
"log4js": "^6.3.0",
"mini-css-extract-plugin": "^1.3.9",
"mustache": "^4.2.0",
"postcss-loader": "^4.3.0",
"postcss-preset-env": "~7.2.3",
"qiniu": "^7.3.2",
Expand All @@ -65,6 +66,7 @@
"@types/css-minimizer-webpack-plugin": "^1.1.1",
"@types/lodash": "^4.14.165",
"@types/mini-css-extract-plugin": "^1.2.2",
"@types/mustache": "^4.2.2",
"@types/postcss-preset-env": "^7.7.0",
"@types/semver": "^7.3.4",
"@types/walk": "^2.3.0",
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 @@ -187,7 +187,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 @@ -73,8 +73,8 @@
"deploy": {
"target": "qiniu",
"config": {
"AccessKey": "",
"SecretKey": "",
"accessKey": "",
"secretKey": "",
"bucket": ""
}
},
Expand Down
27 changes: 22 additions & 5 deletions src/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import path from 'path'
import walk from 'walk'
import qiniu from 'qiniu'
import Mustache from 'mustache'
import logger from './utils/logger'
import { findBuildConfig } from './utils/build-conf'
import { Deploy, findBuildConfig } from './utils/build-conf'
import { getDistPath } from './utils/paths'
import { getPathFromUrl } from './utils'

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

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

return (Object.keys(config) as Array<keyof Deploy['config']>).reduce((prev, curr) => {
prev[curr] = Mustache.render(config[curr], model)
return prev
}, {} as Deploy['config'])
}

async function upload() {
const buildConfig = await findBuildConfig()
const deployConfig = buildConfig.deploy.config
const { deploy, publicUrl } = buildConfig
const distPath = 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 @@ -135,7 +152,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

0 comments on commit 1b345db

Please sign in to comment.