默认分类

微信小程序使用Hbuildx+jenkins流水线自动构建与发布

任务

关于【小程序体验版本】jenkins自动构建与发布

下周研究一下 使用Hbuilder cli工具和jenkins自动发布小程序体验版本

  1. 建立一个windows的虚拟机(使用vSphereClient创建),安装hbuilder和jenkins
  2. 配置jenkins脚本

参考下面链接
https://hx.dcloud.net.cn/cli/publish-mp-weixin

1、准备服务器

windows server 2022

ip:10.65.90.14

需安装软件:

HbuilderX 免安装

git

nodejs

java

jenkins 2.346.2

微信开发者工具

使用svn拉取代码应还需要安装svn客户端

2、安装微信开发者客户端并扫码登录,并开启服务端口(默认关闭)

微信截图_20220831094539.png

3、测试环境使用gitee作为代码拉取地址

配置gitee,配置完成测试链接,成功

微信截图_20220831094658.png

上边API令牌使用私人令牌

微信截图_20220831094750.png

4、建立项目执行构建上传操作

选择流水线

5、流水线脚本

properties([
    parameters([
        [$class: "ChoiceParameter", 
            choiceType: "PT_SINGLE_SELECT", 
            description: "Select the APP_TAG Name from the Dropdown List", 
            filterLength: 1, 
            filterable: false, 
            name: "APP_TAG", 
            randomName: "choice-parameter-5631314439613978", 
            script: [
                $class: "GroovyScript", 
                fallbackScript: [
                    classpath: [], 
                    sandbox: true, 
                    script: 
                        '''return["Could not get Env"]'''
                ], 
                script: [
                    classpath: [], 
                    sandbox: true, 
                    script: 
                       '''return["PH","WHP","KY","WYC"]'''
                ]
            ]
        ], 
        [$class: "CascadeChoiceParameter", 
            choiceType: "PT_SINGLE_SELECT", 
            description: "Select the GIT_TAG from the Dropdown List", 
            filterLength: 1, 
            filterable: false, 
            name: "GIT_TAG", 
            randomName: "choice-parameter-5631314456178619", 
            referencedParameters: "APP_TAG", 
            script: [
                $class: "GroovyScript", 
                fallbackScript: [
                    classpath: [], 
                    sandbox: true, 
                    script: 
                        '''return["Could not get Environment from GIT_TAG Param"]'''
                ], 
                script: [
                    classpath: [], 
                    sandbox: true, 
                    script: 
                        ''' if (APP_TAG.equals("PH")){
                                return["uni-phcomp"]
                            }
                            else if(APP_TAG.equals("WHP")){
                                return["uni-whpcomp"]
                            }
                            else if(APP_TAG.equals("WYC")){
                                return["uni-onlinecar"]
                            }
                            else if(APP_TAG.equals("KY")){
                                return["uni-kycomp"]
                            }
                        '''
                ]
            ]
        ],
        [
            $class: "CascadeChoiceParameter", 
            choiceType: "PT_SINGLE_SELECT", 
            description: "key文件路径", 
            filterLength: 1, 
            filterable: false, 
            name: 'KEY', 
            randomName: "choice-parameter-5631319756196371", 
            referencedParameters: "APP_TAG", 
            script: [
                $class: "GroovyScript", 
                fallbackScript: [
                    classpath: [], 
                    sandbox: true, 
                    script: 
                        '''return["Could not get Environment from KEY Param"]'''
                ], 
                script: [
                    classpath: [], 
                    sandbox: true, 
                    script: 
                        ''' if (APP_TAG.equals("PH")){
                                return["D:/key/phcomp.wxfc809685bbb644fb.key"]
                            }
                            else if(APP_TAG.equals("WHP")){
                                return["D:/key/whpcomp-private.wxf95996f0f39b3b1b.key"]
                            }
                            else if(APP_TAG.equals("WYC")){
                                return["TEST2"]
                            }
                            else if(APP_TAG.equals("KY")){
                                return["D:/key/kycomp.wx44ea4c8f3250d9c4.key"]
                            }
                        '''
                ]
            ]
        ],
        [
            $class: "CascadeChoiceParameter", 
            choiceType: "PT_SINGLE_SELECT", 
            description: "设置APPID", 
            filterLength: 1, 
            filterable: false, 
            name: 'APP_ID', 
            randomName: "choice-parameter-5631286456198542", 
            referencedParameters: "APP_TAG", 
            script: [
                $class: "GroovyScript", 
                fallbackScript: [
                    classpath: [], 
                    sandbox: true, 
                    script: 
                        '''return["Could not get Environment from APP_ID Param"]'''
                ], 
                script: [
                    classpath: [], 
                    sandbox: true, 
                    script: 
                        ''' if (APP_TAG.equals("PH")){
                                return["wxfc809685bbb644fb"]
                            }
                            else if(APP_TAG.equals("WHP")){
                                return["wxf95996f0f39b3b1b"]
                            }
                            else if(APP_TAG.equals("WYC")){
                                return["wxbde776f92c9aab97"]
                            }
                            else if(APP_TAG.equals("KY")){
                                return["wx44ea4c8f3250d9c4"]
                            }
                        '''
                ]
            ]
        ]
    ])
])



