Install and Publish Packages with Dynamic Versioning (PEP 440) #21
This file contains 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: Install and Publish Packages with Dynamic Versioning (PEP 440) | |
on: | |
workflow_dispatch: | |
inputs: | |
tag_name: | |
description: "The base tag for the release (e.g., v0.6.0)" | |
required: true | |
pre_release: | |
description: "The pre-release identifier (e.g., .dev1, .rc1, .alpha1)" | |
required: false | |
default: "" | |
jobs: | |
test-and-publish: | |
env: | |
UNIQUE_VENV_PATH: "${{ github.workspace }}/.venv_core_${{ github.run_id }}" | |
runs-on: self-hosted | |
steps: | |
- name: Check out the repository | |
uses: actions/checkout@v4 | |
- name: Set up Python 3.12 | |
uses: actions/setup-python@v5 | |
with: | |
python-version: "3.12" | |
- name: Install Poetry | |
run: | | |
curl -sSL https://install.python-poetry.org | python3 - | |
echo "PATH=$HOME/.local/bin:$PATH" >> $GITHUB_ENV | |
- name: Install Poetry-Dynamic-Versioning Plugin | |
run: poetry self add poetry-dynamic-versioning | |
- name: Extract Version from Tag | |
id: extract-version | |
run: | | |
FULL_TAG="${{ inputs.tag_name }}${{ inputs.pre_release }}" | |
echo "Full tag: $FULL_TAG" | |
# Remove the 'v' prefix if present and extract the version | |
VERSION=${FULL_TAG#v} | |
echo "Extracted version: $VERSION" | |
echo "version=$VERSION" >> $GITHUB_ENV | |
- name: Update pyproject.toml Files | |
run: | | |
for package in pkgs/*; do | |
if [ -d "$package" ] && [ -f "$package/pyproject.toml" ]; then | |
echo "Processing $package" | |
cd "$package" | |
# Update version field in pyproject.toml | |
sed -i "s/^version = \".*\"/version = \"${{ env.version }}\"/" pyproject.toml | |
# Replace path dependencies with the extracted version | |
sed -i "s/{ path = \"..\/core\" }/\"^${{ env.version }}\"/" pyproject.toml | |
sed -i "s/{ path = \"..\/base\" }/\"^${{ env.version }}\"/" pyproject.toml | |
cat pyproject.toml | |
cd - | |
fi | |
done | |
- name: Generate poetry.lock (if missing) | |
run: | | |
if [ ! -f "poetry.lock" ]; then | |
echo "poetry.lock not found. Generating it..." | |
cd pkgs | |
poetry lock | |
else | |
echo "poetry.lock exists." | |
fi | |
- name: Install All Packages | |
run: | | |
python -m venv $UNIQUE_VENV_PATH | |
source $UNIQUE_VENV_PATH/bin/activate | |
cd pkgs/ | |
poetry install --no-cache -vv --all-extras | |
- name: Install, Build, and Publish All Sub-Packages | |
run: | | |
source $UNIQUE_VENV_PATH/bin/activate | |
for package in pkgs/*; do | |
if [ -d "$package" ] && [ -f "$package/pyproject.toml" ]; then | |
echo "Processing $package" | |
cd "$package" | |
# Build the package with the dynamically generated version | |
poetry build | |
# Publish the package to PyPI | |
poetry publish --username __token__ --password "${{ secrets.DANGER_MASTER_PYPI_API_TOKEN }}" | |
cd - | |
fi | |
done | |
- name: Show Pip Freeze | |
run: | | |
source $UNIQUE_VENV_PATH/bin/activate | |
pip freeze | |
- name: Clean up Virtual Environment | |
if: always() | |
run: | | |
rm -rf ${{ env.UNIQUE_VENV_PATH }} |