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 }} # 在创建标签后,尝试更新一下本地的 Git 标签缓存,以防万一 - name: Fetch all tags again (after potential tag creation) run: git fetch --tags - name: Determine if Pre-release id: prerelease_check shell: bash 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 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 # 如果没有上一个标签,或者新创建的标签在当前Git环境中可能还没完全同步 # 我们将从上一个提交到当前提交(HEAD)生成日志 echo "No previous tag found or current tag not fully recognized. Generating changelog from last commit to HEAD." # 获取最近的非合并提交作为起始点,或者直接从仓库的起源点开始 # git log HEAD^..HEAD 获取当前提交与前一个提交之间的差异 # git log --pretty=format:"* %s (%h)" HEAD 只获取当前提交的信息 # 为了确保包含所有自上次"已知"状态以来的提交 # 我们可以使用 git log ..HEAD 或者直接 HEAD # 但如果您确定这是第一次发布,那么从 HEAD 开始生成所有日志也可以 # 最稳妥的方法是,如果LAST_TAG为空,日志内容是“首次发布”或只包含当前提交 # 如果您想包含所有历史,需要从根提交开始,但这通常不适用于“更新日志” CHANGELOG_BODY=$(git log --pretty=format:"* %s (%h)" HEAD) echo "Note: This is likely the first release or a tag issue. Changelog shows current commit(s)." else # 获取上一个标签到当前标签之间的所有提交 # 这里使用 HEAD 作为终点,以确保获取到当前工作树中的所有提交 echo "Generating changelog from ${LAST_TAG}..HEAD" CHANGELOG_BODY=$(git log --pretty=format:"* %s (%h)" "${LAST_TAG}..HEAD") 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 }} - 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 }} body: | ${{ steps.changelog.outputs.CHANGELOG_BODY }} --- 测试版兼容性较差,请谨慎下载 draft: false prerelease: ${{ steps.prerelease_check.outputs.IS_PRERELEASE }} files: DoroHelper.exe env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}