Gitlab-CI自動(dòng)化CocosCreator安卓打包之Runner配置和CI腳本說(shuō)明
一. 新建打包工程
在gitlab上新建一個(gè)打包工程目錄病曾,比如auto_build_game_apk(https://gitlab.com/CocosTool/auto_build_game_apk)箩退,必須獲取工程的master權(quán)限。
示例Cocos Hello World工程:https://gitlab.com/CocosTool/cocos_example
二. Runner配置
-
安裝Runner
https://docs.gitlab.com/runner/install/
下載對(duì)應(yīng)平臺(tái)源文件,我這里以win10為例。在任意位置,新建文件夾GitLab-Runner,將gitlab-runner-windows-386.exe放入重命名gitlab-runner.exe例嘱,然后命令行執(zhí)行安裝。
cd C:\GitLab-Runner //無(wú)用戶安裝宁舰,由于此時(shí)運(yùn)行runner時(shí)使用的并不是當(dāng)前系統(tǒng)賬號(hào)拼卵,后面使用git的時(shí)候會(huì)有權(quán)限問(wèn)題,不推薦 ./gitlab-runner.exe install //以當(dāng)前管理員賬號(hào)安裝 ./gitlab-runner.exe install --user ENTER-YOUR-USERNAME --password ENTER-YOUR-PASSWORD //如果提示賬號(hào)不存在蛮艰,嘗試添加.\ ./gitlab-runner.exe install --user ".\ENTER-YOUR-USERNAME" --password "ENTER-YOUR-PASSWORD" //啟動(dòng)runner ./gitlab-runner.exe start //停止runner ./gitlab-runner.exe stop
-
注冊(cè)Runner
gitlab的工程界面腋腮,進(jìn)入Settings => CI/CD => Runners settings 獲取相關(guān)參數(shù),如圖:運(yùn)行以下命令:
gitlab-runner register
輸入您的GitLab實(shí)例URL:
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com ) https://gitlab.com/
輸入您獲得的令牌以注冊(cè)Runner:
Please enter the gitlab-ci token for this runner 4G257zVcnRJzxjGd3evs
輸入Runner的描述壤蚜,您可以稍后在GitLab的UI中更改:
Please enter the gitlab-ci description for this runner my-runner
輸入與Runner關(guān)聯(lián)的標(biāo)簽即寡,您可以稍后在GitLab的UI中更改:
Please enter the gitlab-ci tags for this runner (comma separated): my-tag
輸入Runner執(zhí)行程序:
Please enter the executor: parallels, shell, kubernetes, custom, docker-ssh, ssh, virtualbox, docker+machine, docker-ssh+machine, docker, docker-windows: shell
注冊(cè)完成后可在gitlab頁(yè)面看到一個(gè)新的runner:
我這邊是將機(jī)器的tag命名為my-tag,這個(gè)后面寫(xiě)CI腳本時(shí)指定Runner需要用到袜刷。
如果啟動(dòng)時(shí)遇到FATAL: Failed to start gitlab-runner: The service did not start due to a logon failure.按以下步驟可解決:
計(jì)算機(jī)管理 => 服務(wù) => gitlab-runner =>登錄 重新輸入密碼即可
三. CI/CD腳本編寫(xiě)
新建腳本文件.gitlab-ci.yml:
stages:
- prepare
- build
- notice
prepare_out:
stage: prepare
script:
- python ./prepare.py
only:
- web
- triggers
artifacts:
paths:
- config/
when: on_success
tags:
- my-tag
build_apk_cocos:
stage: build
script:
- python ./build_cocos.py
only:
variables:
- $build_apk == "true"
tags:
- my-tag
notice_job:
stage: notice
script:
- python ./notice.py
only:
- web
- triggers
tags:
- my-tag
- my-tag 替換成你自己的Runner的tag聪富。
- only下面的配置表示只有在gitlab網(wǎng)頁(yè)操作和Pipeline triggers時(shí)才會(huì)觸發(fā)
only:
- web
- triggers
-
配置Pipeline triggers:
gitlab的工程界面,進(jìn)入Settings => CI/CD => Pipeline triggers 配置相關(guān)參數(shù)
Add trigger 然后會(huì)獲取一個(gè)token著蟹,頁(yè)面下方會(huì)有具體請(qǐng)求的URL墩蔓,variables用來(lái)傳參數(shù)給執(zhí)行runner的環(huán)境變量如: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進(jìn)行測(cè)試:
Post請(qǐng)求Url: https://gitlab.com/api/v4/projects/15418684/trigger/pipeline Body: token:437ad5074f67b38e5b97699f004a56 ref:master variables[branch]:master variables[client_version]:1 variables[client_version_code]:100 variables[dingding_open]:true variables[build_apk_res]:false variables[build_apk_dev]:true variables[cocos_version_code]:212 variables[build_apk]:true variables[hotupdate_packageUrl]:https://cdn.wxgame.youxi765.com/ylcy/remote_assets variables[hotupdate_packageVersion]:2.0.0.8 variables[outPut_suffix]:patch_2008 variables[is_skip_u8]:true variables[gameConfig_client_version]:200011018002 variables[outPut_dir]:D:/outPutDir/apk
各個(gè)參數(shù)說(shuō)明:
branch:要打包apk的分支 client_version:apk版本名 client_version_code:apk版本號(hào) dingding_open:是否打開(kāi)釘釘通知 build_apk_res:是否連接正式服 build_apk_dev:是否連接測(cè)試服 cocos_version_code:cocos creator的版本 build_apk:是否構(gòu)建apk hotupdate_packageUrl:熱更新地址 hotupdate_packageVersion:熱更新版本號(hào) outPut_suffix:生成的apk命名的后綴添加 is_skip_u8:是否跳過(guò)U8萧豆,登錄廣告等接口默認(rèn)成功 gameConfig_client_version:游戲客戶端版本號(hào) outPut_dir:apk輸出路徑 //以下可不傳奸披,用默認(rèn)的配置 game_name:游戲名字 game_config_path:游戲配置文件的路徑 hotupdate_config_path:熱更新文件的路徑
-
修改Python腳本
ciparams.py文件:修改為當(dāng)前目標(biāo)打包工程路徑 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"]
game_config的修改需要游戲自己具體實(shí)現(xiàn),cocos_example中已經(jīng)實(shí)現(xiàn)的修改配置
{ "version":124011015001, "is_dev":true, "is_skip_u8":true }
hotupdate_config_path依賴cocos的構(gòu)建完自動(dòng)替換熱更新文件的插件.
-
修改安卓工程
修改gradle腳本支持修改Apk的versionCode,versionName,文件名字及Apk輸出路徑
proj.android-studio => app => build.gradle
defaultConfig { if (project.hasProperty('VERSION_CODE_PARA')) { versionCode Integer.parseInt(VERSION_CODE_PARA) } else{ versionCode 1 } if (project.hasProperty('VERSION_NAME_PARA')) { versionName VERSION_NAME_PARA } else{ 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_dir1 variant.getPackageApplication().outputDirectory = output_dir1 outputFileName = fileName; println "輸出文件位置: " + output.outputFile } else { outputFileName = fileName println "輸出文件位置: " + output.outputFile } } } } }
-
開(kāi)始正式打包
在Runner的機(jī)子上涮雷,先手動(dòng)構(gòu)建一次Cocos Creator工程阵面,將打包相關(guān)配置都配置好,確認(rèn)Android studio能生成正常的Apk.
用PostMan拋送Post請(qǐng)求洪鸭,觸發(fā)Pipeline trigger,開(kāi)始自動(dòng)構(gòu)建
notice_job 用來(lái)觸發(fā)打包完成通知
由于打包完成后包在本地膜钓,所以需要在本機(jī)上建了一個(gè)web服務(wù),支持指定目錄的文件下載卿嘲。參考Win10搭建web服務(wù)實(shí)現(xiàn)文件共享修改notice.py文件:
if ci_params.dingding_open != 'false': apk_name = ci_params.apk_name msg = '#### 打包完成 ' 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> 安卓_連測(cè)試服包:'+apk_name+'[點(diǎn)擊下載](' + root_url + apk_name + ')' if ci_params.build_apk_res != 'false': msg += '\n\n> 安卓_連正式服包:'+apk_name+'[點(diǎn)擊下載](' + root_url + apk_name + ')' dingding.sendMarkdown(cmd_logger, '打包完成,抓緊測(cè)試', msg, True)
修改dingding.py文件:
dingding_url1夫壁,dingding_url2替換成對(duì)應(yīng)游戲打包群的地址