顯示具有 GitHub 標籤的文章。 顯示所有文章
顯示具有 GitHub 標籤的文章。 顯示所有文章

2025/04/30

用一行指令搞定 Git 歷史中的大檔案:git lfs migrate import

用一行指令搞定 Git 歷史中的大檔案:git lfs migrate import

如果你曾經在推送 Git 專案到 GitHub 時遇到以下錯誤訊息:

remote: error: File opencv2.framework/Versions/A/opencv2 is 236.31 MB; this exceeds GitHub's file size limit remote: error: GH001: Large files detected. You may want to try Git Large File Storage

那麼你就遇到 GitHub 強制限制:單一檔案不可超過 100MB。而這個限制,不只是針對目前版本的檔案,就算該檔案只曾出現在 Git 歷史中一次,也會導致整個 Push 被拒絕。

常見的錯誤誤解

2025/03/13

如何使用 cURL 刪除與查詢 GitHub Packages 的 NuGet 套件版本

在 GitHub Packages 上,你可以透過 API 來查詢或刪除已發布的 NuGet 套件版本。本篇文章將介紹如何使用 curl 來執行這些操作。

🔍 查詢 NuGet 套件的所有版本

如果你想要取得某個 GitHub Organization 內的 NuGet 套件的所有版本,可以使用以下 curl 指令:

curl -H "Authorization: Bearer ghp_xxxxxx" \
     -H "Accept: application/vnd.github+json" \
     "https://api.github.com/orgs/YourOrgName/packages/nuget/ProjectName/versions"

這段指令會:

  • 使用 -H "Authorization: Bearer ghp_xxxxxx" 提供你的 GitHub Personal Access Token(PAT)來進行身份驗證。
  • 加上 -H "Accept: application/vnd.github+json" 以確保回傳的 JSON 資料格式符合最新 API 版本。
  • 向 GitHub API 發送請求,列出 Organization YourOrgName 下,NuGet 套件 ProjectName 的所有版本。

2025/03/10

透過 Git Subtree 無痛整合 GitHub 上的既有專案

Git Subtree 輕鬆合併專案,保留完整 commit 歷史

在軟體開發的過程中,時常會遇到這樣的需求:

  • 你有一個現有的 Git 專案,但需要將另一個儲存庫的內容整合進來,並且希望保留完整的 commit 歷史。

這時,Git 提供了幾種不同的方式來達成這個目標,其中 Git Subtree 是一個簡單又強大的解法。

本文將帶你了解 Git Subtree 的用途、與 Submodule 的差異,以及如何實際操作,讓你的開發流程更順暢!

2025/03/02

使用 GitHub Actions 在 NX Monorepo 中實現單元測試與 Cypress E2E 測試


  # 單元測試及E2E測試
  test:
    name: 單元測試及E2E測試
    runs-on: [ self-hosted, Ubuntu, 24.04 ]
    steps:
      # 單元測試
      - name: Unit Test
        run: pnpm nx test project
      # E2E 測試
      - name: E2E Test
        run: |
          # 安裝依賴套件
          sudo apt install -y libgtk2.0-0t64 libgtk-3-0t64 libgbm-dev libnotify-dev libnss3 libxss1 libasound2t64 libxtst6 xauth xvfb
          # 安裝 Cypress
          pnpm cypress install
          # 驗證 Cypress
          pnpm cypress verify
          # 執行 E2E 測試
          pnpm nx e2e project --headless

2025/03/01

透過 Github Actions 自動清除7天前Actions紀錄


name: Cleanup Workflow Runs

on:
  # 每週日 UTC 15:59 執行 (台灣時間 23:59)
  schedule:
    - cron: "59 15 * * 0"
  # 允許手動執行
  workflow_dispatch:

