小程序的版本管理与发布流程
版本管理基础概念
微信小程序的版本管理主要涉及代码版本控制和发布流程两个核心部分。代码版本控制通常使用Git等工具进行管理,而发布流程则通过微信开发者工具和微信公众平台完成。合理的版本管理能够确保开发团队高效协作,同时保证线上版本的稳定性。
一个典型的小程序项目目录结构如下:
project/
├── pages/
│ ├── index/
│ │ ├── index.js
│ │ ├── index.json
│ │ ├── index.wxml
│ │ └── index.wxss
│ └── logs/
│ ├── logs.js
│ ├── logs.json
│ ├── logs.wxml
│ └── logs.wxss
├── utils/
├── app.js
├── app.json
├── app.wxss
└── project.config.json
Git分支策略
对于团队开发的小程序项目,推荐采用Git Flow分支策略:
- master分支:存放稳定版本代码,对应线上生产环境
- develop分支:日常开发集成分支
- feature分支:功能开发分支,从develop分支创建
- release分支:版本发布准备分支
- hotfix分支:紧急修复分支
示例创建feature分支命令:
git checkout -b feature/user-auth develop
小程序版本号规范
微信小程序版本号遵循语义化版本控制(SemVer)规范,格式为:主版本号.次版本号.修订号
(MAJOR.MINOR.PATCH)
版本号变更规则:
- MAJOR:不兼容的API修改
- MINOR:向下兼容的功能新增
- PATCH:向下兼容的问题修正
在project.config.json
中配置版本号:
{
"setting": {
"urlCheck": true,
"es6": true,
"postcss": true,
"minified": true
},
"libVersion": "2.10.4",
"appid": "wx1234567890abcdef",
"projectname": "my-miniprogram",
"version": "1.2.0",
"description": "小程序描述信息"
}
开发环境配置
微信开发者工具提供多种环境配置:
- 本地开发环境:使用开发者工具的模拟器
- 测试环境:配置测试服务器域名
- 预发布环境:与生产环境配置相同
- 生产环境:正式线上环境
环境配置示例代码:
// config.js
const env = {
develop: {
baseUrl: 'https://dev.api.example.com'
},
trial: {
baseUrl: 'https://staging.api.example.com'
},
release: {
baseUrl: 'https://api.example.com'
}
}
const currentEnv = env[__wxConfig.envVersion || 'release']
module.exports = currentEnv
代码提交规范
推荐使用Angular提交规范:
<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
常见type类型:
- feat:新功能
- fix:bug修复
- docs:文档变更
- style:代码格式调整
- refactor:代码重构
- test:测试相关
- chore:构建过程或辅助工具变更
示例提交信息:
feat(user): 新增用户登录功能
添加微信授权登录功能模块
包含login页面和用户信息存储
关联issue #123
持续集成与自动化
小程序支持CI/CD流程自动化:
- 使用微信开发者工具的CLI模式
- 结合Jenkins/GitHub Actions等CI工具
- 自动化构建和上传代码
GitHub Actions示例配置:
name: Build and Deploy
on:
push:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: npm install
- name: Build
run: npm run build
- name: Upload to WeChat
uses: wechat-miniprogram/miniprogram-ci-action@v1
with:
appid: ${{ secrets.APPID }}
privateKey: ${{ secrets.PRIVATE_KEY }}
version: ${{ github.sha }}
desc: 'Auto deploy by GitHub Actions'
小程序发布流程
完整的发布流程包括以下步骤:
- 代码审核:团队内部代码审查
- 构建测试:在测试环境验证功能
- 版本提交:通过开发者工具上传代码
- 体验版测试:生成体验版供测试人员验证
- 提交审核:在微信公众平台提交审核
- 发布上线:审核通过后发布
上传代码命令示例:
miniprogram-ci upload \
--pp ./demo \
--pkp ./private.key \
--appid wx1234567890abcdef \
--uv 1.2.0 \
--desc '版本描述信息' \
--robot 1
版本回滚机制
当线上版本出现严重问题时,需要快速回滚:
- 在微信公众平台找到历史版本
- 选择要回滚的版本
- 点击"回滚"按钮
- 确认回滚操作
也可以通过API实现自动化回滚:
const ci = require('miniprogram-ci')
const project = new ci.Project({
appid: 'wx1234567890abcdef',
type: 'miniProgram',
projectPath: './',
privateKeyPath: './private.key'
})
ci.rollback({
project,
version: '1.1.0', // 要回滚的版本号
robot: 1 // 机器人编号
})
多环境配置管理
复杂项目需要管理多套环境配置:
- 使用不同的
project.config.json
文件 - 通过环境变量切换配置
- 动态注入配置参数
动态配置示例:
// 根据编译模式获取不同配置
const configs = {
develop: {
apiBase: 'https://dev.api.com',
debug: true
},
trial: {
apiBase: 'https://test.api.com',
debug: true
},
release: {
apiBase: 'https://api.com',
debug: false
}
}
const currentConfig = configs[__wxConfig.envVersion || 'release']
App({
onLaunch() {
this.globalData.config = currentConfig
}
})
灰度发布策略
微信小程序支持分阶段发布:
- 按用户比例灰度:逐步扩大用户范围
- 按设备ID灰度:特定设备优先体验
- 按地域灰度:特定地区优先发布
配置灰度发布示例:
// 检查是否在灰度名单中
function isInGrayList(userId) {
const grayList = [
'user123',
'user456',
// 其他灰度用户ID
]
return grayList.includes(userId)
}
// 根据灰度状态加载不同模块
if (isInGrayList(getApp().globalData.userId)) {
require('./new-feature')
} else {
require('./old-feature')
}
版本兼容性处理
处理新旧版本兼容问题的方法:
- 接口版本控制
- 数据格式兼容
- 功能降级方案
接口版本控制示例:
// 请求头中添加版本信息
const request = (url, data) => {
return new Promise((resolve, reject) => {
wx.request({
url,
data,
header: {
'X-API-Version': '1.2',
'X-MiniProgram-Version': getApp().globalData.version
},
success(res) {
resolve(res.data)
},
fail(err) {
reject(err)
}
})
})
}
小程序分包策略
大型小程序应采用分包加载优化:
- 主包包含核心功能
- 分包按功能模块划分
- 预下载策略优化体验
分包配置示例:
// app.json
{
"pages": [
"pages/index",
"pages/logs"
],
"subPackages": [
{
"root": "packageA",
"pages": [
"pages/cat",
"pages/dog"
]
},
{
"root": "packageB",
"pages": [
"pages/apple",
"pages/banana"
]
}
],
"preloadRule": {
"pages/index": {
"network": "all",
"packages": ["packageA"]
}
}
}
性能监控与错误追踪
发布后需要持续监控:
- 使用微信自带的数据分析
- 接入第三方监控平台
- 自定义性能埋点
自定义错误监控示例:
// 全局错误捕获
App({
onError(err) {
wx.request({
url: 'https://monitor.api.com/log',
method: 'POST',
data: {
timestamp: Date.now(),
error: err.stack,
version: getApp().globalData.version,
scene: getApp().globalData.scene
}
})
}
})
// 接口错误监控
const monitoredRequest = (options) => {
const startTime = Date.now()
return new Promise((resolve, reject) => {
wx.request({
...options,
success(res) {
logPerformance(startTime, options.url, 'success')
resolve(res)
},
fail(err) {
logPerformance(startTime, options.url, 'fail')
reject(err)
}
})
})
}
小程序更新机制
处理小程序强制更新和可选更新:
// app.js
const updateManager = wx.getUpdateManager()
updateManager.onCheckForUpdate(function(res) {
console.log('是否有新版本:', res.hasUpdate)
})
updateManager.onUpdateReady(function() {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success(res) {
if (res.confirm) {
updateManager.applyUpdate()
}
}
})
})
updateManager.onUpdateFailed(function() {
wx.showToast({
title: '更新失败',
icon: 'none'
})
})
多团队协作开发
大型项目多团队协作方案:
- 使用Git子模块管理公共组件
- 制定统一的代码规范
- 定期同步主分支变更
子模块添加示例:
git submodule add https://github.com/team/common-components.git
组件引用示例:
// 在page的json配置中
{
"usingComponents": {
"common-header": "/common-components/header/header"
}
}
小程序代码审核要点
提高审核通过率的技巧:
- 确保功能完整可用
- 提供清晰的测试账号
- 检查敏感权限使用
- 核实内容合规性
- 避免诱导分享行为
审核注意事项:
- 用户隐私协议完整
- 收集用户数据需声明
- 内容安全无违规
- 无强制授权要求
- 无虚假功能提示
长期版本维护策略
长期项目的版本维护方法:
- 建立版本支持周期表
- 制定废弃功能迁移计划
- 维护变更日志(CHANGELOG)
变更日志示例:
# 变更日志
## [1.2.0] - 2023-05-15
### 新增
- 用户个人中心页面
- 订单评价功能
### 变更
- 优化支付流程
- 调整首页布局
### 修复
- 修复登录态失效问题
- 解决图片加载闪烁问题
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益,请来信告知我们删除。邮箱:cc@cccx.cn
上一篇:小程序的调试与测试工具