-
Notifications
You must be signed in to change notification settings - Fork 15
135 lines (110 loc) · 4.26 KB
/
release.yml
File metadata and controls
135 lines (110 loc) · 4.26 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
129
130
131
132
133
134
135
name: Release
on:
push:
tags:
- 'v*'
jobs:
release:
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write # For PyPI trusted publishing
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch full history for changelog generation
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install PDM
uses: pdm-project/setup-pdm@v4
with:
python-version: '3.11'
- name: Install dependencies
run: pdm install --dev
- name: Run tests
run: pdm run python -m pytest
- name: Run linting
run: pdm run ruff check .
- name: Run type checking
run: pdm run ty check src
- name: Build package
run: pdm build
- name: Generate changelog
id: changelog
run: |
# Extract version from tag
VERSION=${GITHUB_REF#refs/tags/v}
echo "version=$VERSION" >> $GITHUB_OUTPUT
# Try to extract changelog from CHANGELOG.md first
if [ -f "CHANGELOG.md" ]; then
# Look for the version section in CHANGELOG.md
VERSION=$VERSION python3 << 'EOF' > release_notes.md
import re
import sys
import os
def extract_version_changelog(version):
try:
with open('CHANGELOG.md', 'r', encoding='utf-8') as f:
content = f.read()
# Look for version section (matches [X.X.X] or [Unreleased])
pattern = rf'## \[{re.escape(version)}\][^\n]*\n(.*?)(?=\n## |\Z)'
match = re.search(pattern, content, re.DOTALL)
if match:
# Extract the content directly from capture group
changelog_content = match.group(1).strip()
if changelog_content:
print(changelog_content)
return True
return False
except Exception as e:
print(f"Error reading CHANGELOG.md: {e}", file=sys.stderr)
return False
# Try to get version-specific changelog
version = os.environ.get('VERSION', '0.0.0')
if not extract_version_changelog(version):
# If no specific version found, check for [Unreleased]
if not extract_version_changelog("Unreleased"):
print("## What's Changed\n\nSee CHANGELOG.md for detailed changes.")
EOF
fi
# If CHANGELOG.md doesn't exist or doesn't have version info, generate from git
if [ ! -s "release_notes.md" ]; then
echo "## What's Changed" > release_notes.md
echo "" >> release_notes.md
# Generate changelog between current tag and previous tag
PREVIOUS_TAG=$(git describe --tags --abbrev=0 HEAD~1 2>/dev/null || echo "")
if [ -n "$PREVIOUS_TAG" ]; then
# Get commit messages between tags
git log --pretty=format:"- %s (%h)" ${PREVIOUS_TAG}..HEAD >> release_notes.md
# Get contributors
echo "" >> release_notes.md
echo "## Contributors" >> release_notes.md
git log --pretty=format:"- @%an" ${PREVIOUS_TAG}..HEAD | sort -u >> release_notes.md
else
echo "- Initial release" >> release_notes.md
fi
fi
# Read changelog content and set as output
echo "changelog<<EOF" >> $GITHUB_OUTPUT
cat release_notes.md >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Create GitHub Release
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ github.ref_name }}
name: Release ${{ steps.changelog.outputs.version }}
body: ${{ steps.changelog.outputs.changelog }}
files: |
dist/*
draft: false
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: https://upload.pypi.org/legacy/
skip-existing: true