Fix/custom template usage (#23) #15
This file contains hidden or 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: Tag and Release | |
on: | |
push: | |
branches: | |
- main | |
workflow_dispatch: | |
inputs: | |
tag: | |
description: 'Tag to create release from (e.g. v1.2.3)' | |
required: false | |
type: string | |
jobs: | |
tag-and-release: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
# Get version from pyproject.toml on push | |
- name: Get version from pyproject.toml | |
if: github.event_name == 'push' | |
id: auto_version | |
run: | | |
VERSION=$(grep '^version = ' pyproject.toml | cut -d'"' -f2) | |
echo "VERSION=$VERSION" | |
echo "version=$VERSION" >> $GITHUB_OUTPUT | |
# Get version from manual input | |
- name: Get version from input | |
if: github.event_name == 'workflow_dispatch' && inputs.tag != '' | |
id: manual_version | |
run: | | |
VERSION=${{ inputs.tag }} | |
VERSION=${VERSION#v} | |
echo "VERSION=$VERSION" | |
echo "version=$VERSION" >> $GITHUB_OUTPUT | |
# Combine versions and verify | |
- name: Set final version | |
id: version | |
run: | | |
VERSION="${{ steps.auto_version.outputs.version || steps.manual_version.outputs.version }}" | |
if [ -z "$VERSION" ]; then | |
echo "Error: No version found" | |
exit 1 | |
fi | |
echo "VERSION=$VERSION" | |
echo "version=$VERSION" >> $GITHUB_OUTPUT | |
# Check if tag exists | |
- name: Check for existing tag | |
id: check_tag | |
run: | | |
if git tag -l "v${{ steps.version.outputs.version }}" | grep -q .; then | |
echo "exists=true" >> $GITHUB_OUTPUT | |
echo "Tag v${{ steps.version.outputs.version }} already exists. Skipping release." | |
exit 0 | |
else | |
echo "exists=false" >> $GITHUB_OUTPUT | |
fi | |
# Create new tag if it doesn't exist | |
- name: Create tag | |
if: steps.check_tag.outputs.exists != 'true' | |
run: | | |
git config --global user.name 'github-actions[bot]' | |
git config --global user.email 'github-actions[bot]@users.noreply.github.com' | |
git tag -a "v${{ steps.version.outputs.version }}" -m "Release v${{ steps.version.outputs.version }}" | |
git push origin "v${{ steps.version.outputs.version }}" | |
# Setup Python for build | |
- name: Set up Python | |
if: steps.check_tag.outputs.exists != 'true' | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.8' | |
# Install build tools | |
- name: Install build tools | |
if: steps.check_tag.outputs.exists != 'true' | |
run: | | |
python -m pip install --upgrade pip | |
pip install build twine | |
# Build package | |
- name: Build package | |
if: steps.check_tag.outputs.exists != 'true' | |
run: python -m build | |
# Get release notes | |
- name: Get release notes | |
if: steps.check_tag.outputs.exists != 'true' | |
run: | | |
VERSION=${{ steps.version.outputs.version }} | |
echo "# Release v$VERSION" > release_notes.md | |
if [ -f CHANGELOG.md ]; then | |
echo "" >> release_notes.md | |
sed -n "/## \[${VERSION}\]/,/## \[/p" CHANGELOG.md | sed '$d' >> release_notes.md || true | |
fi | |
# Create GitHub Release | |
- name: Create GitHub Release | |
if: steps.check_tag.outputs.exists != 'true' | |
uses: softprops/action-gh-release@v1 | |
with: | |
tag_name: v${{ steps.version.outputs.version }} | |
body_path: release_notes.md | |
files: dist/* | |
# Publish to PyPI | |
- name: Publish to PyPI | |
if: steps.check_tag.outputs.exists != 'true' | |
env: | |
TWINE_USERNAME: __token__ | |
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }} | |
run: twine upload dist/* |