持續整合介紹

  • 定義
Continuous Integration (CI) is a software development practice wherein developers regularly merge their code with a central repository after which automated builds and tests are run.

programmer push 任何 code change, CI 要能告訴你最基本的 build 是否會失敗? 若成功的話再告訴你單元測試是否通過? 最終能把 build 好的東西統一管理, 讓往後的人或是接下來的 CD 可以直接接著 CI 的成果繼續,而不用再重頭來一次。

CI/CD Layer cake
CI/CD 範疇

CI 基本原則

  • MAINTAIN A SINGLE SOURCE CODE REPOSITORY
  • BUILD AUTOMATION
  • MAKE THE BUILD SELF TESTING
  • MAKE FREQUENT COMMITS TO THE MAIN BRANCH
  • EVERY COMMIT SHOULD TRIGGER A BUILD ON THE CI SERVER
  • BROKEN BUILDS SHOULD BE FIXED IMMEDIATELY
  • THE CI BUILD SHOULD BE DONE IN A REPLICA OF THE PRODUCTION ENVIRONMENT
  • THE OUTPUT OF CI BUILDS SHOULD BE VISIBLE

使用CI好處

  • Since build output is extremely visible, when a build fails, we can find and resolve bugs quickly.
  • Continuous Integration helps to enforce testing in our applications, since we rely on these tests to assess the success of our builds.
  • Because the build is run in a production like environment, CI reduces the time it takes to validate the quality of software to be deployed.
  • Since atomic commits are encouraged, CI allows us to reduce integration problems with existing code, enabling us to deliver better software faster.

單元測試 Unit test

  • 團隊的 team leader 或是 architect 一定要清楚知道 TDD
  • 新的專案開始用 TDD,舊的專案別急著補測試,先把流程建立好,再逐步累積測試的內容
  • Unit test 的好處
    • 當作 library 的使用範例
    • 碰到回報的 bugs, 可以寫 unit test 重現 (reproduce) 問題
    • 拿來當作 API 規格, 用 unit test 來驗證 API 是否符合規範
  • 可使用 GitLab (pipeline 頁面可察看測試結果)

相關工具介紹

Github 持續CI/CD 部屬