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

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

MSB4276: The default SDK resolver failed to resolve SDK "Microsoft.NET.Sdk" because directory did not exist

因環境變數缺少MSBuildSDKsPath
MSBuildSDKsPath=C:\Program Files\dotnet\sdk\8.0.406\Sdks

參考資料:

打造自建 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