background
Cloud Development CloudBase provides environment replication capabilities, which is convenient for developers to develop projects in multiple environments.
Environmental resource replication practice (Environment A -> Environment B)
Function resource
- The cloud function code is copied from the corresponding function of the A environment. Note that the hard-coded environment ID A in the code needs to be manually modified to B.
If the Node SDK is used in the function and the current environment is used, it is recommended to write:
const cloudbase = require("@cloudbase/node-sdk")
const app = cloudbase.init({
env: cloudbase.SYMBOL_CURRENT_ENV // 自动选取当前环境
})
- Function attribute configuration such as memory, timeout time, environment variables, timing trigger, VPC, public network access configuration, function corresponding to cloud access configuration & authentication, CLS log configuration is copied, no developer operation is required.
- The function layer will not be copied, you need to create a new layer in B manually.
Database resources
- When the database is copied, only an empty collection with the same name is created in the new environment, and the table data needs to be manually imported by the user in the console (A environment library is exported, and the B environment library is imported).
- Database security rules and index settings are all copied, and developers do not need to operate.
Cloud storage resources
- Cloud storage configuration such as permission configuration and cache configuration are copied without user operation
- Specific file resources need to be manually imported by the user (A environment exports file resources and imports them to B environment)
Recommended Practice:
- Install cloudbase cli tool and log in
npm i -g @cloudbase/cli
tcb login
- Download all files of A environment to local
# 下载全部文件
tcb storage download / localPath --dir -e A
- Upload local files to B environment
tcb storage upload localPath -e B
Multi-environment project development practice
1. Development environment, production environment distinction
Based on the environment replication capabilities, two environments for development dev and production prod can be quickly set up (eliminating the need to repeatedly build tables and functions).
Reference documents:
https://developers.weixin.qq.com/miniprogram/dev/wxcloud/basis/concepts/environment.html
Operation Practice
- When using the SDK on the cloud function side, use the dynamic environment writing method (similar to function resource copying) to avoid writing dead environment IDs.
- Small program side example
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
- Tencent cloud side example
const cloudbase = require("@cloudbase/node-sdk")
const app = cloudbase.init({
env: cloudbase.SYMBOL_CURRENT_ENV // 自动选取当前环境
})
- In the development and production environment of the client, specify the corresponding environment ID respectively.
wx.cloud.init({
// 此处请填入环境 ID, 环境 ID 可打开云控制台查看
env: config.ENV // dev or prod
})
Furthermore, in order to avoid the risk of human operation, the environment ID can be configured in an engineering way, such as the dev environment ID in the development mode, and the prod environment ID in the production mode. The specific implementation will not be expanded here.
2. Project collaborative development
In the same environment, the problem of dirty data is prone to occur during multi-person collaborative development. Each developer can copy their own development environment based on the initial environment of the project, and debugging and development in each environment will not affect each other.
Open cloud development: https://console.cloud.tencent.com/tcb?tdl_anchor=techsite
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。