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

語言: 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(推薦)

首先需要在小程式配置並下載金鑰,參考https://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

參考連結