Convert_Ruleset_YAML_to_MRS #11884
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 工作流程名称 | |
name: Convert_Ruleset_YAML_to_MRS | |
# 触发条件 | |
on: | |
schedule: | |
- cron: '*/20 * * * *' # 每20分钟运行一次 | |
workflow_dispatch: # 允许手动触发 | |
# 定义作业 | |
jobs: | |
update-and-convert: | |
runs-on: windows-latest # 使用 Windows 运行环境 | |
steps: | |
# 步骤1:检出仓库代码 | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
# 步骤2:检查并下载 mihomo(仅在第一次运行时) | |
- name: Check and download mihomo v1.18.7 if needed | |
shell: powershell | |
run: | | |
$exePath = "./mihomo.exe" # 定义 mihomo.exe 的路径 | |
$downloadMarker = "./.mihomo_downloaded" # 定义下载标记文件的路径 | |
# 检查 mihomo.exe 和 .mihomo_downloaded 标记文件是否存在 | |
if (-not (Test-Path $exePath) -or -not (Test-Path $downloadMarker)) { | |
Write-Host "mihomo.exe or .mihomo_downloaded not found, downloading..." | |
$url = "https://github.com/MetaCubeX/mihomo/releases/download/v1.18.7/mihomo-windows-amd64-compatible-v1.18.7.zip" # 定义下载 URL | |
$zipFile = "mihomo.zip" # 定义下载的 ZIP 文件名 | |
$maxAttempts = 5 # 定义最大重试次数 | |
for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) { | |
try { | |
Write-Host "Downloading mihomo v1.18.7 (Attempt $attempt)..." | |
Invoke-WebRequest -Uri $url -OutFile $zipFile -TimeoutSec 300 # 下载 ZIP 文件 | |
# 解压和重命名 mihomo.exe | |
Expand-Archive -Path $zipFile -DestinationPath . -Force # 解压 ZIP 文件 | |
Remove-Item -Path $zipFile # 删除 ZIP 文件 | |
$exeFile = Get-ChildItem -Recurse -Filter "*mihomo*.exe" | Select-Object -First 1 # 查找解压后的 exe 文件 | |
if ($exeFile) { | |
Rename-Item -Path $exeFile.FullName -NewName $exePath -Force # 重命名 exe 文件 | |
New-Item -Path . -Name ".mihomo_downloaded" -ItemType "file" -Force # 创建下载标记文件 | |
Write-Host "Extraction and renaming complete!" | |
break | |
} else { | |
throw "Executable file not found in the extracted contents" # 抛出异常如果未找到 exe 文件 | |
} | |
} catch { | |
Write-Host "Download, extraction, or renaming failed. Error: $_" # 捕获异常并输出错误信息 | |
if ($attempt -eq $maxAttempts) { | |
Write-Host "Max attempts reached. Process failed." # 如果达到最大重试次数,退出流程 | |
exit 1 | |
} | |
Start-Sleep -Seconds 10 # 等待10秒后重试 | |
} | |
} | |
} else { | |
Write-Host "mihomo.exe and .mihomo_downloaded already exist." # 如果文件已存在,跳过下载 | |
} | |
# 步骤3:转换规则集(依赖于 mihomo.exe) | |
- name: Convert ruleset | |
shell: powershell | |
run: | | |
$exePath = "./mihomo.exe" # 定义 mihomo.exe 的路径 | |
# 检查 mihomo.exe 是否存在 | |
if (Test-Path $exePath) { | |
Write-Host "Mihomo version:" | |
& $exePath -v # 输出 mihomo 版本 | |
Write-Host "Mihomo help:" | |
& $exePath convert-ruleset --help # 输出 mihomo 帮助信息 | |
$maxAttempts = 5 # 定义最大重试次数 | |
for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) { | |
try { | |
Write-Host "Attempting conversion (Attempt $attempt)..." | |
& $exePath convert-ruleset domain yaml adblock_reject.yaml adblock_reject.mrs # 尝试转换规则集 | |
if ($LASTEXITCODE -eq 0) { | |
Write-Host "Conversion successful!" # 如果成功,输出成功信息 | |
break | |
} else { | |
throw "Conversion failed with exit code $LASTEXITCODE" # 如果失败,抛出异常 | |
} | |
} catch { | |
Write-Host "Attempt $attempt failed. Error: $_" # 捕获异常并输出错误信息 | |
if ($attempt -eq $maxAttempts) { | |
Write-Host "Max attempts reached. Exiting." # 如果达到最大重试次数,退出流程 | |
exit 1 | |
} | |
Start-Sleep -Seconds 5 # 等待5秒后重试 | |
} | |
} | |
} else { | |
Write-Host "mihomo.exe not found, skipping conversion step." # 如果未找到 mihomo.exe,跳过转换步骤 | |
exit 1 | |
} | |
# 步骤4:提交并推送更改(包括 mihomo.exe 和标记文件) | |
- name: Push changes | |
shell: powershell | |
env: | |
TOKEN: ${{ secrets.TOKEN }} # 从 GitHub Secrets 中获取 TOKEN | |
run: | | |
$maxAttempts = 5 # 定义最大重试次数 | |
for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) { | |
try { | |
git config user.email "[email protected]" # 配置 Git 用户邮箱 | |
git config user.name "GitHub Action" # 配置 Git 用户名 | |
git pull https://${{ secrets.TOKEN }}@github.com/${{ github.repository }}.git HEAD:${{ github.ref }} --rebase # 尝试拉取最新更改 | |
break | |
} catch { | |
Write-Host "git pull attempt $attempt failed. Error: $_" | |
if ($attempt -eq $maxAttempts) { | |
Write-Host "Failed to pull changes after $maxAttempts attempts. Exiting." | |
exit 1 | |
} | |
Start-Sleep -Seconds 30 # 等待30秒后重试 | |
} | |
} | |
git add adblock_reject.mrs mihomo.exe .mihomo_downloaded # 添加转换后的文件和 mihomo.exe 以及标记文件到 Git | |
if (git status --porcelain) { | |
git commit -m "Update adblock_reject.mrs and add mihomo.exe" # 如果有更改,进行提交 | |
for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) { | |
try { | |
Write-Host "Attempting to push changes (Attempt $attempt of $maxAttempts)..." | |
git push https://${{ secrets.TOKEN }}@github.com/${{ github.repository }}.git HEAD:${{ github.ref }} --force # 强制推送更改 | |
Write-Host "Changes successfully pushed." # 如果成功,输出成功信息 | |
break | |
} catch { | |
Write-Host "Push attempt $attempt failed. Error: $_" # 捕获异常并输出错误信息 | |
if ($attempt -eq $maxAttempts) { | |
Write-Host "Failed to push changes after $maxAttempts attempts. Exiting." # 如果达到最大重试次数,退出流程 | |
exit 1 | |
} | |
Start-Sleep -Seconds 30 # 等待30秒后重试 | |
} | |
} | |
} else { | |
Write-Host "No changes to commit." # 如果没有更改,输出无更改信息 | |
} |