### 如何實現自動化的程式碼整合與部屬
# CI/CD
# 持續整合/持續部屬
---

---
## 為何需要 CI/CD ?
將所有日常瑣碎的工作都交給開發工具處理,讓我們能夠將心思放在真正該做的事情上面。
<!-- 以下參考這兩篇文章的定義: -->
[山姆鍋對持續整合、持續部署、持續交付的定義](https://samkuo.me/post/2013/10/continuous-integration-deployment-delivery/)
[什麼是 CI / CD ?](https://bear-1111.medium.com/%E4%BB%80%E9%BA%BC%E6%98%AF-ci-cd-72bd5ae571f1)
---
### 持續整合 (continuous integration)
目的在儘快讓新功能的程式碼整合到現存的基礎程式庫 (codebase) 中來進行測試。
> 1. 降低風險 --- 盡快找到並解決有衝突的程式碼 (conflict codes)
> 2. 減少人工手動的繁複程序
> 3. 可隨時產生一版可部署的版本
藉由自動化測試可加速能夠部屬的程式碼的速度。
> 1. 單元測試 (unit tests)
> 2. 煙霧測試 (smoke tests)
---
### 持續部署 (continous deployment)
目的就是要讓軟體可以快速的自動部署到不同的環境,根據不同的需求而定,常見的有:
> 1. 整合測試環境 (Integration Tests Environment)
> 2. 測試環境 (QA Environment)
> 3. 用戶驗收測試環境 (UAT Environment)
> 4. 上架環境 (Pre-production/Staging Environment)
> 5. 生產環境 (Production Environment)
---
### 持續交付 (continous delivery)
「持續交付」是將新的特性儘快交付到最終使用者 (end-user) 的手中,為了達到這個目的,必須仰賴「持續整合」與「持續部署」的機制。換言之,持續交付關注在減少新的特性到使用者手上所必須花費的時間。
---
## 你需要使用的工具
* Git — 版本管理
* GitHub — 程式碼託管、審查
* CircleCI — 自動化建置、測試、部署
* Docker — 可攜式、輕量級的執行環境(使用 Docker,統一開發者、測試人員、以及產品的執行環境。)
* AWS Elastic Beanstalk — 雲端平台
* Slack — 團隊溝通、日誌、通知
[CircleCI与Jenkins:选择正确的CI/CD工具](https://www.kubernetes.org.cn/8268.html)
---
## 實作 CI

---
##### [DevOps:持續整合&持續交付(Docker、CircleCI、AWS)](https://blog.amowu.com/devops-continuous-integration-delivery-docker-circleci-aws-beanstalk/)
詳情請參考上方文件,以 [這個Git範例檔](https://github.com/BlaChalk/ci-workflow-demo) 做為示範,裡面已包含啟動 node.js 的基本架構與一個使用 Jest 的單元測試檔。
---
將範例檔載下來:
```
git clone https://github.com/BlaChalk/ci-workflow-demo.git
```
---
啟動專案:
```
yarn start
```
打開瀏覽器 http://127.0.0.1:3000 確認專案正常啟用。

---
使用 jest 進行單元測試:
```
yarn test
```
確認專案可正常執行測試。

---
### [CircleCI](https://circleci.com/)
直接進入官網即可連結 Github 帳號

---
選擇想要加入的 GitHub repository,下方使用 Fast 選擇自訂的 .yml 檔

---
選擇 Node(Advanced)

---
最後的 .yml 檔大概會長這樣:
```yaml=
# Use the latest 2.1 version of CircleCI pipeline process engine.
# See: https://circleci.com/docs/2.0/configuration-reference
version: 2.1
orbs:
# The Node.js orb contains a set of prepackaged CircleCI configuration you can utilize
# Orbs reduce the amount of configuration required for common tasks.
# See the orb documentation here: https://circleci.com/developer/orbs/orb/circleci/node
node: circleci/node@4.7
jobs:
# Below is the definition of your job to build and test your app, you can rename and customize it as you want.
build-and-test:
# These next lines define a Docker executor: https://circleci.com/docs/2.0/executor-types/
# You can specify an image from Dockerhub or use one of our Convenience Images from CircleCI's Developer Hub.
# A list of available CircleCI Docker Convenience Images are available here: https://circleci.com/developer/images/image/cimg/node
docker:
- image: cimg/node:16.10
# Then run your tests!
# CircleCI will report the results back to your VCS provider.
steps:
# Checkout the code as the first step.
- checkout
# Next, the node orb's install-packages step will install the dependencies from a package.json.
# The orb install-packages step will also automatically cache them for faster future runs.
- node/install-packages:
# If you are using yarn, change the line below from "npm" to "yarn"
pkg-manager: yarn
- run:
name: Run tests
command: yarn test
workflows:
# Below is the definition of your workflow.
# Inside the workflow, you provide the jobs you want to run, e.g this workflow runs the build-and-test job above.
# CircleCI will run this workflow on every commit.
# For more details on extending your workflow, see the configuration docs: https://circleci.com/docs/2.0/configuration-reference/#workflows
sample:
jobs:
- build-and-test
# For running simple node tests, you could optionally use the node/test job from the orb to replicate and replace the job above in fewer lines.
# - node/test
```
---
回到 github 點選 Compare & pull request 按鈕

---
點選 Create pull request 將預設好的訊息一並發送出去

---
點選 Merge pull request

---
點選 Confirm merge

---
回到 master 即可發現 circleci-project-setup 已成功合併進來

[點此觀看最後成果](https://github.com/BlaChalk/ci-workflow-sample)
---
[Docker](https://www.docker.com/)

Docker 是一種軟體平台,可讓您快速地建立、測試和部署應用程式
細心的觀眾可能有留意到,之前的 .yml 檔其實就已經有使用到 docker 進行部屬
```yaml=
docker:
- image: cimg/node:16.10
```
---

---
以上是其中一種的 CI 流程示意,CD 的部分有興趣的可以繼續參考下方文章接續挑戰
[DevOps:持續整合&持續交付(Docker、CircleCI、AWS)](https://blog.amowu.com/devops-continuous-integration-delivery-docker-circleci-aws-beanstalk/)
---
最後回顧

---
## 參考資料
[CI/CD (持續性整合 / 部署) - 因為懶,所以更要 CI/CD!概念講解!](https://blog.kennycoder.io/2020/04/07/CI-CD-%E6%8C%81%E7%BA%8C%E6%80%A7%E6%95%B4%E5%90%88-%E9%83%A8%E7%BD%B2-%E5%9B%A0%E7%82%BA%E6%87%B6%EF%BC%8C%E6%89%80%E4%BB%A5%E6%9B%B4%E8%A6%81CI-CD%EF%BC%81%E6%A6%82%E5%BF%B5%E8%AC%9B%E8%A7%A3%EF%BC%81/)
[山姆鍋對持續整合、持續部署、持續交付的定義](https://samkuo.me/post/2013/10/continuous-integration-deployment-delivery/)
[什麼是 CI / CD ?](https://bear-1111.medium.com/%E4%BB%80%E9%BA%BC%E6%98%AF-ci-cd-72bd5ae571f1)
[DevOps:持續整合&持續交付(Docker、CircleCI、AWS)](https://blog.amowu.com/devops-continuous-integration-delivery-docker-circleci-aws-beanstalk/)
[架構師觀點: 你需要什麼樣的 CI / CD ?](https://columns.chicken-house.net/2017/08/05/what-cicd-do-you-need/)
[CircleCI與摩卡的敏捷對談](https://medium.com/@victortsai_74477/circleci%E8%88%87%E6%91%A9%E5%8D%A1%E7%9A%84%E6%95%8F%E6%8D%B7%E5%B0%8D%E8%AB%87-eea02b86f0bc)
[CircleCI与Jenkins:选择正确的CI/CD工具](https://www.kubernetes.org.cn/8268.html)
{"metaMigratedAt":"2023-06-16T23:21:01.858Z","metaMigratedFrom":"Content","title":"CI/CD","breaks":true,"description":"CI/CD流程圖","contributors":"[{\"id\":\"1d46f1b2-8c82-4de6-956b-5ada888a4850\",\"add\":8443,\"del\":1762}]"}