Mini Program: Automated Deployment Scheme

The integration of CI/CD in the applet allows us to get a better experience in the release process and version control.

Mainly have the following advantages:

  1. The released version of the applet must be consistent with the code version of gitlab
  2. Solve the need to frequently switch the experience version during multiplayer development to improve release efficiency
  3. Solve the problem of switching back to the trial version after the online pre-review version
  4. According to the agreed small program version naming convention, the specific code version can be traced

Integrated CI/CD ideas

  1. Package applet
  2. With the help of the script call method in miniprogram-ci officially provided by the applet, combined with gitlab ci to execute the corresponding release script.
    a. When executing the script, you need to pass the version parameters, specify the release robot, and describe the description (here we use the commit message as the description)

Insert picture description here

Integrated development & release model

  1. Release the alpha version: the code is submitted to the test branch for automatic release (commit message is used as the description of the release). By default, the robot set in this branch is the experience version, and there is no need to switch the experience version for multiple releases.
  2. Publish the beta version: merge the test code into the preview branch and automatically package and submit it. The robot set in this branch is the beta version and needs to be submitted for review on the WeChat public platform. After the review is passed, the administrator sees that it is the beta version and releases it in grayscale.
  3. Release the official version: merge the test/preview code into the master branch, and automatically package and submit. The robot set in this branch is the official version and needs to be submitted for review on the WeChat public platform. After the review is passed, the administrator sees the online version and performs the full amount release.
    When developing, there is no need to upload code in the WeChat developer tool.
    Only the version released by the robot can be seen in the version management of the WeChat public platform, and there are no more versions submitted by individuals.
    Insert picture description here

Integration steps

1. Install miniprogram-ci

npm i miniprogram-ci -D

2. Prepare ci.js release script file

const ci = require('miniprogram-ci')

// gitlab-runner执行脚本时,传进来的参数
var params = process.argv.splice(2)
var version = params[0]
var robot = parseInt(params[1])
var desc = params.splice(2).join(' ')

// appid 和privateKeyPath需要设置下
const project = new ci.Project({
    
    
  appid: '',
  type: 'miniProgram',
  projectPath: './',
  privateKeyPath: './ci-private.key',
  ignores: ['node_modules/**/*', 'src/**/*']
})

/** 上传 */
async function upload({
    
     version = '0.0.0', desc = 'test', robot = 1 }) {
    
    
  await ci.upload({
    
    
    project,
    version,
    desc,
    setting: {
    
    
      es7: true,
      minify: true,
      autoPrefixWXSS: true
    },
    robot,
    onProgressUpdate: console.log
  })
}

upload({
    
     version, desc, robot })

For more release settings, please refer to the official documentation .

3. The key file

The specific location is in the applet management platform, development -> development settings -> applet code upload -> applet code upload key.
Place the generated key file in the root directory of the project, as shown in the figure:
Note: Only the administrator of the applet has the authority to generate the key file.
Insert picture description here

4. IP whitelist

Add the ip whitelist here, and the specified ip can execute the upload script.
Insert picture description here

5. .gitlab-ci.yml release file

We agree that the test branch is the branch that releases the experience version, the preview branch is the branch that releases the beta version, and the master branch is the branch that releases the official version. Both the beta version and the official version correspond to the online environment, and the alpha version corresponds to the test environment. Therefore, the packaging commands are different. They are npm run build and npm run build:test. The packaging commands need to be supported in package.json.
Execute the release script in gitlab-runner and pass in the required parameters.
Note: Every time you develop a new version, you need to manually modify the version number.

# test、preview和master分支默认支持线上部署

stages:
  - build
  - deploy

# 小程序版本设置,每次开发新版本需要手动更新
variables:
  PROJECT_VERSION: 1.0.0

# 安装依赖&&构建打包
build test:
  stage: build
  only:
    - test
  tags:
    - f2e
  script:
    - cnpm i
    - npm run build:test
    - node ci.js $PROJECT_VERSION.$CI_COMMIT_SHORT_SHA.alpha 1 $(git log -1 --pretty=%B)

build preview:
  stage: build
  only:
    - preview
  tags:
    - f2e
  script:
    - cnpm i
    - npm run build
    - node ci.js $PROJECT_VERSION.beta 2 $(git log -1 --pretty=%B)

build prd:
  stage: build
  only:
    - master
  tags:
    - f2e
  script:
    - cnpm i
    - npm run build
    - node ci.js $PROJECT_VERSION 3 $(git log -1 --pretty=%B)

package.json

{
    
    
	...
  "scripts": {
    
    
    ...
    "build": "cross-env NODE_ENV=production ./node_modules/.bin/wepy build --no-cache",
    "build:test": "cross-env NODE_ENV=test ./node_modules/.bin/wepy build --no-cache",
		...
  },
  ...
}

Guess you like

Origin blog.csdn.net/weixin_43972437/article/details/113448724