name: Compile AHK 2.0 and Release on: workflow_dispatch: # 允许手动触发 inputs: version_override: description: "Override version from AHK script (e.g., v1.0.0)" required: false type: string jobs: build-and-release: runs-on: windows-latest steps: - name: Checkout code uses: actions/checkout@v4 with: fetch-depth: 0 # 获取所有历史,包括标签,以便进行版本比较 - name: Extract Version from AHK script id: get_version shell: pwsh run: | $scriptPath = "DoroHelper.ahk" $version = "${{ github.event.inputs.version_override }}" if ([string]::IsNullOrWhiteSpace($version)) { if (-not (Test-Path $scriptPath)) { Write-Warning "Script file '${scriptPath}' not found for version extraction. Current directory: $(Get-Location)" $version = "0.0.0" # 如果找不到脚本,使用默认版本号 } else { $content = Get-Content -Path $scriptPath -Raw $match = $content | Select-String -Pattern 'currentVersion\s*:=\s*"([^"]+)"' if ($match) { $version = $match.Matches[0].Groups[1].Value Write-Host "Version found in AHK script: ${version}" } else { Write-Warning "Version pattern (currentVersion := \"...\") not found in ${scriptPath}. Using default '0.0.0'." $version = "0.0.0" } } } else { Write-Host "Using overridden version: ${version}" } # 确保版本号以 'v' 开头 if (-not $version.StartsWith("v")) { $version = "v$version" } echo "VERSION_TAG=$version" >> $env:GITHUB_OUTPUT - name: Determine if Tag Already Exists id: check_tag shell: bash env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | TAG_NAME="${{ steps.get_version.outputs.VERSION_TAG }}" echo "Checking if tag ${TAG_NAME} already exists..." TAG_EXISTS=$(git tag -l "${TAG_NAME}") if [ -n "$TAG_EXISTS" ]; then echo "Tag ${TAG_NAME} already exists." echo "TAG_EXISTS=true" >> "$GITHUB_OUTPUT" else echo "Tag ${TAG_NAME} does not exist." echo "TAG_EXISTS=false" >> "$GITHUB_OUTPUT" fi - name: Create Git Tag if Not Exists if: steps.check_tag.outputs.TAG_EXISTS == 'false' id: create_tag uses: actions/github-script@v6 with: script: | const tag = '${{ steps.get_version.outputs.VERSION_TAG }}'; const owner = context.repo.owner; const repo = context.repo.repo; const sha = context.sha; console.log(`Creating tag ${tag} at commit ${sha}`); try { await github.rest.git.createRef({ owner, repo, ref: `refs/tags/${tag}`, sha: sha }); console.log(`Tag ${tag} created successfully.`); } catch (error) { if (error.status === 422 && error.response.data.message.includes('Reference already exists')) { console.warn(`Tag ${tag} already exists, skipping creation.`); } else { throw error; } } github-token: ${{ secrets.GITHUB_TOKEN }} - name: Determine if Pre-release id: prerelease_check shell: bash run: | VERSION_TAG="${{ steps.get_version.outputs.VERSION_TAG }}" IS_PRERELEASE="false" # 检查版本号是否包含预发布标识符 (beta, alpha, rc) if [[ "$VERSION_TAG" == *beta* || "$VERSION_TAG" == *alpha* || "$VERSION_TAG" == *rc* ]]; then IS_PRERELEASE="true" fi echo "IS_PRERELEASE=$IS_PRERELEASE" >> $GITHUB_OUTPUT echo "Release version: ${VERSION_TAG}, Is Prerelease: ${IS_PRERELEASE}" - name: Generate Changelog id: changelog shell: bash run: | CURRENT_TAG="${{ steps.get_version.outputs.VERSION_TAG }}" echo "Current Tag: ${CURRENT_TAG}" ALL_SORTED_TAGS=$(git tag --sort=-v:refname) LAST_TAG="" FOUND_CURRENT_TAG=false while IFS= read -r tag; do if [ "$FOUND_CURRENT_TAG" = true ]; then LAST_TAG="$tag" break fi if [ "$tag" = "$CURRENT_TAG" ]; then FOUND_CURRENT_TAG=true fi done <<< "$ALL_SORTED_TAGS" 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 # 使用EOF语法输出多行字符串 echo "CHANGELOG_BODY<> "$GITHUB_OUTPUT" echo "$CHANGELOG_BODY" >> "$GITHUB_OUTPUT" echo "EOF" >> "$GITHUB_OUTPUT" - name: Compile AutoHotkey Script with Action id: compile_script uses: benmusson/ahk2exe-action@v1 with: in: .\DoroHelper.ahk out: .\DoroHelper.exe icon: .\doro.ico target: x64 ahk-tag: v2.0.19 github-token: ${{ secrets.GITHUB_TOKEN }} # 用于避免 GitHub API 限速 - name: Create GitHub Release uses: softprops/action-gh-release@v2 with: tag_name: ${{ steps.get_version.outputs.VERSION_TAG }} # 使用从脚本提取的版本号作为标签名 name: ${{ steps.get_version.outputs.VERSION_TAG }} # Release 的标题 body: | ${{ steps.changelog.outputs.CHANGELOG_BODY }} --- 测试版兼容性较差,请谨慎下载 draft: false prerelease: ${{ steps.prerelease_check.outputs.IS_PRERELEASE }} files: DoroHelper.exe env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}