返回上一頁

Gitlab CI/CD for ASP.NET 筆記

Windows 安裝

前置作業: 準備一台 Windows 主機,安裝好 build tool,可以是 visual studio build tools 或是 visual studio 任何版本(需合法授權)

  1. 下載 gitlab runner 執行檔: 64 bit,32 bit
  2. 找個好地方放他,譬如 D:\GitLab_CI,他所在的目錄會是以後程式碼 pull 下來的目錄
  3. 開啟 CMD,切換目錄到 runner.exe 所在的位置
    1. runner.exe install
      1. 如有使用 private nuget 的情形,應至電腦管理 -> 服務,調整 runner 的服務執行帳號, runner 預設的執行帳號為 本機系統帳戶
      2. 調整完帳號後,應替該帳號設定好 nuget source (或想辦法幫本機系統帳戶掛好 nuget source)
      3. 將服務調整為 自動(延遲啟用)

        install 之後會註冊為服務,並預設為 自動 但一開始測試時會發生重開機後服務未啟用的情形,測試調成延遲啟用會避免這個狀況

      4. runner.exe stop; runner.exe start

runner.exe start

build server 上處理過 runner 的 install 以及 start 之後,已註冊的 repository 上就可以看到 runner 顯示綠燈,如果要在新的 repository 也用這台 build server 來處理 CI/CD,只要重複 register 程序就可以

Tips

 script:
    - chcp 65001 
  • 於 yml 中指定特定 powershell file 進行建置,建置失敗仍顯示 Job Success 在 powershell 中判斷 $LASTEXITCODE 或 $Error.Count,兩者其一不為零時表示建置已有錯誤發生
    • $LASTEXITCODE 表於 powershell 呼叫 cmd 時發生 exit code 不為 0 的錯誤
    • $Error.Count 不為 0 時表示 powershell 已發生錯誤,使用時應於 powershell file 第一行先進行 $Error.Clear() 以避免上一次的錯誤影響當次建置

向 Gitlab 註冊 runner

使用互動模式註冊

在 CMD 下runner.exe register 接著依照指示,輸入:

  1. gitlab 位置
  2. repository 的 registration token (可於 repository 的 Setting -> CI/CD -> Runner 尋獲)
  3. 輸入當前 runner 的描述,預設會帶入 %COMPUTERNAME%
  4. 當前 runner 的 tag name (optional)
  5. 提示註冊成功,之後會要你選擇 executor, msbuild 專案選 shell

使用手動註冊模式

  • 因自動模式預設會將 runner 鎖定在單一專案上,一台 windows build node 只能給一個專案跑太浪費了
  • 假設有其他進階需求,互動模式無法滿足時可使用下列指令:
  • 假設除了 lock 之外其餘均與互動模式參數相同,指令如下:
    runner.exe register 
        --non-interactive  
        --url={GitlabServerUrl} 
        --registration-token={RepositoryRegistrationToken}  
        --executor=shell  # windows default 為 powershell,可搭配 --shell 指令來指定 bash|cmd|powershell 
        --locked=false 
        --name={指定 runner 在 gitlab ci 頁面的顯示名稱, 建議輸入主機名稱}
    
  • 其他更多可參考 runner.exe register --help 的詳細說明
  • 上述說明的 locked=false 在設定完成後當前的 runner 會在整個 gitlab 上公開,其他 repository 要使用這個 runner 只要於 CI/CD 頁籤上的 Available specific runners 內點下你的 runner 後的 Enable for this project 就可以開始使用
    • 其他 repository 只能選擇啟用或停用,無法替你刪除
    • 如果是 gitlab 管理員要提供所有 repository 可以使用你提供的 runner 時,可用此方法,只要註冊一次, repository owner 選擇要不要啟用就可以
    • 要確認當前 runner 是否能讓全系統共用,也可於 CI/CD 的 runner 清單中確認剛剛註冊上去的 runner 名字後面是否有鎖頭
    • 不想拼這麼長的指令,也可以先用互動模式建立完 runner 之後,回去 gitlab 頁面上編輯 runner,把 Lock to current projects 的勾選給取消

完成註冊後,可以看到 config.toml 檔被建立:

  • Windows 下可以在 runner.exe 的目錄下看到新增一個 config.toml 檔案
  • Linux 可以看到 config.toml 檔的位置位於 /etc/gitlab-runner/config.toml

撰寫 .gitlab-ci.yml

  1. 於 source 的根目錄中增加一個 .gitlab-ci.yml 檔案
    1. 開始寫這個 yml 檔...
      • yml 檔撰寫參考
      • CI 工具有提供額外的環境變數,可以參考 Predefined environment variables,但要注意不要用到 deprecated variables
      • 可以在 job 中執行 gci env:* | sort-object name 來列出所有的環境變數 (會包含OS + USER + CI的)
  2. 可於 CD/CI -> Pipeline 中觀察建置結果
  3. 於 build node 觀察建置結果
    1. Windows 可於 D:\Gitlab_CI 中觀察 git fetch/clone 結果 (下面會多一個 build 目錄)
    2. Linux 安裝後會是使用 gitlab-runner 帳號執行,可確認帳號的 ~/build (/home/gitlab-runner/build) 目錄有沒有建立及內容是否有正確拉下來

