將 Modernize CLI 整合進您的 CI/CD 管線,以自動化大規模的應用程式現代化。 本文將教你如何配置 GitHub Actions 和 Azure Pipelines,以排程或按需執行 Modernize CLI。
在 CI/CD 管線中執行 Modernize CLI 可讓你:
- 自動化升級 ,並以定期時間表進行,無需人工介入。
- 標準化組織內的現代化工作流程。
- 透過專用分支追蹤變更並建立工件。
- 透過 pull request、建置摘要和日誌來檢視結果。
本文中的範例管線執行以下步驟:
- 下載並安裝最新的 Modernize CLI。
- 使用可配置的目標執行
modernize upgrade(例如,Java 21)。 - 提交任何產生的變更,並推送到專用分支。
- 發布結果摘要,並將 CLI 日誌上傳為建置工件。
先決條件
- GitHub Copilot 訂閱方案:免費、Pro、Pro+、商業或企業方案。 請參閱副駕駛計畫。
-
GitHub 個人存取權杖(PAT):建立一個權杖並儲存為名為
GH_TOKEN的倉庫秘密。 請參閱 管理您的個人存取權杖。
設定管線
在您的儲存庫中建立一個工作流程檔案 .github/workflows/modernize.yml ,內容如下:
name: Modernization CLI
on:
workflow_dispatch:
inputs:
upgrade_target:
description: 'Upgrade target (e.g., Java 21)'
required: false
default: 'latest'
schedule:
# Run during off-peak hours: 2 AM UTC daily
- cron: '0 2 * * *'
permissions:
id-token: write
contents: write
actions: read
jobs:
modernization:
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download Modernize CLI
run: |
curl -fsSL https://raw.githubusercontent.com/microsoft/modernize-cli/main/scripts/install.sh | sh
- name: Run Modernize CLI to upgrade code
run: |
TARGET="${{ github.event.inputs.upgrade_target }}"
if [ -z "$TARGET" ] || [ "$TARGET" = "latest" ]; then
modernize upgrade --no-tty
else
modernize upgrade "$TARGET" --no-tty
fi
- name: Push changes to result branch
id: push_changes
run: |
BRANCH_NAME="modernize-upgrade-${{ github.event.inputs.upgrade_target || 'latest' }}-$(date +%Y%m%d-%H%M%S)"
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add -A
git reset .github/workflows
git diff --cached --quiet || git commit -m "chore: apply Modernize CLI changes [skip ci]"
git checkout -B "$BRANCH_NAME"
git push origin "$BRANCH_NAME"
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_OUTPUT
- name: Display results summary
if: success()
run: |
cat >> $GITHUB_STEP_SUMMARY <<EOF
## Modernization Complete
### Branch Information
- **Result Branch**: \`${{ steps.push_changes.outputs.BRANCH_NAME }}\`
- **Target**: ${{ github.event.inputs.upgrade_target || 'latest' }}
### Links
- [View Branch](https://github.com/${{ github.repository }}/tree/${{ steps.push_changes.outputs.BRANCH_NAME }})
- [Create PR](https://github.com/${{ github.repository }}/compare/main...${{ steps.push_changes.outputs.BRANCH_NAME }})
EOF
- name: Upload Modernize CLI logs
if: always()
uses: actions/upload-artifact@v4
with:
name: modernize-logs
path: ~/.modernize/logs/
if-no-files-found: warn
工作流程詳細資料
工作流程包含兩個觸發器:
-
手動派遣 (
workflow_dispatch):按需從 動作 分頁執行工作流程。可選擇性地指定升級目標,例如Java 21。 -
排程
schedule():每日凌晨2點(UTC)自動運行。 調整 cron 表達式以符合你偏好的時間表。
每次運行執行以下步驟:
- 檢查倉庫程式碼。
- 下載並安裝最新的 Modernize CLI。
- 以指定目標執行
modernize upgrade或預設為latest。 - 提交任何變更並推送到有時間戳的分支。
- 撰寫摘要,並附有分支和 PR 連結到 GitHub Actions 步驟摘要。
- 上傳 現代化 CLI 日誌作為建置產物,用於故障排除。
備註
工作流程會在提交前重置 .github/workflows ,以避免不小心修改工作流程檔案本身。
執行流程
手動觸發工作流程:
- 去你在 GitHub 上的倉庫看看。
- 選取動作索引標籤。
- 從工作流程清單中選擇 現代化 CLI 。
- 選取 [執行工作流程]。
- 可選擇輸入升級目標,然後選擇 執行工作流程 以確認。
工作流程結束後,檢視步驟摘要中是否有指向結果分支的連結,並建立拉取請求來合併變更。
Troubleshooting
常見問題
認證錯誤:
- 請用有效的 GitHub 個人存取權杖驗證秘密
GH_TOKEN或變數是否正確設定。 - 確保該令牌具備 GitHub Copilot 存取所需的範圍。
未偵測到任何變化:
- Modernize CLI 可能會判定指定目標不需要更改。
- 請檢視上傳的日誌檔案來了解評估細節。
Push failures (Azure Pipelines):
- 確認建置服務身份在倉庫上有 建立分支、 貢獻和 讀取 權限。
- 詳細的設定說明請參考在腳本中執行 Git 指令。
將 CLI 下載錯誤現代化:
- 確認跑者具有到https://github.com的網路連線。
- 檢查是否有代理或防火牆限制,可能阻擋下載。