WeChat Cloud Hosting is a one-stop back-end cloud service launched by the WeChat team and Tencent Cloud. For scenarios where the front-end and back-end separation architecture is adopted for application development, cloud hosting can achieve free operation and maintenance, free domain name, server management, anti-DDoS attacks and overseas acceleration, etc., from code management to CI/CD pipeline deployment and release, providing full links, Low-cost, enterprise-level cloud-native solutions.
On the PC, visit https://cloud.weixin.qq.com to immediately start using WeChat cloud hosting.
Preface
During project development, there are usually multiple environments that are used to function in each process stage of the development, such as pre-launch and production.
Depending on the environment, the corresponding configuration information such as the database is different. It is necessary to create a separate code warehouse corresponding to the configuration information changes, and the pipeline corresponds to a code warehouse, so the maintenance cost is too large.
Here is mainly the change of configuration information. This article mainly introduces how to dynamically perceive the WeChat cloud hosting environment in the project code, and then give the correct configuration information.
One, environment variables
WeChat Cloud Hosting brings in an environment variable in the process of running the project service. The name is CBR_ENV_ID, which means the ID of the WeChat Cloud hosting environment where the service is currently running.
So within the project, you can get the CBR_ENV_ID variable to perceive which environment the current project is running in.
Second, divide the environment
Multiple WeChat cloud hosting environments can be established, and there can be multiple services in a single environment, and separate databases and object storages are isolated from objective conditions.
Therefore, we can use the WeChat cloud hosting environment as a dimension to isolate and create various environments for project development (pre-release, production)
Finally, we can get a list with the following style:
werun—id1 = '预发'
werun—id2 = '生产'
Where werun_id is the environment ID hosted by WeChat Cloud.
Three, configuration code
Next, write the configuration of the above list into the project code, taking nodejs as an example:
Assuming that our configuration config.js is like this at the beginning:
module.exports = {
"text":"开发环境"
}
In order to simplify, there is only one text here. In actual applications, it should be the configuration information of the database, storage, and network communication, that is, the information that needs to be distinguished according to the environment must be written in one piece.
Next, add the CBR_ENV_ID variable for transformation, the code is as follows:
const config = {
'werun—id1': {
text:'预发环境'
},
'werun—id2': {
text:'生产环境'
},
NO: {
text: '本地环境'
}
}
module.exports = function(env=null){
const key = env || process.env.CBR_ENV_ID
if(config[key] != null){
return config[key]
} else {
return config.NO
}
}
The above code includes the configuration information of each environment uniformly, and then distributes it according to the CBR_ENV_ID variable. If there is no preset environment configuration, it returns to the local test configuration.
The above has completed the dynamic configuration transformation of the project, and then we begin to configure the pipeline.
Fourth, configure the pipeline
The pre-release and production environments should be consistent as a whole, but differentiated at the data level, so the two environments have little difference in overall configuration.
According to your own business needs, configure one or several services, select database or object storage, etc.
Next, we will focus on the configuration differences of the pipeline:
The above is the information box of the new pipeline. The same service in the two environments should be configured with the same code repository, branch, and target directory.
The trigger conditions can be slightly changed according to the development process. A reasonable choice is to trigger on changes or timed triggers; for example, if you release a version every Wednesday, then you can choose timed triggers according to the plan.
In the release strategy, because the production environment requires stability, it cannot be directly released in full. You can choose to build only the image or build the version.
The pre-release environment can be directly released in full, which is convenient to directly start the experience test of the system. After the test is passed, the gray-scale release of the production environment can be arranged.
Five, environmental process recommendations
Each team has its own development and testing process and environment. Please understand the content of this article reasonably according to your own situation.
The development and testing process under the traditional development model requires some adaptations when transferring to WeChat cloud hosting. Here are some suggestions:
1. Development environment: An environment where team developers produce code self-test. Generally, a Docker container can be opened on a local PC to mount project code development. The container image ensures the unity of the team and will greatly reduce the failure of joint debugging caused by environmental problems.
2. Testing, joint debugging, and regression environment: The overall testing experience of the project is required. Here, we recommend in different forms. The project form has two forms: a single service type and a microservice type:
Single service type: No matter how big the project is, only one service runs, and all updates need to be replaced; in this case, it is not recommended to put it in the WeChat cloud hosting environment for joint debugging and testing. Direct local testing and joint debugging and development will be more efficient Taller. Because only internal businesses call each other, no other services are involved (except for API interfaces, databases, and object storage, which are themselves external connectivity).
Microservice form: The project is split into different modules. Each module service runs independently and works together. Updates only need to replace the changed ones. In this case, it is recommended to put them in the WeChat cloud hosting for testing, that is, the changed modules will go through Test, when the performance is stable, deploy it to WeChat cloud hosting, and do overall testing with other services.
3. Pre-release and production environment: Separate environments are opened in WeChat cloud hosting. If you need to connect the pre-release environment to the production environment database, you can set up the intranet connection of the two environments, and then connect the intranet to the corresponding database.
Experience WeChat cloud hosting now and enjoy 1 month free quota: https://cloud.weixin.qq.com/cloudrun
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。