pipeline {
    agent any
        environment{
        //KEY = 'D:/key/private.wx18ec3eb21c226bbe.key'
        //APP_ID = 'wx18ec3eb21c131234e'
        HbuildX_pass = 'Huildx的登录密码'
    }
        //parameters {
            //string defaultValue: '', description: '手动输入版本号,未输入则执行源码中配置的版本号', name: 'APP_Version', trim: true
    //}
        parameters {
            string defaultValue: '', description: '请输入发布描述', name: 'description', trim: true
    }
    stages {

        stage('pull uni') { 
            
            steps {
                   
                checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '', excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '', locations: [[cancelProcessOnExternalsFail: true, credentialsId: 'd651156a-971d-4c5a-9c2e-65a85104ef37', depthOption: 'infinity', ignoreExternalsOption: true, local: '.', remote: 'svn://10.80.0.30/uni_proj/uni-systems/${GIT_TAG}']], quietOperation: true, workspaceUpdater: [$class: 'CheckoutUpdater']])
                //checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '6bb316dc-3f79-4ed7-a706-088e55081568', url: 'https://gitee.com/qiyue5688/${GIT_TAG}.git']]])

            }
        }
        stage('npm install') {
            steps {
                bat """npm install"""
            }
        }
       // stage('open hbuildx') {
   
       //     steps {
       //         bat """D:\\HBuilderX\\start.bat"""
       //     }
       // }
        stage('build uni') {

            steps {
              bat """
D:\\HBuilderX\\cli.exe user login --username hbuildx的登录ID  --password %HbuildX_pass%

D:\\HBuilderX\\cli.exe project open --path C:/ProgramData/Jenkins/.jenkins/workspace/${env.JOB_NAME}

D:\\HBuilderX\\cli.exe publish --platform mp-weixin --project ${env.JOB_NAME} --upload true --appid %APP_ID% --description %description% --version %APP_Version% --privatekey %KEY%

D:\\HBuilderX\\cli.exe project list

D:\\HBuilderX\\cli.exe project close --path C:/ProgramData/Jenkins/.jenkins/workspace/${env.JOB_NAME}

D:\\HBuilderX\\cli.exe user logout"""
            }
        }

    }

}

注意:配置到生产环境hbuildx ID、APPID及key需要重新配置,此为个人账号测试

服务器需手动启动HBuildx,使用jenkins的bat命令行无法启动,原因未知

需配置变量:

APP_TAG:构建时选项,暂配置为WHP、PH、KY、WYC

GIT_TAG:拉取仓库的地址变量,应APP_TAG自动变更

KEY:需提前拷贝key文件到发布主机本地路径写死,或者key文件放置于拉取的代码目录并指定,或者可访问的远方目录

env.JOB_NAME:jenkisn项目名称,全局变量

APP_ID:小程序id

HbuildX_pass:hbuildx的登录密码

6、jenkins执行构建后,可以在微信小程序后台查看新构建上传版本

微信截图_20220831095418.png

排错

1、编译失败,报错如下图

微信截图_20220831095454.png

解决方法:

右键项目,使用命令行窗口打开所在目录

微信截图_20220831095553.png

依次执行命令:
npm cache clear --force

npm i

npm install dateformat

npm install js-base64

npm install random-string

再次执行编译操作
This is just a placeholder img.