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

213 lines
8.7 KiB
YAML

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). Using default '0.0.0'."
$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}"
}
if (-not $version.StartsWith("v")) {
$version = "v$version"
}
echo "VERSION_TAG=$version" >> $env:GITHUB_OUTPUT
- name: Check if version matches latest release tag
id: check_latest_release
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const currentVersionTag = '${{ steps.get_version.outputs.VERSION_TAG }}';
let latestReleaseTagName = null;
try {
const { data: latestRelease } = await github.rest.repos.getLatestRelease({
owner: context.repo.owner,
repo: context.repo.repo,
});
if (latestRelease && latestRelease.tag_name) {
latestReleaseTagName = latestRelease.tag_name;
console.log(`Latest release tag found: ${latestReleaseTagName}`);
} else {
console.log('No latest release found or tag_name is missing.');
}
} catch (error) {
if (error.status === 404) {
console.log('No releases found in the repository yet. Proceeding to create a new release.');
} else {
console.warn(`Could not fetch latest release: ${error.message}. Proceeding with caution.`);
// Do not fail here, allow the workflow to continue if API fails for other reasons
}
}
if (currentVersionTag && latestReleaseTagName && currentVersionTag === latestReleaseTagName) {
core.setFailed(`Version ${currentVersionTag} matches the latest release tag (${latestReleaseTagName}). Workflow terminated to prevent duplicate release.`);
} else {
console.log(`Version ${currentVersionTag} does not match latest release tag ${latestReleaseTagName} (or no latest release found). Proceeding with release.`);
}
- name: Create Git Tag
# This step will not run if 'check_latest_release' failed.
# The script handles "tag already exists" gracefully.
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }} # Explicitly provide token for clarity
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(`Attempting to create 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) {
// error.response.data.message often includes 'Reference already exists'
if (error.message && (error.message.includes('Reference already exists') || error.message.includes('Tag already exists'))) {
console.warn(`Tag ${tag} already exists on remote. Skipping creation.`);
} else {
core.setFailed(`Failed to create tag ${tag}: ${error.message || error}`);
throw error; // Re-throw to ensure workflow fails if it's an unexpected error
}
}
- name: Fetch all tags again (after potential tag creation)
run: git fetch --tags --force # Use --force to ensure local tags are updated correctly
- 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 for Changelog: ${CURRENT_TAG}"
# Ensure local git knows about all tags, especially the one just created
# git fetch --tags # Already done in previous step, but good to be aware
ALL_SORTED_TAGS=$(git tag --sort=-v:refname)
echo "All sorted tags:"
echo "${ALL_SORTED_TAGS}"
LAST_TAG=""
FOUND_CURRENT_TAG=false
# Loop to find the tag immediately preceding CURRENT_TAG in the sorted list
while IFS= read -r tag_item; do
if [ "$FOUND_CURRENT_TAG" = true ]; then
# The first tag encountered after CURRENT_TAG is the previous one in semantic versioning
LAST_TAG="$tag_item"
break
fi
if [ "$tag_item" = "$CURRENT_TAG" ]; then
FOUND_CURRENT_TAG=true
fi
done <<< "$ALL_SORTED_TAGS"
echo "Previous Tag for Changelog: ${LAST_TAG}"
CHANGELOG_BODY=""
if [ -z "${LAST_TAG}" ]; then
echo "No previous tag found. Generating changelog from the beginning up to ${CURRENT_TAG}."
# This will list all commits leading to the CURRENT_TAG if it's the first tag
CHANGELOG_BODY=$(git log --pretty=format:"* %s (%h)" "${CURRENT_TAG}")
if [ -z "$CHANGELOG_BODY" ]; then
CHANGELOG_BODY="* Initial release or no new commits to list for ${CURRENT_TAG}."
fi
else
echo "Generating changelog from ${LAST_TAG}..${CURRENT_TAG}"
CHANGELOG_BODY=$(git log --pretty=format:"* %s (%h)" "${LAST_TAG}..${CURRENT_TAG}")
if [ -z "$CHANGELOG_BODY" ]; then
CHANGELOG_BODY="* No changes detected between ${LAST_TAG} and ${CURRENT_TAG}."
fi
fi
echo "CHANGELOG_BODY<<EOF" >> "$GITHUB_OUTPUT"
echo "$CHANGELOG_BODY" >> "$GITHUB_OUTPUT"
echo "EOF" >> "$GITHUB_OUTPUT"
echo "--- Generated Changelog ---"
echo "$CHANGELOG_BODY"
echo "-------------------------"
- 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 # 指定 AHK v2 版本
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: Release ${{ 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 }}