-
Notifications
You must be signed in to change notification settings - Fork 0
128 lines (119 loc) · 4.58 KB
/
release.yml
File metadata and controls
128 lines (119 loc) · 4.58 KB
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
name: Create Release
on:
push:
tags:
- 'v*.*.*' # Triggers on semantic version tags like v1.0.0, v2.1.3, etc.
workflow_dispatch:
inputs:
version:
description: 'Release version (e.g., 1.0.0 or v1.0.0)'
required: true
type: string
prerelease:
description: 'Mark as pre-release?'
required: false
type: boolean
default: false
draft:
description: 'Create as draft?'
required: false
type: boolean
default: false
permissions:
contents: write # Required to create releases
jobs:
create-release:
name: Create GitHub Release
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for proper release notes/changelog generation
- name: Extract version
id: version
env:
INPUT_VERSION_ARG: ${{ github.event.inputs.version }}
run: |
if [ -n "$INPUT_VERSION_ARG" ]; then
# Manual trigger - use input version
INPUT_VERSION="$INPUT_VERSION_ARG"
# Ensure version starts with 'v' for the tag, but keep raw version clean
if [[ "$INPUT_VERSION" == v* ]]; then
TAG_NAME="$INPUT_VERSION"
VERSION="${INPUT_VERSION#v}"
else
TAG_NAME="v$INPUT_VERSION"
VERSION="$INPUT_VERSION"
fi
echo "Manual trigger detected."
else
# Tag push - extract from ref
TAG_NAME=${GITHUB_REF#refs/tags/}
VERSION=${TAG_NAME#v}
echo "Tag push detected."
fi
echo "tag=$TAG_NAME" >> $GITHUB_OUTPUT
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "Selected Tag: $TAG_NAME"
echo "Selected Version: $VERSION"
- name: Check if pre-release
id: prerelease
env:
INPUT_PRERELEASE: ${{ github.event.inputs.prerelease }}
run: |
# Check if manually triggered with prerelease flag
if [ "$INPUT_PRERELEASE" == "true" ]; then
echo "is_prerelease=true" >> $GITHUB_OUTPUT
echo "Manual prerelease flag set."
else
# Auto-detect from version string (works for both manual and auto triggers)
VERSION="${{ steps.version.outputs.version }}"
if [[ "$VERSION" =~ -(alpha|beta|rc|dev) ]]; then
echo "is_prerelease=true" >> $GITHUB_OUTPUT
echo "Auto-detected pre-release from version string."
else
echo "is_prerelease=false" >> $GITHUB_OUTPUT
echo "Auto-detected stable release."
fi
fi
- name: Extract version changelog
id: release_notes
env:
VERSION: ${{ steps.version.outputs.version }}
run: |
python3 << 'PYEOF'
import re, os, sys
version = os.environ['VERSION']
try:
with open('CHANGELOG.md', 'r') as f:
content = f.read()
# Match "## {version}" section up to next "## " heading or EOF
pattern = rf'^## {re.escape(version)}\n(.*?)(?=^## |\Z)'
m = re.search(pattern, content, re.MULTILINE | re.DOTALL)
if m:
section = f'## {version}\n' + m.group(1).rstrip()
with open('version_changelog.md', 'w') as f:
f.write(section)
print(f'Extracted changelog section for version {version}')
else:
print(f'Warning: No section found for {version} in CHANGELOG.md', file=sys.stderr)
open('version_changelog.md', 'w').close()
except FileNotFoundError:
print('Warning: CHANGELOG.md not found', file=sys.stderr)
open('version_changelog.md', 'w').close()
PYEOF
- name: Create Release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ steps.version.outputs.tag }}
name: Release ${{ steps.version.outputs.version }}
body_path: version_changelog.md
prerelease: ${{ steps.prerelease.outputs.is_prerelease }}
draft: ${{ github.event.inputs.draft || 'false' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Release created successfully
run: |
echo "✅ Release ${{ steps.version.outputs.tag }} created successfully!"
echo "🔗 View at: ${{ github.server_url }}/${{ github.repository }}/releases/tag/${{ steps.version.outputs.tag }}"