iT邦幫忙

2025 iThome 鐵人賽

DAY 27
1
Modern Web

30天一起搞懂Web觀念系列 第 27

[DAY27] CI/CD 是什麼?怎麼用GitHub Actions做?

  • 分享至 

  • xImage
  •  

CI/CD 是什麼?

  • CI(Continuous Integration,持續整合)

    每次Commit和Push的時候,都可以自動化的執行的測試、建置,透過這樣的方式快速找出問題

  • CD(Continuous Delivery / Deployment,持續交付 / 部署)

    程式碼通過自動化測試後,可以快速且可靠地部署到測試環境或正式環境

    • 持續交付(Continuous Delivery):程式碼自動打包,可隨時部署,通常需手動觸發部署
    • 持續部署(Continuous Deployment):程式碼自動部署到正式環境,完全無需人為介入

CI/CD Pipeline

  1. Commit:開發者將程式碼推送到 GitHub

== CI Pipeline ==

  1. Build:編譯、打包前端或後端程式碼
  2. Test:執行自動化測試,包括單元測試(Unit Test)、整合測試(Integration Tests)、端對端測試(End-to-End Test)

== CD Pipeline ==

  1. Deploy:測試通過後自動部署到預備環境或正式環境
  2. Monitor:監控系統狀態與使用者反饋

Github Actions是什麼?

是 GitHub 提供的一個自動化服務,就是把 CI/CD 流程寫成程式碼直接在 GitHub 裡運行,當上傳code後,就可以自動化完成:

  • 安裝依賴、建置程式
  • 執行測試
  • 部署到伺服器或 GitHub Pages

用Github Actions做超簡易的CI/CD

(範例使用ChatGPT生成)

這邊用一個很簡單的前端網頁

  1. 首先需要一個在 GitHub 上建立一個 repo

    專案裡放一個index.html ,內容隨便寫了一個Hello World

  2. 然後在 repo 裡新增一個資料夾 .github/workflows/,並新增檔案 deploy.yml

    • 建立了這個資料夾就建立好 GitHub Actions
    • 每個 .yml 檔案是一個 Action
    name: Deploy static site to GitHub Pages
    
    on:
      push:
        branches:
          - main   # 當 main 分支有更新時執行
    
    permissions:
      contents: write   # 允許 Github Actions push 到 gh-pages 分支
    
    jobs:
      deploy:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout code
            uses: actions/checkout@v3
    
          - name: Deploy to GitHub Pages
            uses: peaceiris/actions-gh-pages@v3
            with:
              github_token: ${{ secrets.GITHUB_TOKEN }}
              publish_dir: ./
    
    

    流程:

    1. 每次 push 到 main 分支,會自動執行
    2. 把 repo 裡的檔案發佈到 gh-pages 分支

    https://ithelp.ithome.com.tw/upload/images/20250830/20177952cCGXVtIYh5.png

  3. 開啟 GitHub Pages

    到 repo 的 Settings → Pages,選擇 Branch = gh-pages,就能看到網頁了


參考資料

https://medium.com/技術筆記/何謂-ci-cd-利用-github-actions-做一個簡單的-ci-cd-2d55e6dabeed

https://kucw.io/blog/github-actions-intro/


上一篇
[DAY26] Git flow是什麼?
下一篇
[DAY28] SPA、SSR是什麼?
系列文
30天一起搞懂Web觀念30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言