這份文件是 Lab 手冊,會帶你完成:
- 啟動 Fastify 應用
- 實際 push 到 GitHub 觀察 CI
- 在本機用
act模擬 CI - 理解不同 branch 推送時的效果
- 完成課堂練習
建議使用 GitHub Codespaces 開啟本 repo(環境已預先準備 Node / Docker / act)。
- 到原始 repo 頁面,點右上角 Fork
- 進入你自己的 fork repo
- 用你的 fork repo 開啟 Codespaces
先確認工具版本:
node --version
docker --version
docker compose version
act --version安裝依賴:
npm ci啟動服務:
npm run build
npm run start驗證服務:
curl http://localhost:3000/
curl http://localhost:3000/healthPush 到 GitHub 觀察 CI
在你的 fork repo:
- 點選上方 Actions 分頁
- 如果看到啟用提示,點 I understand my workflows, go ahead and enable them(或同義按鈕)
- 回到 Code 頁面繼續操作
把 snippets/01_hello.yaml 複製到 .github/workflows/ 底下
git checkout -b feature/ci-observe
cp snippets/01_hello.yaml .github/workflows/
git add .
git commit -m "ci: add hello.yaml"
git push origin feature/ci-observe- 到你的 fork repo 的 Actions 頁面
- 找到最新
ciworkflow run - 查看 01_hello.yaml 的執行步驟與結果
把 snippets/02_run-test.yaml 複製到 .github/workflows/ 底下
cp snippets/snippets/02_run-test.yaml .github/workflows/
git add .
git commit -m "ci: add run-test.yaml"
git push origin feature/ci-observe- 觀察 run-test.yaml 內容
- 到 GitHub Actions 頁面, 查看 run test 執行結果
- 觀察 artifact
act 是一個可以在本機執行 GitHub Actions workflow 的工具
你可以用它:
- 在不 push 到 GitHub 的情況下先驗證 workflow
- 快速重跑失敗步驟、縮短除錯時間
- 模擬
push/pull_request等事件
官方資源:
- 官網:https://nektosact.com/
- GitHub 專案:https://github.com/nektos/act
切到要模擬的 branch,執行 act push
act push若只想跑單一 workflow,可加 -W:
act push -W .github/workflows/ci.yaml進階補充:若你需要在「不切 branch」情況下指定事件分支,可以使用:
act push --env GITHUB_REF=refs/heads/<branch>實際應用中, 我們可能會將每一個版本都跑過 ci, 並 build 出 image 但 image tag 要可以區分出是 feature branch 或是 release branch 方便我們理解與追蹤特定版本的 source code
觀察 snippets/ci.yaml 與 snippets/cd.yaml
將 snippets/ci.yaml 與 snippets/cd.yaml 複製到 .github/workflows/ 底下
cp snippets/ci.yaml .github/workflows/
cp snippets/cd.yaml .github/workflows/git checkout -b feature/a
act push觀察 build 出來的 image
docker images切出 release branch
git checkout -b release/1.0.0
act push觀察 build 出來的 image
docker images- 如何設計 CI Pipeline 以確保程式碼品質
- 如何設計 CD Pipeline 部署到目標環境
- CI Pipeline 與 CD Pipeline 的相依關係
- 通知或報表機制