-
Notifications
You must be signed in to change notification settings - Fork 0
157 lines (134 loc) · 5.64 KB
/
create-release.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
name: Create Tag and Release
on:
pull_request:
types: [opened, reopened, synchronize, edited] # 添加更多触发类型以便调试
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