一. 新建打包工程
在gitlab上新建一个打包工程目录,比如auto_build_game_apk(https://gitlab.com/CocosTool/auto_build_game_apk),必须获取工程的master权限。
示例Cocos Hello World工程:https://gitlab.com/CocosTool/cocos_example
二. Runner配置
- 安装Runner
https://docs.gitlab.com/runner/install/
下载对应平台源文件,我这里以win10为例。
在任意位置,新建文件夹GitLab-Runner,将gitlab-runner-windows-386.exe放入重命名gitlab-runner.exe,然后命令行执行安装。 ```shell cd C:\GitLab-Runner //无用户安装,由于此时运行runner时使用的并不是当前系统账号,后面使用git的时候会有权限问题,不推荐 ./gitlab-runner.exe install
//以当前管理员账号安装 ./gitlab-runner.exe install —user ENTER-YOUR-USERNAME —password ENTER-YOUR-PASSWORD //如果提示账号不存在,尝试添加.\ ./gitlab-runner.exe install —user “.\ENTER-YOUR-USERNAME” —password “ENTER-YOUR-PASSWORD”
//启动runner ./gitlab-runner.exe start
//停止runner ./gitlab-runner.exe stop
2. 注册Runner<br />gitlab的工程界面,进入Settings => CI/CD => Runners settings 获取相关参数,如图:<br /><br />运行以下命令:```shellgitlab-runner register
输入您的GitLab实例URL:
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )https://gitlab.com/
输入您获得的令牌以注册Runner:
Please enter the gitlab-ci token for this runner4G257zVcnRJzxjGd3evs
输入Runner的描述,您可以稍后在GitLab的UI中更改:
Please enter the gitlab-ci description for this runnermy-runner
输入与Runner关联的标签,您可以稍后在GitLab的UI中更改:
Please enter the gitlab-ci tags for this runner (comma separated):my-tag
输入Runner执行程序:
Please enter the executor: parallels, shell, kubernetes, custom, docker-ssh, ssh, virtualbox, docker+machine, docker-ssh+machine, docker, docker-windows:shell
注册完成后可在gitlab页面看到一个新的runner:

