Gitlab-CI自動化打包之Runner配置和CI腳本說明

Gitlab-CI自動化CocosCreator安卓打包之Runner配置和CI腳本說明

一. 新建打包工程

在gitlab上新建一個打包工程目錄,比如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配置

  1. 安裝Runner

    https://docs.gitlab.com/runner/install/
    下載對應(yīng)平臺源文件,我這里以win10為例。

    在任意位置,新建文件夾GitLab-Runner,將gitlab-runner-windows-386.exe放入重命名gitlab-runner.exe,然后命令行執(zhí)行安裝。

    cd C:\GitLab-Runner
    //無用戶安裝,由于此時運(yùn)行runner時使用的并不是當(dāng)前系統(tǒng)賬號,后面使用git的時候會有權(quán)限問題,不推薦
    ./gitlab-runner.exe install
    
    //以當(dāng)前管理員賬號安裝
    ./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

    gitlab的工程界面,進(jìn)入Settings => CI/CD => Runners settings 獲取相關(guān)參數(shù),如圖:
    runner-register-var.png

    運(yùn)行以下命令:

    gitlab-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 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
    

    注冊完成后可在gitlab頁面看到一個新的runner:


    runner-state.png

    我這邊是將機(jī)器的tag命名為my-tag,這個后面寫CI腳本時指定Runner需要用到。

    如果啟動時遇到FATAL: Failed to start gitlab-runner: The service did not start due to a logon failure.按以下步驟可解決:
    計算機(jī)管理 => 服務(wù) => gitlab-runner =>登錄 重新輸入密碼即可

三. CI/CD腳本編寫

新建腳本文件.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
  1. my-tag 替換成你自己的Runner的tag。
  2. only下面的配置表示只有在gitlab網(wǎng)頁操作和Pipeline triggers時才會觸發(fā)
only:
  - web
  - triggers
  1. 配置Pipeline triggers:
    gitlab的工程界面,進(jìn)入Settings => CI/CD => Pipeline triggers 配置相關(guān)參數(shù)
    Add trigger 然后會獲取一個token,頁面下方會有具體請求的URL,variables用來傳參數(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)行測試:

    Post請求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
    

    各個參數(shù)說明:

    branch:要打包apk的分支
    client_version:apk版本名
    client_version_code:apk版本號
    dingding_open:是否打開釘釘通知
    build_apk_res:是否連接正式服
    build_apk_dev:是否連接測試服
    cocos_version_code:cocos creator的版本
    build_apk:是否構(gòu)建apk
    hotupdate_packageUrl:熱更新地址
    hotupdate_packageVersion:熱更新版本號
    outPut_suffix:生成的apk命名的后綴添加
    is_skip_u8:是否跳過U8,登錄廣告等接口默認(rèn)成功
    gameConfig_client_version:游戲客戶端版本號
    outPut_dir:apk輸出路徑
    
    //以下可不傳,用默認(rèn)的配置
    game_name:游戲名字
    game_config_path:游戲配置文件的路徑
    hotupdate_config_path:熱更新文件的路徑
    
  2. 修改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的修改需要游戲自己具體實現(xiàn),cocos_example中已經(jīng)實現(xiàn)的修改配置

    {
        "version":124011015001,
        "is_dev":true,
        "is_skip_u8":true
    }
    

    hotupdate_config_path依賴cocos的構(gòu)建完自動替換熱更新文件的插件.

  3. 修改安卓工程

    修改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
                         }
                     }
                 }
             }
         }
    
  4. 開始正式打包

    在Runner的機(jī)子上,先手動構(gòu)建一次Cocos Creator工程,將打包相關(guān)配置都配置好,確認(rèn)Android studio能生成正常的Apk.
    用PostMan拋送Post請求,觸發(fā)Pipeline trigger,開始自動構(gòu)建
    notice_job 用來觸發(fā)打包完成通知
    由于打包完成后包在本地,所以需要在本機(jī)上建了一個web服務(wù),支持指定目錄的文件下載。參考Win10搭建web服務(wù)實現(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> 安卓_連測試服包:'+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, '打包完成,抓緊測試', msg, True)
    

    修改dingding.py文件:
    dingding_url1,dingding_url2替換成對應(yīng)游戲打包群的地址

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容