微信小程序配置自動化部署

語言: CN / TW / HK

方式一:命令行V2

```

本地構建

npm run build:weapp-sit

命令行工具所在位置

cd /Applications/wechatwebdevtools.app/Contents/MacOS/

登錄

./cli login

打開啟動工具(定位到項目)

./cli -o /Users/niceBoy/Documents/workingProject/perpetual/perpetual-ts.git

小程序預覽

./cli preview --project /Users/niceBoy/Documents/workingProject/perpetual/perpetual-ts.git

小程序上傳

./cli upload --project /Users/niceBoy/Documents/workingProject/perpetual/perpetual-ts.git -v 2.20.1 -d '小程序自動發版測試' # 查看所有命令 ./cli --lang zh -h ```

方式二:CI(推薦)

首先需要在小程序配置並下載密鑰,參考http://developers.weixin.qq.com/miniprogram/dev/devtools/ci.html

CI初始化

``` // index.js const shell = require('shelljs'); const ci = require('miniprogram-ci'); const appid = require('../project.config.json').appid; const pkg = require('../package.json');

const desc = '小程序上傳'; const arguments = process.argv.splice(2); console.log('------小程序環境------', arguments[0]);

const projectCi = new ci.Project({ appid, type: 'miniProgram', projectPath: process.cwd(), privateKeyPath: process.cwd() + '/wechat/private.key', ignores: ['node_modules/*/'] });

async function commit() { const _gitLog = await getLog(); return Object.assign({}, _gitLog, { message: _gitLog.message.split(':')[1] || _gitLog.message.split(':')[1] || desc }); }

function getLog() { let _cmd = git log --no-merges -1 \ --date=iso --pretty=format:'{"author": "%aN","message": "%s"},' \ [email protected] | \ perl -pe 'BEGIN{print "["}; END{print "]\n"}' | \ perl -pe 's/},]/}]/'; return new Promise((resolve, reject) => { shell.exec(_cmd, (code, stdout, stderr) => { if (code) { reject(stderr); } else { const obj = Object.assign({}, JSON.parse(stdout)[0], { branch: shell.exec('git symbolic-ref --short -q HEAD').stdout }); resolve(obj); } }); }); }

module.exports = { projectName: pkg.name, // 項目名,用於後台設置的賬號密碼匹配 version: pkg.version, // 本次發佈的版本號 desc: 小程序環境:${arguments[0]}, // 上傳備註信息 projectCi, commit }; ```

預覽代碼

``` // config.preview.js const { projectName, version, desc, projectCi } = require('./index'); const ci = require('miniprogram-ci');

console.log('------開始預覽------');

(async () => { try { const previewResult = await ci.preview({ project: projectCi, desc, setting: { es6: true }, qrcodeFormat: 'image', qrcodeOutputDest: process.cwd() + '/wechat/preview_destination.jpg' // pagePath: 'pages/index/index', // 預覽頁面 // searchQuery: 'a=1&b=2', // 預覽參數 [注意!]這裏的&字符在命令行中應寫成轉義字符& }); console.log('------預覽成功------'); console.log(previewResult); } catch (error) { console.log('------預覽失敗------'); console.error(error); } finally { console.log('------預覽完成------'); } })(); ```

上傳代碼

``` // config.upload.js const { projectName, version, desc, projectCi, commit } = require('./index'); const ci = require('miniprogram-ci');

console.log('------開始上傳------');

(async () => { try { const getlogInfo = await commit(); console.log(getlogInfo); const uploadResult = await ci.upload({ project: projectCi, version, desc: ${desc} 開發分支:${getlogInfo.branch} 描述:${getlogInfo.message} 作者:${getlogInfo.author}, robot: 2, // 本地部署機器人為 2,Jenkins部署機器人為 1 setting: { minify: true } }); console.log('------上傳成功------'); console.log(uploadResult); } catch (error) { console.log('------上傳失敗------'); console.error(error); } finally { console.log('------上傳完成------'); } })(); ```

運行

node wechat/config.preview.js node wechat/config.upload.js

查看結果

image.png

image.png

參考鏈接