我这边是将机器的tag命名为my-tag,这个后面写CI脚本时指定Runner需要用到。
如果启动时遇到FATAL: Failed to start gitlab-runner: The service did not start due to a logon failure.按以下步骤可解决:
计算机管理 => 服务 => gitlab-runner =>登录 重新输入密码即可
三. CI/CD脚本编写
新建脚本文件.gitlab-ci.yml:
stages:- prepare- build- noticeprepare_out:stage: preparescript:- python ./prepare.pyonly:- web- triggersartifacts:paths:- config/when: on_successtags:- my-tagbuild_apk_cocos:stage: buildscript:- python ./build_cocos.pyonly:variables:- $build_apk == "true"tags:- my-tagnotice_job:stage: noticescript:- python ./notice.pyonly:- web- triggerstags:- my-tag
- my-tag 替换成你自己的Runner的tag。
- only下面的配置表示只有在gitlab网页操作和Pipeline triggers时才会触发
only:- web- triggers
- 配置Pipeline triggers:
gitlab的工程界面,进入Settings => CI/CD => Pipeline triggers 配置相关参数
Add trigger 然后会获取一个token,页面下方会有具体请求的URL,variables用来传参数给执行runner的环境变量如:curl -X POST \-F token=TOKEN \-F "ref=REF_NAME" \-F "variables[RUN_NIGHTLY_BUILD]=true" \https://gitlab.com/api/v4/projects/15418684/trigger/pipeline
以cocos_example为例,用PostMan进行测试:
Post请求Url:https://gitlab.com/api/v4/projects/15418684/trigger/pipelineBody:token:437ad5074f67b38e5b97699f004a56ref:mastervariables[branch]:mastervariables[client_version]:1variables[client_version_code]:100variables[dingding_open]:truevariables[build_apk_res]:falsevariables[build_apk_dev]:truevariables[cocos_version_code]:212variables[build_apk]:truevariables[hotupdate_packageUrl]:https://cdn.wxgame.youxi765.com/ylcy/remote_assetsvariables[hotupdate_packageVersion]:2.0.0.8variables[outPut_suffix]:patch_2008variables[is_skip_u8]:truevariables[gameConfig_client_version]:200011018002variables[outPut_dir]:D:/outPutDir/apk
各个参数说明:
branch:要打包apk的分支client_version:apk版本名client_version_code:apk版本号dingding_open:是否打开钉钉通知build_apk_res:是否连接正式服build_apk_dev:是否连接测试服cocos_version_code:cocos creator的版本build_apk:是否构建apkhotupdate_packageUrl:热更新地址hotupdate_packageVersion:热更新版本号outPut_suffix:生成的apk命名的后缀添加is_skip_u8:是否跳过U8,登录广告等接口默认成功gameConfig_client_version:游戏客户端版本号outPut_dir:apk输出路径//以下可不传,用默认的配置game_name:游戏名字game_config_path:游戏配置文件的路径hotupdate_config_path:热更新文件的路径
- 修改Python脚本
ciparams.py文件: ```python 修改为当前目标打包工程路径 self.root_dir = r’H:/Code/ylcy’
修改游戏配置文件的路径 self.game_config_path = r’/client/assets/resources/game_config/game_config.json’ if “game_config_path” in os.environ: self.game_config_path = os.environ[“game_config_path”]
修改游戏热更新文件的路径 self.hotupdate_config_path = r’/client/PluginDir/hotUpdate/cfg.json’ if “hotupdate_config_path” in os.environ: self.hotupdate_config_path = os.environ[“hotupdate_config_path”]
<br />game_config的修改需要游戏自己具体实现,cocos_example中已经实现的修改配置```json{"version":124011015001,"is_dev":true,"is_skip_u8":true}
hotupdate_config_path依赖cocos的构建完自动替换热更新文件的插件.
- 修改安卓工程
修改gradle脚本支持修改Apk的versionCode,versionName,文件名字及Apk输出路径
proj.android-studio => app => build.gradle ``` defaultConfig { if (project.hasProperty(‘VERSION_CODE_PARA’)) {
} else{versionCode Integer.parseInt(VERSION_CODE_PARA)
} if (project.hasProperty(‘VERSION_NAME_PARA’)) {versionCode 1
} else{versionName VERSION_NAME_PARA
} }versionName "1.0.0"
buildTypes { release { android.applicationVariants.all { variant -> variant.outputs.all { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith(‘.apk’)) { def fileName = outputFile.name; if (project.hasProperty(‘OUT_PUT_APK_FILE_NAME’)) { fileName = “${OUT_PUT_APK_FILE_NAME}”; } if (project.hasProperty(‘OUT_PUT_DIR_PARA’)) { File output_dir1 = file(“${OUT_PUT_DIR_PARA}”);
println "输出文件位置: " + output_dir1variant.getPackageApplication().outputDirectory = output_dir1outputFileName = fileName;println "输出文件位置: " + output.outputFile} else {outputFileName = fileNameprintln "输出文件位置: " + output.outputFile}}}}}
6. 开始正式打包<br />在Runner的机子上,先手动构建一次Cocos Creator工程,将打包相关配置都配置好,确认Android studio能生成正常的Apk.<br />用PostMan抛送Post请求,触发Pipeline trigger,开始自动构建<br />notice_job 用来触发打包完成通知<br />由于打包完成后包在本地,所以需要在本机上建了一个web服务,支持指定目录的文件下载。参考[Win10搭建web服务实现文件共享](http://www.solves.com.cn/it/wlyx/fwq/2019-05-31/602.html)<br />修改notice.py文件:```pythonif ci_params.dingding_open != 'false':apk_name = ci_params.apk_namemsg = '#### 打包完成 'if ci_params.build_apk != 'false':root_url = 'YOUR_WEB_DOWNLOAD_URL'#替换下载地址,例如root_url = 'http://10.0.19.61/apk/'if ci_params.build_apk_dev != 'false':msg += '\n> 安卓_连测试服包:'+apk_name+'[点击下载](' + root_url + apk_name + ')'if ci_params.build_apk_res != 'false':msg += '\n\n> 安卓_连正式服包:'+apk_name+'[点击下载](' + root_url + apk_name + ')'dingding.sendMarkdown(cmd_logger, '打包完成,抓紧测试', msg, True)
修改dingding.py文件:
dingding_url1,dingding_url2替换成对应游戏打包群的地址
