DoroHelper/.github/workflows/Compile AHK 2.0 and Release...

112 lines
5.3 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

name: Compile AHK 2.0 and Release
on:
push:
tags:
- "v**" # 当有以 'v' 开头的标签推送到仓库时触发
workflow_dispatch: # 允许手动触发
jobs:
build-and-release:
runs-on: windows-latest # 运行环境是 Windows 最新版本
steps:
- name: Checkout code
uses: actions/checkout@v4 # 签出仓库代码
with:
fetch-depth: 0 # 获取所有历史,包括标签,以便进行版本比较
- name: Compile AutoHotkey Script with Action
id: compile_script # 为此步骤设置 ID以便后续引用其输出或工作目录
uses: benmusson/ahk2exe-action@v1 # 使用 benmusson 提供的 AHK 编译 Action
with:
in: .\DoroHelper.ahk # 你的 AHK 脚本路径
out: .\DoroHelper.exe # 输出的 EXE 文件名
icon: .\doro.ico # 你的图标文件路径
target: x64 # 目标架构为 x64
ahk-tag: v2.0.19 # 指定 AutoHotkey 2.0.19 版本
github-token: ${{ secrets.GITHUB_TOKEN }} # 用于避免 GitHub API 限速
- name: Extract Version from AHK script
id: get_version # 为此步骤设置 ID以便后续引用其输出
shell: pwsh # 使用 PowerShell Core
run: |
$scriptPath = "DoroHelper.ahk" # 你的主脚本文件路径
if (-not (Test-Path $scriptPath)) {
Write-Warning "Script file '${scriptPath}' not found for version extraction. Current directory: $(Get-Location)"
echo "VERSION_TAG=0.0.0" >> $env:GITHUB_OUTPUT # 如果找不到脚本,使用默认版本号
exit 0
}
$content = Get-Content -Path $scriptPath -Raw # 读取脚本内容
# 使用正则表达式匹配 currentVersion := "..."
$match = $content | Select-String -Pattern 'currentVersion\s*:=\s*"([^"]+)"'
if ($match) {
$version = $match.Matches[0].Groups[1].Value # 提取版本号
Write-Host "Version found: ${version}"
echo "VERSION_TAG=$version" >> $env:GITHUB_OUTPUT # 设置输出变量 VERSION_TAG
} else {
Write-Warning "Version pattern (currentVersion := \"...\") not found in ${scriptPath}. Using default '0.0.0'."
echo "VERSION_TAG=0.0.0" >> $env:GITHUB_OUTPUT # 如果没有匹配到,使用默认版本号
}
- name: Determine if Pre-release
id: prerelease_check # 为此步骤设置 ID以便后续引用其输出
shell: bash # 使用 Bash shell
run: |
VERSION_TAG="${{ steps.get_version.outputs.VERSION_TAG }}" # 获取上一步输出的版本号
IS_PRERELEASE="false"
if [[ "$VERSION_TAG" == *beta* || "$VERSION_TAG" == *alpha* || "$VERSION_TAG" == *rc* ]]; then # 判断是否包含预发布标识符
IS_PRERELEASE="true"
fi
echo "IS_PRERELEASE=$IS_PRERELEASE" >> $GITHUB_OUTPUT # 设置输出变量 IS_PRERELEASE
echo "Release version: ${VERSION_TAG}, Is Prerelease: ${IS_PRERELEASE}"
- name: Generate Changelog
id: changelog # 为此步骤设置 ID以便后续引用其输出
shell: bash
run: |
CURRENT_TAG="${{ steps.get_version.outputs.VERSION_TAG }}"
# 获取所有标签并排序,跳过当前的标签
LAST_TAG=$(git tag --sort=-v:refname | grep -v "${CURRENT_TAG}" | head -n 1)
echo "Current Tag: ${CURRENT_TAG}"
echo "Last Tag: ${LAST_TAG}"
CHANGELOG_BODY=""
if [ -z "${LAST_TAG}" ]; then
# 如果没有上一个标签,则获取从第一次提交到当前标签的所有提交
echo "No previous tag found. Generating changelog from initial commit to ${CURRENT_TAG}."
CHANGELOG_BODY=$(git log --pretty=format:"* %s (%h)" "${CURRENT_TAG}")
else
# 获取上一个标签到当前标签之间的所有提交
echo "Generating changelog from ${LAST_TAG}..${CURRENT_TAG}."
CHANGELOG_BODY=$(git log --pretty=format:"* %s (%h)" "${LAST_TAG}..${CURRENT_TAG}")
fi
# 将 CHANGELOG_BODY 中的换行符替换为 %0A并进行 URL 编码以确保多行内容能够正确传递
CHANGELOG_BODY="${CHANGELOG_BODY//'%'/'%25'}" # 先转义百分号
CHANGELOG_BODY="${CHANGELOG_BODY//$'\n'/'%0A'}" # 再转义换行符
CHANGELOG_BODY="${CHANGELOG_BODY//$'\r'/'%0D'}" # 转义回车符
echo "CHANGELOG_BODY=${CHANGELOG_BODY}" >> "$GITHUB_OUTPUT"
- name: Create GitHub Release
uses: softprops/action-gh-release@v2 # 使用 GitHub Release Action
with:
tag_name: ${{ steps.get_version.outputs.VERSION_TAG }} # 使用从脚本提取的版本号作为标签名
name: ${{ steps.get_version.outputs.VERSION_TAG }} # Release 的标题
body: |
${{ steps.changelog.outputs.CHANGELOG_BODY }}
---
测试版兼容性较差,请谨慎下载
# Automated release for DoroHelper ${{ steps.get_version.outputs.VERSION_TAG }}
# Compiled from commit ${{ github.sha }}
draft: false # 不是草稿 Release
prerelease: ${{ steps.prerelease_check.outputs.IS_PRERELEASE }} # 根据上一步的判断设置是否为预发布
files: DoroHelper.exe
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # GitHub 令牌