stages

Gitlab CI 可以自訂 stage 來設定建置的順序,也可以不指定,不指定時預設有下列 stage 可以用

  • .pre
  • build
  • test
  • deploy
  • .post

然後每一個 job 要指定自己是在哪個 stage 跑的,像是:

build_src:
  stage: build
  script:
    - echo "done build src"

push_artifact:
    stage: deploy
    script:
     - echo "done push artifact"

clean_all:
    stage: .post
    script:
      - echo "Clean Up!"

如果執行順利,可以在 Pipeline 看到類似這樣的結果

如果有多個 job 指向同一個 stage 時,這些 job 會進行平行處理,不會互相等待,但會在當前 stage 都成功後,才繼續下一個 stage

build_src-a:
  stage: build
  script:
    - echo "完成 a 建置"

build_src-b:
  stage: build
  script:
    - echo "完成 b 建置"


push_artifact:
    stage: deploy
    script:
     - echo "done 發布"

clean_all:
    stage: .post
    script:
      - echo "Clean Up!"

REF: https://docs.gitlab.com/ee/ci/yaml/


MEMO

2022-09-27 發現 Gitlab CI Output log 的最後面顯示以下訊息:

Job's log exceeded limit of 4194304 bytes.
Job execution will continue but no more output will be collected.

調整 config.toml 的 output_limit 來增加上限 ( Default is 4096 (4MB).)

https://stackoverflow.com/questions/53541000/gitlab-ci-cd-jobs-log-exceeded-limit https://docs.gitlab.com/runner/configuration/advanced-configuration.html



註記一下動得了的 yml 檔先,目前能搭配之前在 jenkins autobuild 時寫的 AutoBuild.csproj 將專案完成建置並正確複製到指定路徑

# This file is a template, and might need editing before it works on your project.
# The following script will work for any project that can be built from command line by msbuild
# It uses powershell shell executor, so you need to add the following line to your config.toml file
# (located in gitlab-runner.exe directory):
#   shell = "powershell"
#
# The script is composed of 3 stages: build, test and deploy.
#
# The build stage restores NuGet packages and uses msbuild to build the exe and msi
# One major issue you'll find is that you can't build msi projects from command line
# if you use vdproj.  There are workarounds building msi via devenv, but they rarely work
# The best solution is migrating your vdproj projects to WiX, as it can be build directly
# by msbuild.
#
# The test stage runs nunit from command line against Test project inside your solution
# It also saves the resulting TestResult.xml file
#
# The deploy stage copies the exe and msi from build stage to a network drive
# You need to have the network drive mapped as Local System user for gitlab-runner service to see it
# The best way to persist the mapping is via a scheduled task (see: https://stackoverflow.com/a/7867064/1288473),
# running the following batch command: net use P: \\x.x.x.x\Projects /u:your_user your_pass /persistent:yes


# place project specific paths in variables to make the rest of the script more generic
variables:
  EXE_RELEASE_FOLDER: 'YourApp\bin\Release'
  MSI_RELEASE_FOLDER: 'Setup\bin\Release'
  TEST_FOLDER: 'Tests\bin\Release'
  DEPLOY_FOLDER: 'D:\Publish\Test'
  NUGET_PATH: 'C:\Windows\System32\nuget.exe'
  MSBUILD_PATH: 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSbuild.exe'
  NUNIT_PATH: 'C:\Program Files (x86)\NUnit.org\nunit-console\nunit3-console.exe'
  SLN_NAME: 'AutoBuild.sln'

stages:
  - build
#  - test
  - deploy

build_job:
  stage: build
  only:
    - tags  # the build process will only be started by git tag commits
    - release
#  tags:
#    - Release
  script:
    - '& "$env:NUGET_PATH" restore "./WebCode/$env:SLN_NAME"'  # restore Nuget dependencies
    - '& "$env:MSBUILD_PATH" "./AutoBuild.csproj" '  # build the project
    - 'Write-Host "Build Passed"'
#  artifacts:
#    expire_in: 1 week  # save gitlab server space, we copy the files we need to deploy folder later on
#    paths:
#      - '$env:EXE_RELEASE_FOLDER\YourApp.exe'  # saving exe to copy to deploy folder
#      - '$env:MSI_RELEASE_FOLDER\YourApp Setup.msi'  # saving msi to copy to deploy folder
#      - '$env:TEST_FOLDER\'  # saving entire Test project so NUnit can run tests

deploy_job:
 stage: deploy
 only:
   - tags
   - release
 when: manual
 script:
    #- '& "$env:NUGET_PATH" restore "./WebCode/MYWEB.sln"'  # restore Nuget dependencies
    #- '& "$env:MSBUILD_PATH" "./WebCode/AutoBuild.csproj" /p:publishCompId=PD'  # build the project
    - 'Write-Host "Depolyed Successfully"'
 dependencies:
   - build_job
#   - test_job