Skip to content

Merge pull request #153 from fgcz/main #27

Merge pull request #153 from fgcz/main

Merge pull request #153 from fgcz/main #27

name: Publish Release
on:
push:
branches:
- release_bfabric
- release_bfabric_scripts
- release_bfabric_app_runner
workflow_dispatch:
inputs:
package:
description: 'Package to release'
type: choice
options:
- bfabric
- bfabric_scripts
- bfabric_app_runner
default: bfabric
environment:
description: 'Target PyPI'
type: choice
options:
- test
- production
default: test
required: true
jobs:
publish:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: write # permission to create tags and releases
steps:
- uses: actions/checkout@v4
# Step: Determine the package that is being built
- name: Set package variable
id: set-package
run: |
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
echo "PACKAGE=${{ github.event.inputs.package }}" >> $GITHUB_ENV
else
# Extract package name from branch name by removing 'release_' prefix
BRANCH_NAME="${{ github.ref_name }}"
PACKAGE=${BRANCH_NAME#release_}
echo "PACKAGE=$PACKAGE" >> $GITHUB_ENV
fi
# Step: Set the PyPI repository URL based on environment
- name: Set PyPI repository URL
id: set-repo-url
run: |
if [ "${{ github.event.inputs.environment }}" == "production" ] || [ "${{ github.event_name }}" == "push" ]; then
echo "PYPI_REPOSITORY_URL=https://upload.pypi.org/legacy/" >> $GITHUB_ENV
else
echo "PYPI_REPOSITORY_URL=https://test.pypi.org/legacy/" >> $GITHUB_ENV
fi
- name: Debug variables
run: |
echo "Selected package: ${{ env.PACKAGE }}"
echo "Target PyPI repository: ${{ env.PYPI_REPOSITORY_URL }}"
# Step: Build the package
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install hatch
run: pip install hatch
- name: Get package version
id: get-version
run: |
cd ${{ env.PACKAGE }}
VERSION=$(hatch version)
echo "VERSION=$VERSION" >> $GITHUB_ENV
echo "Package version: $VERSION"
- name: Build package
run: |
cd ${{ env.PACKAGE }}
hatch build
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: ${{ env.PYPI_REPOSITORY_URL }}
packages-dir: ${{ env.PACKAGE }}/dist
- name: Create and push tag
run: |
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
TAG_NAME="${{ env.PACKAGE }}/${{ env.VERSION }}"
git tag -a "$TAG_NAME" -m "Release ${{ env.PACKAGE }} version ${{ env.VERSION }}"
git push origin "$TAG_NAME"
- name: Extract changelog and create release
run: |
set -x
CHANGELOG_PATH="${{ env.PACKAGE }}/docs/changelog.md"
TAG_NAME="${{ env.PACKAGE }}/${{ env.VERSION }}"
if [ -f "$CHANGELOG_PATH" ]; then
# Extract the changelog section for the current version
# This assumes the changelog follows the keep-a-changelog format
CHANGELOG_CONTENT=$(python3 -c '
import re,sys
print(re.search(
rf"^## \\\[{re.escape(sys.argv[1])}\\\].*?(?=^## \\\[|\Z)",
open(sys.argv[2]).read(),
re.M|re.S
).group(0).strip())' "${VERSION}" "$CHANGELOG_PATH")
if [ -n "$CHANGELOG_CONTENT" ]; then
# Create GitHub release
gh release create "$TAG_NAME" \
--title "Release ${{ env.PACKAGE }} ${{ env.VERSION }}" \
--notes "$CHANGELOG_CONTENT" \
--target ${{ github.sha }} \
--draft
else
# Create release with basic notes if no changelog section is found
gh release create "$TAG_NAME" \
--title "Release ${{ env.PACKAGE }} ${{ env.VERSION }}" \
--notes "Release of ${{ env.PACKAGE }} version ${{ env.VERSION }}" \
--target ${{ github.sha }} \
--draft
fi
else
echo "Warning: Changelog file not found at $CHANGELOG_PATH"
# Create release with basic notes
gh release create "$TAG_NAME" \
--title "Release ${{ env.PACKAGE }} ${{ env.VERSION }}" \
--notes "Release of ${{ env.PACKAGE }} version ${{ env.VERSION }}" \
--target ${{ github.sha }} \
--draft
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Debug package info
run: |
echo "Built and published package: ${{ env.PACKAGE }}"
echo "Created tag: ${{ env.PACKAGE }}/${{ env.VERSION }}"
if [ "${{ env.PYPI_REPOSITORY_URL }}" == "https://pypi.org/legacy/" ]; then
echo "Check it at: https://pypi.org/project/${{ env.PACKAGE }}/"
else
echo "Check it at: https://test.pypi.org/project/${{ env.PACKAGE }}/"
fi
if: always()