fix: update release workflow to add more trigger types and improve de… #39
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Create Tag and Release | |
on: | |
pull_request: | |
# types: [opened, reopened, synchronize, edited] # 添加更多触发类型以便调试 | |
types: [closed] | |
branches: | |
- main | |
# - v1.3.0 | |
concurrency: | |
group: create-tag-and-release-${{ github.ref }} | |
cancel-in-progress: true | |
jobs: | |
validate_and_tag: | |
# 暂时注释掉 merged 条件以便调试 | |
if: github.event.pull_request.merged == true | |
runs-on: ubuntu-latest | |
outputs: | |
version: ${{ steps.validate_title.outputs.version }} | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
with: | |
token: ${{ secrets.PAT_TOKEN }} | |
submodules: "recursive" | |
- name: Validate branch name and extract version | |
id: validate_title | |
run: | | |
# 打印环境变量以进行调试 | |
echo "GITHUB_HEAD_REF: ${GITHUB_HEAD_REF}" | |
echo "GITHUB_REF: ${GITHUB_REF}" | |
echo "GITHUB_EVENT_NAME: ${GITHUB_EVENT_NAME}" | |
echo "GITHUB_BASE_REF: ${GITHUB_BASE_REF}" | |
# 设置分支名 | |
branch_name="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" | |
echo "Initial branch_name: $branch_name" | |
# 移除空白字符并打印结果 | |
branch_name=$(echo "$branch_name" | tr -d '[:space:]') | |
echo "Cleaned branch_name: $branch_name" | |
# 使用 xxd 显示确切的字符 | |
echo "Branch name hex dump:" | |
echo -n "$branch_name" | xxd | |
# 测试正则表达式匹配 | |
if echo "$branch_name" | grep -qE '^v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9._-]+)?$'; then | |
version=$(echo "$branch_name" | grep -oE '^v[0-9]+\.[0-9]+\.[0-9]+') | |
echo "version=$version" >> "$GITHUB_OUTPUT" | |
echo "Matched version: $version" | |
echo "Full branch name matched the pattern successfully" | |
else | |
echo "ERROR: Branch name '$branch_name' does not match the pattern" | |
echo "Expected format: vX.X.X or vX.X.X-suffix" | |
echo "Where X is a number and suffix can contain letters, numbers, underscores, hyphens and dots" | |
exit 1 | |
fi | |
shell: bash | |
- name: Create a tag | |
if: ${{ steps.validate_title.outputs.version != '' }} | |
run: | | |
version=${{ steps.validate_title.outputs.version }} | |
echo "Creating tag for version: $version" | |
# 检查本地是否存在该 Tag | |
tag_exists=$(git tag -l "$version") | |
echo "Tag exists locally: $tag_exists" | |
if [ -z "$tag_exists" ]; then | |
echo "Creating new local tag..." | |
git tag "$version" | |
# 检查远程是否存在该 Tag | |
remote_tag_exists=$(git ls-remote --tags origin | grep "refs/tags/$version" || true) | |
echo "Tag exists in remote: $remote_tag_exists" | |
if [ -z "$remote_tag_exists" ]; then | |
echo "Pushing tag to remote..." | |
git push origin "$version" | |
echo "Tag $version created and pushed successfully." | |
else | |
echo "Tag $version already exists in remote. Skipping push." | |
fi | |
else | |
echo "Tag $version already exists locally. Skipping tag creation and push." | |
fi | |
shell: bash | |
- name: List all files in the workspace | |
run: | | |
echo "Workspace directory: $GITHUB_WORKSPACE" | |
ls -la $GITHUB_WORKSPACE | |
echo "Listing all files recursively:" | |
find $GITHUB_WORKSPACE | |
- name: Create release assets archive | |
run: | | |
output_zip="sub-adjust_${{ steps.validate_title.outputs.version }}.zip" | |
assets_file="$GITHUB_WORKSPACE/.github/workflows/PackageAssets.txt" | |
echo "Creating zip file: $output_zip" | |
echo "Assets file path: $assets_file" | |
if [ -f "$assets_file" ]; then | |
echo "Content of PackageAssets.txt:" | |
cat "$assets_file" | |
# 创建包含指定文件的压缩包 | |
while IFS= read -r asset; do | |
echo "Adding asset: $asset" | |
if [ -f "$GITHUB_WORKSPACE/$asset" ]; then | |
zip -j "$output_zip" "$GITHUB_WORKSPACE/$asset" | |
else | |
echo "Warning: File not found - $GITHUB_WORKSPACE/$asset" | |
fi | |
done < "$assets_file" | |
else | |
echo "Warning: Assets file not found at $assets_file" | |
fi | |
shell: bash | |
# Create a release and upload the zip file | |
- name: Create GitHub release | |
id: create_release | |
if: ${{ steps.validate_title.outputs.version != '' }} | |
uses: actions/create-release@latest | |
env: | |
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }} | |
with: | |
tag_name: ${{ steps.validate_title.outputs.version }} | |
release_name: ${{ steps.validate_title.outputs.version }} | |
body: | | |
Automatically created release for version ${{ steps.validate_title.outputs.version }}. | |
### PR Description: | |
${{ github.event.pull_request.body }} | |
draft: false | |
prerelease: false | |
commitish: ${{ github.sha }} | |
# Upload the zip file as the release asset | |
- name: Upload zip file to release | |
if: ${{ steps.validate_title.outputs.version != '' }} | |
uses: actions/upload-release-asset@v1 | |
env: | |
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }} | |
with: | |
upload_url: ${{ steps.create_release.outputs.upload_url }} | |
asset_path: ./sub-adjust_${{ steps.validate_title.outputs.version }}.zip | |
asset_name: sub-adjust_${{ steps.validate_title.outputs.version }}.zip | |
asset_content_type: application/zip |