微信小程序 CI/CD 实践
使用 GitLab CI/CD 实现微信小程序的自动化构建和部署
部署脚本示例
以下是部署脚本示例,目前只在 Taro 项目中测试通过,仅供参考
js
1/** 2 * 上传小程序代码到微信平台 3 * https://developers.weixin.qq.com/miniprogram/dev/devtools/ci.html 4 */ 5 6const ci = require('miniprogram-ci') 7const fs = require('fs') 8const path = require('path') 9 10/** 从运行时参数中获取 appid version desc projectPath */ 11const main = async () => { 12 const { APP_ID, VERSION, DESC, PROJECT_PATH } = process.env 13 14 console.log('APP_ID', APP_ID) 15 console.log('VERSION', VERSION) 16 console.log('DESC', DESC) 17 console.log('PROJECT_PATH', PROJECT_PATH) 18 19 // 读取 PROJECT_PATH 目录下的 project.config.json 文件,移除 json 文件中的 miniprogramRoot 属性 20 // 参见这个 issue: https://github.com/NervJS/taro/issues/15947 21 const projectConfigPath = path.join(PROJECT_PATH, 'project.config.json') 22 const projectConfig = JSON.parse(fs.readFileSync(projectConfigPath, 'utf8')) 23 delete projectConfig.miniprogramRoot 24 fs.writeFileSync(projectConfigPath, JSON.stringify(projectConfig, null, 2)) 25 26 const project = new ci.Project({ 27 appid: APP_ID, 28 type: 'miniProgram', 29 projectPath: PROJECT_PATH, 30 privateKeyPath: './private.key', 31 ignores: ['node_modules/**/*'], 32 }) 33 34 // 上传 35 console.log('\x1b[32m开始上传小程序代码到微信平台...\x1b[0m') 36 const uploadResult = await ci.upload({ 37 project, 38 version: VERSION, 39 desc: DESC, 40 setting: { 41 es6: true, 42 minifyJS: true, 43 minifyWXML: true, 44 minifyWXSS: true, 45 }, 46 onProgressUpdate: console.log, 47 }) 48 49 console.log('\x1b[32m上传结果\x1b[0m') 50 console.log(uploadResult) 51 console.log('\x1b[32m上传动作已结束\x1b[0m') 52 53 console.log('\x1b[32m请前往微信公众平台查看上传结果并设置为体验版: https://mp.weixin.qq.com/wxamp/wacodepage/getcodepage?token=2141894780&lang=zh_CN\x1b[0m') 54 55 process.exit(0) 56} 57 58main()