问题缘由
在微信小程序开发中,先要在本地使用微信开发者工具进行调试,如果需要在线测试,则需要将编译好的代码上传。
目前,只能通过微信开发者工具手动点击上传,而该过程无法与持续集成/持续部署结合在一起,本文就是为了解决能够实现自动化和持续部署的难题
实现原理
微信 miniprogram-ci 库,提供了命令行调用方式,其中就包括上传的命令,我们可以通过脚本实现自动化集成
步骤
安装并配置 GitLab Runner
这部分文档在 Install GitLab Runner on macOS
安装
首先需要在本机上安装 GitLab Runner, 由于微信开发者工具只提供了 mac 和 windows 版本,所以目前只能在这两种系统上实现持续集成,本文讲述在 mac 的具体实现, windows 上的实现与此类似,只是相关命令和路径需要做些变更
注册 GitLab Runner
编写 .gitlab-ci.yml
在此之前,你需要对GitLab-CI有一定的掌握,这部分资料参考下方的相关文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| image: node:alpine
before_script: - export APP_ENV=testing - yarn config set registry 'https://registry.npm.taobao.org' stages: - build - deploy
variables: NPM_CONFIG_CACHE: "/cache/npm" YARN_CACHE_FOLDER: "/cache/yarn" DOCKER_DRIVER: overlay2 build-package: stage: build dependencies: [] cache: key: "$CI_COMMIT_REF_NAME" policy: pull paths: - node_modules script: - if [ ! -d "node_modules" ]; then - yarn install --cache-folder /cache/yarn - fi - yarn build - cp deploy/project.config.json ./dist/project.config.json artifacts: name: "wxpkg-dlkhgl-$CI_COMMIT_TAG" untracked: false paths: - dist only: - tags tags: - docker release: stage: deploy before_script: [] dependencies: - build-package variables: GIT_STRATEGY: none script: - yarn global add miniprogram-ci - if [[ -z "$CI_COMMIT_TAG" ]];then - CI_COMMIT_TAG="测试版:$(date '+%Y-%m-%d')" - fi - COMMIT_MESSAGE=`cat ./dist/release.txt` - rm -f ./dist/release.txt - LF=$'\\\x0A' - echo $UPLOAD_PRIVATE_KEY | sed -e "s/-----BEGIN RSA PRIVATE KEY-----/&${LF}/" -e "s/-----END RSA PRIVATE KEY-----/${LF}&${LF}/" | sed -e "s/[^[:blank:]]\{64\}/&${LF}/g" > private.key - miniprogram-ci upload --project-path=$PWD/dist --appid=$APP_ID --upload-version=$CI_COMMIT_TAG --private-key-path=$PWD/private.key --upload-description="$COMMIT_MESSAGE" environment: name: production url: https://mp.weixin.qq.com/wxamp/wacodepage/getcodepage only: - tags - master
|
注意,在设置-CI/CD-变量中,需要添加两个变量:
APP_ID 小程序 appid
UPLOAD_PRIVATE_KEY 小程序代码上传密钥, 需要在小程序后台获取
由于配置的密钥,在运行时过去到是单行文本形式,需要转换为多行原始格式,并输出到文件,以便上传使用
于是使用一下命令- LF=$'\\\x0A' - echo $UPLOAD_PRIVATE_KEY | sed -e "s/-----BEGIN RSA PRIVATE KEY-----/&${LF}/" -e "s/-----END RSA PRIVATE KEY-----/${LF}&${LF}/" | sed -e "s/[^[:blank:]]\{64\}/&${LF}/g" > private.key
相关文档