Skip to content

Commit 83e3bd5

Browse files
committed
Auto release when add tag
1 parent a1d8bfb commit 83e3bd5

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

.github/workflows/release.yml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*'
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
release:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v5
17+
with:
18+
fetch-depth: 0
19+
20+
- name: Extract tag name
21+
id: tag
22+
run: echo "TAG_NAME=${GITHUB_REF#refs/tags/}" >> "$GITHUB_OUTPUT"
23+
24+
- name: Extract changelog for this version
25+
id: changelog
26+
run: |
27+
# Extract the current version's changelog
28+
TAG="${{ steps.tag.outputs.TAG_NAME }}"
29+
TAG_WITHOUT_V="${TAG#v}"
30+
31+
# Create a temporary file for the changelog content
32+
CHANGELOG_FILE="changelog_content.md"
33+
34+
# Extract content between the current version and the next version header
35+
# This handles different possible formats: ## [x.y.z], ## x.y.z, ### [x.y.z], ### x.y.z
36+
awk -v ver="$TAG_WITHOUT_V" '
37+
BEGIN { found = 0; }
38+
$0 ~ "^#{2,3} \\[?" ver "\\]?" { found = 1; next; }
39+
/^#{2,3} \[?[0-9]+\.[0-9]+\.[0-9]+\]?/ { if (found) exit; }
40+
found { print; }
41+
' CHANGELOG.md > "$CHANGELOG_FILE"
42+
43+
# If no content found with version without 'v', try with 'v'
44+
if [ ! -s "$CHANGELOG_FILE" ]; then
45+
awk -v ver="$TAG" '
46+
BEGIN { found = 0; }
47+
$0 ~ "^#{2,3} \\[?" ver "\\]?" { found = 1; next; }
48+
/^#{2,3} \[?v?[0-9]+\.[0-9]+\.[0-9]+\]?/ { if (found) exit; }
49+
found { print; }
50+
' CHANGELOG.md > "$CHANGELOG_FILE"
51+
fi
52+
53+
# If still no content, use a default message
54+
if [ ! -s "$CHANGELOG_FILE" ]; then
55+
echo "No changelog entry found for version $TAG" > "$CHANGELOG_FILE"
56+
echo ""
57+
echo "Please check CHANGELOG.md for updates." >> "$CHANGELOG_FILE"
58+
fi
59+
60+
# Set the output
61+
{
62+
echo 'CHANGELOG_CONTENT<<EOF'
63+
cat "$CHANGELOG_FILE"
64+
echo 'EOF'
65+
} >> "$GITHUB_OUTPUT"
66+
67+
- name: Create Release
68+
uses: softprops/action-gh-release@v2
69+
with:
70+
tag_name: ${{ steps.tag.outputs.TAG_NAME }}
71+
name: ${{ steps.tag.outputs.TAG_NAME }}
72+
body: ${{ steps.changelog.outputs.CHANGELOG_CONTENT }}
73+
draft: false
74+
prerelease: ${{ contains(steps.tag.outputs.TAG_NAME, '-alpha') || contains(steps.tag.outputs.TAG_NAME, '-beta') || contains(steps.tag.outputs.TAG_NAME, '-rc') }}
75+
generate_release_notes: false
76+
env:
77+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)