jobs:
  cleanup:
    # 改為使用自建代理
    runs-on: [self-hosted, Ubuntu, 24.04]
    permissions:
      # 允許刪除 workflow runs
      actions: write
    steps:
      - name: Install GitHub CLI
        run: |
          sudo apt update
          sudo apt install -y gh jq

      - name: Authenticate GitHub CLI
        run: echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token

      - name: Get workflow runs from last week
        run: |
          # 設定刪除 7 天前的 workflow runs
          SEVEN_DAYS_AGO=$(TZ=UTC date -d "7 days ago" +%s)

          # 取得目前 repository 內所有 workflows
          gh api repos/${{ github.repository }}/actions/workflows | jq -r '.workflows[].id' > workflows.txt

          # 清空刪除列表
          > runs_to_delete.txt

          # 迴圈檢查每個 workflow,刪除超過 7 天的 workflow runs
          while read WORKFLOW_ID; do
            gh api --paginate repos/${{ github.repository }}/actions/workflows/$WORKFLOW_ID/runs | \
            jq -r --argjson SEVEN_DAYS_AGO "$SEVEN_DAYS_AGO" '
              .workflow_runs[] | select((.created_at | fromdateiso8601) < $SEVEN_DAYS_AGO) | .id' >> runs_to_delete.txt
          done < workflows.txt

      - name: Display need delete workflow runs
        run: cat runs_to_delete.txt

      - name: Delete old workflow runs
        run: |
          if [ -s runs_to_delete.txt ]; then
            while read RUN_ID; do
              echo "Deleting workflow run ID: $RUN_ID"
              gh api --method DELETE repos/${{ github.repository }}/actions/runs/$RUN_ID
            done < runs_to_delete.txt
          else
            echo "No workflow runs to delete."
          fi

2025/02/26

打造自建 GitHub Actions 2.322 環境:在 Windows 11 設置 Self-Hosted Runner 並建置.Net Core專案

Windows 相較於 macOS 和 Linux 設置較為繁瑣
在 Windows 上設定 GitHub Actions Runner 需要額外的權限與步驟。請先 以管理員身份 開啟 PowerShell,然後執行以下指令:
下載並安裝 GitHub Actions Runner
# 回到 C:\
cd ..\..\

# 創建一個文件夾
mkdir actions-runner; cd actions-runner

# 下載最新的runner包
Invoke-WebRequest -Uri https://github.com/actions/runner/releases/download/v2.322.0/actions-runner-win-x64-2.322.0.zip -OutFile actions-runner-win-x64-2.322.0.zip

# 可選:驗證哈希值
if((Get-FileHash -Path actions-runner-win-x64-2.322.0.zip -Algorithm SHA256).Hash.ToUpper() -ne 'ace5de018c88492ca80a2323af53ff3f43d2c82741853efb302928f250516015'.ToUpper()){ throw 'Computed checksum did not match' }

# 解壓安裝程序
Add-Type -AssemblyName System.IO.Compression.FileSystem ; [System.IO.Compression.ZipFile]::ExtractToDirectory("$PWD/actions-runner-win-x64-2.322.0.zip", "$PWD")

# 配置 GitHub Actions runner
# 接著填入需要配置的設定即可
./config.cmd --url https://github.com/YourOrganization --token yourToken

打造自建 GitHub Actions 2.322 環境:在 Ubuntu 24.04.2 LTS 設置 Self-Hosted Runner


# 建立一個使用者 github
sudo useradd --system --create-home --shell /bin/bash github

# 創建一個文件夾
mkdir actions-runner && cd actions-runner

# 下載最新的runner包
curl -o actions-runner-linux-x64-2.322.0.tar.gz -L https://github.com/actions/runner/releases/download/v2.322.0/actions-runner-linux-x64-2.322.0.tar.gz

# 可選:驗證哈希值
echo "b13b784808359f31bc79b08a191f5f83757852957dd8fe3dbfcc38202ccf5768  actions-runner-linux-x64-2.322.0.tar.gz" | shasum -a 256 -c

# 解壓安裝程序
tar xzf ./actions-runner-linux-x64-2.322.0.tar.gz && rm -fr actions-runner-linux-x64-2.322.0.tar.gz

# 配置 GitHub Actions runner
# 接著填入需要配置的設定即可
./config.sh --url https://github.com/YourOrganization --token yourToken

# 開機透過github用戶啟動GitHub Actions runner service
sudo -u github ./svc.sh install

# 立即啟動GitHub Actions runner
sudo -u github ./svc.sh start