chore: update API domain deployment config
这个提交包含在:
+155
-1
@@ -169,7 +169,161 @@ admin/
|
||||
ADMIN_COS_PREFIX=admin-test/ npm run deploy:cos
|
||||
```
|
||||
|
||||
## 8. 部署后验证
|
||||
## 8. 后端 SCF 代码更新
|
||||
|
||||
常规后端代码更新流程如下。当前线上函数名为 `jw-beauty-api`,部署包上传到 COS:
|
||||
|
||||
```text
|
||||
deploy/jw-beauty-api/hox-http-function.zip
|
||||
```
|
||||
|
||||
### 8.1 拉取代码并检查
|
||||
|
||||
```bash
|
||||
git status --short --branch
|
||||
git pull --ff-only
|
||||
```
|
||||
|
||||
如果 `server/` 有依赖变更,先同步依赖:
|
||||
|
||||
```bash
|
||||
cd server
|
||||
npm install
|
||||
```
|
||||
|
||||
部署前至少做语法检查:
|
||||
|
||||
```bash
|
||||
cd server
|
||||
node -c src/app.js
|
||||
node -c src/index.js
|
||||
node -c src/lib/serverless.js
|
||||
node -c scripts/local-server.js
|
||||
```
|
||||
|
||||
如果只改了某个路由,也可以额外检查对应文件,例如:
|
||||
|
||||
```bash
|
||||
node -c src/routes/subscription.js
|
||||
```
|
||||
|
||||
### 8.2 打包后端代码
|
||||
|
||||
部署包必须以 `server/` 为根目录打包,并包含:
|
||||
|
||||
- `index.js`
|
||||
- `package.json`
|
||||
- `package-lock.json`
|
||||
- `scripts/`
|
||||
- `scf_bootstrap`
|
||||
- `src/`
|
||||
- `sql/`
|
||||
- `node_modules/`
|
||||
|
||||
不要把 `server/.env` 打进部署包。
|
||||
|
||||
实际使用命令:
|
||||
|
||||
```bash
|
||||
cd server
|
||||
rm -f /tmp/hox-http-function.zip
|
||||
zip -qry /tmp/hox-http-function.zip index.js package.json package-lock.json scripts scf_bootstrap src sql node_modules
|
||||
```
|
||||
|
||||
### 8.3 上传部署包到 COS
|
||||
|
||||
上传复用 `server/.env` 中的腾讯云/COS 凭证。命令只读取本地环境变量,不要把真实密钥写入文档或提交到 Git。
|
||||
|
||||
```bash
|
||||
cd server
|
||||
set -a; source ./.env; set +a
|
||||
node - <<'NODE'
|
||||
const fs = require('fs')
|
||||
const COS = require('cos-nodejs-sdk-v5')
|
||||
const cos = new COS({
|
||||
SecretId: process.env.TENCENT_SECRET_ID,
|
||||
SecretKey: process.env.TENCENT_SECRET_KEY
|
||||
})
|
||||
|
||||
cos.putObject({
|
||||
Bucket: process.env.COS_BUCKET,
|
||||
Region: process.env.COS_REGION || process.env.TENCENT_REGION || 'ap-guangzhou',
|
||||
Key: 'deploy/jw-beauty-api/hox-http-function.zip',
|
||||
Body: fs.createReadStream('/tmp/hox-http-function.zip')
|
||||
}, err => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
}
|
||||
console.log('uploaded deploy/jw-beauty-api/hox-http-function.zip')
|
||||
})
|
||||
NODE
|
||||
```
|
||||
|
||||
### 8.4 更新 SCF 函数代码
|
||||
|
||||
使用 `tccli scf UpdateFunctionCode` 指向刚上传的 COS 对象:
|
||||
|
||||
```bash
|
||||
cd server
|
||||
set -a; source ./.env; set +a
|
||||
/Users/guoguo/.local/bin/tccli scf UpdateFunctionCode \
|
||||
--region ap-guangzhou \
|
||||
--FunctionName jw-beauty-api \
|
||||
--CosBucketName "$COS_BUCKET" \
|
||||
--CosObjectName /deploy/jw-beauty-api/hox-http-function.zip \
|
||||
--CosBucketRegion "${COS_REGION:-ap-guangzhou}" \
|
||||
--secretId "$TENCENT_SECRET_ID" \
|
||||
--secretKey "$TENCENT_SECRET_KEY"
|
||||
```
|
||||
|
||||
如果命令返回 `RequestId`,表示更新请求已提交成功。函数实例刷新通常需要等待几秒。
|
||||
|
||||
### 8.5 更新后快速验证
|
||||
|
||||
```bash
|
||||
node - <<'NODE'
|
||||
const base = 'https://api.vsai.net.cn'
|
||||
|
||||
async function print(label, res) {
|
||||
const text = await res.text()
|
||||
console.log(label, res.status, text.slice(0, 1000))
|
||||
return text
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
await print('health', await fetch(base + '/health'))
|
||||
|
||||
const loginText = await print('login', await fetch(base + '/api/v1/admin/login', {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body: JSON.stringify({ username: 'admin', password: 'admin' })
|
||||
}))
|
||||
|
||||
const token = JSON.parse(loginText).data && JSON.parse(loginText).data.token
|
||||
if (token) {
|
||||
await print('settings', await fetch(base + '/api/v1/admin/settings', {
|
||||
headers: { authorization: 'Bearer ' + token }
|
||||
}))
|
||||
await print('dashboard', await fetch(base + '/api/v1/admin/dashboard', {
|
||||
headers: { authorization: 'Bearer ' + token }
|
||||
}))
|
||||
}
|
||||
})().catch(err => {
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
})
|
||||
NODE
|
||||
```
|
||||
|
||||
常见成功结果:
|
||||
|
||||
- `GET /health` 返回 `code: 0`
|
||||
- `POST /api/v1/admin/login` 使用 `admin/admin` 返回 `code: 0`
|
||||
- `GET /api/v1/admin/settings` 返回 `code: 0`
|
||||
- `GET /api/v1/admin/dashboard` 返回 `code: 0`
|
||||
|
||||
## 9. 部署后验证
|
||||
|
||||
建议按顺序验证:
|
||||
|
||||
|
||||
在新工单中引用
屏蔽一个用户