Skip to content

Release Windows

Release Windows #10

name: Release Windows
on:
schedule:
- cron: '0 0 * * *' # 每天 UTC 时间 00:00 触发
workflow_dispatch: # 允许手动触发
jobs:
localization:
runs-on: windows-latest # 使用 Windows 作为运行环境
timeout-minutes: 30 # 设置超时时间为 30 分钟
env:
APP_FILE: app-windows.asar # 定义 app.asar 文件名
APP_CRACK_FILE: app-windows-crack.asar # 定义 app-crack.asar 文件名
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: Install required Python packages
shell: pwsh
run: |
$ErrorActionPreference = 'Stop' # 启用错误检查
python -m pip install --upgrade pip
- name: Install Chocolatey
shell: pwsh
run: |
$ErrorActionPreference = 'Stop' # 启用错误检查
if (-not (Get-Command choco -ErrorAction SilentlyContinue)) {
Write-Host "Installing Chocolatey with elevated permissions..."
$installScript = {
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
}
Start-Process pwsh -ArgumentList "-NoProfile -Command & { $installScript }" -Verb RunAs -Wait
} else {
Write-Host "Chocolatey is already installed."
}
- name: Get latest Termius version from Chocolatey
id: get_choco_version
shell: pwsh
run: |
$ErrorActionPreference = 'Stop' # 启用错误检查
$chocoVersion = choco search termius --exact | Select-String -Pattern 'termius\s+(\d+\.\d+\.\d+)'
if ($chocoVersion.Matches.Success) {
$chocoVersion = $chocoVersion.Matches.Groups[1].Value
Write-Host "Latest Termius version from Chocolatey: $chocoVersion"
echo "CHOCO_VERSION=$chocoVersion" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
} else {
Write-Host "Failed to get Termius version from Chocolatey"
exit 1
}
- name: Get latest Termius version from GitHub Releases
id: get_github_version
shell: pwsh
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # 使用 GitHub Token
run: |
$ErrorActionPreference = 'Stop' # 启用错误检查
# 设置请求头,包含 GitHub Token
$headers = @{
"Authorization" = "Bearer $env:GITHUB_TOKEN"
"Accept" = "application/vnd.github.v3+json"
}
# 获取最新 Release 信息
try {
$githubRelease = Invoke-RestMethod -Uri "https://api.github.com/repos/${{ github.repository }}/releases/latest" -Headers $headers -ErrorAction Stop
$githubVersion = $githubRelease.tag_name -replace 'v', ''
Write-Host "Latest Termius version from GitHub Releases: $githubVersion"
} catch {
# 如果返回 404,说明没有 Release,设置默认版本号
if ($_.Exception.Response.StatusCode -eq 404) {
$githubVersion = "0.0.0"
Write-Host "No releases found. Setting default version to v$githubVersion."
} else {
# 其他错误,直接抛出异常
Write-Host "Failed to get GitHub Release: $($_.Exception.Message)"
exit 1
}
}
# 将版本号保存到环境变量
echo "GITHUB_VERSION=$githubVersion" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
- name: Compare versions
id: compare_versions
shell: pwsh
run: |
$ErrorActionPreference = 'Stop' # 启用错误检查
if ([version]$env:CHOCO_VERSION -gt [version]$env:GITHUB_VERSION) {
Write-Host "Chocolatey version ($env:CHOCO_VERSION) is higher than GitHub Release version ($env:GITHUB_VERSION). Continuing workflow."
echo "CONTINUE_WORKFLOW=true" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
} else {
Write-Host "Chocolatey version ($env:CHOCO_VERSION) is lower than or equal to GitHub Release version ($env:GITHUB_VERSION). Exiting workflow."
echo "CONTINUE_WORKFLOW=false" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
}
- name: Install Termius using Chocolatey
if: env.CONTINUE_WORKFLOW == 'true'
shell: pwsh
run: |
$ErrorActionPreference = 'Stop' # 启用错误检查
choco install termius -y --no-progress
if ($LASTEXITCODE -ne 0) {
Write-Host "Termius installation failed"
exit 1
} else {
Write-Host "Termius installed successfully"
}
- name: Install asar
if: env.CONTINUE_WORKFLOW == 'true'
shell: pwsh
run: |
$ErrorActionPreference = 'Stop' # 启用错误检查
npm install -g asar
- name: Generate and prepare assets
if: env.CONTINUE_WORKFLOW == 'true'
shell: pwsh
run: |
$ErrorActionPreference = 'Stop' # 启用错误检查
# 创建临时目录
$tempDir = New-Item -ItemType Directory -Path "$env:RUNNER_TEMP\termius_assets"
Write-Host "Temporary directory created at: $tempDir"
# 定义源文件路径
$sourcePath = "$env:USERPROFILE\AppData\Local\Programs\Termius\resources\app.asar"
# 定义生成文件的函数
function Generate-Asset {
param (
[string]$scriptArgs,
[string]$destFileName
)
python lang.py $scriptArgs
if ($LASTEXITCODE -ne 0) {
Write-Host "生成 $destFileName 失败"
exit 1
}
$destPath = "$tempDir\$destFileName"
Copy-Item -Path $sourcePath -Destination $destPath
Write-Host "已将 $destFileName 复制到临时目录: $destPath"
}
# 生成 app.asar 和 app-crack.asar
Generate-Asset -scriptArgs "-R" -destFileName $env:APP_FILE
Generate-Asset -scriptArgs "-RK" -destFileName $env:APP_CRACK_FILE
# 保存临时目录路径到环境变量
echo "TEMP_DIR=$tempDir" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
- name: Update Release Notes with version
if: env.CONTINUE_WORKFLOW == 'true'
id: update_release_notes
shell: pwsh
run: |
$ErrorActionPreference = 'Stop' # 启用错误检查
# 读取 RELEASE_NOTES.md 文件内容
$releaseNotes = Get-Content -Path .\RELEASE_NOTES.md -Raw
# 定义版本号变量
$releaseVersion = "v$env:CHOCO_VERSION"
# 替换版本号占位符
$updatedReleaseNotes = $releaseNotes -replace '\{\{VERSION\}\}', $releaseVersion
# 将更新后的内容保存到环境变量
echo "UPDATED_RELEASE_NOTES<<EOF" >> $env:GITHUB_ENV
echo "$updatedReleaseNotes" >> $env:GITHUB_ENV
echo "EOF" >> $env:GITHUB_ENV
- name: Create Release and Upload Assets
if: env.CONTINUE_WORKFLOW == 'true'
uses: ncipollo/release-action@v1
with:
allowUpdates: true
tag: v${{ env.CHOCO_VERSION }} # 使用 Chocolatey 版本号作为标签
body: ${{ env.UPDATED_RELEASE_NOTES }} # 使用更新后的 Release Notes
draft: true # 创建 Draft Release
prerelease: false
artifacts: ${{ env.TEMP_DIR }}/*
token: ${{ secrets.GITHUB_TOKEN }}
- name: Clean up
if: always()
shell: pwsh
run: |
$ErrorActionPreference = 'Stop' # 启用错误检查
if ($env:TEMP_DIR -and (Test-Path $env:TEMP_DIR)) {
Write-Host "清理临时目录: $env:TEMP_DIR"
Remove-Item -Recurse -Force $env:TEMP_DIR
} else {
Write-Host "临时目录未设置或不存在,无需清理。"
}