set hardocded version to 0.0.0 #1
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: Publish to TestPyPI | |
on: | |
pull_request: | |
types: [opened, synchronize] # Runs on new PRs and updates | |
push: | |
branches: | |
- main # Runs on PR merge | |
jobs: | |
publish: | |
runs-on: ubuntu-latest | |
permissions: | |
id-token: write # Required for Trusted Publisher authentication | |
defaults: | |
run: | |
working-directory: packages/python # Set working directory | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.10' | |
- name: Install dependencies | |
run: | | |
pip install --upgrade setuptools wheel twine tomli tomli-w build | |
- name: Determine Commit SHA | |
run: | | |
# Determine the correct commit SHA based on event type | |
if [ "${{ github.event_name }}" == "pull_request" ]; then | |
LAST_COMMIT_SHA=${{ github.event.pull_request.head.sha }} | |
else | |
LAST_COMMIT_SHA=${{ github.sha }} | |
fi | |
echo "LAST_COMMIT_SHA=$LAST_COMMIT_SHA" >> $GITHUB_ENV | |
- name: Extract Version and Generate New Version | |
run: | | |
# Get main version from pyproject.toml | |
MAIN_VERSION=$(python3 -c "import tomli; from pathlib import Path; \ | |
content = Path('pyproject.toml').read_text(); \ | |
print(tomli.loads(content)['project']['version'])") | |
# Convert SHA to numeric value | |
SHORT_SHA=$(git rev-parse --short ${LAST_COMMIT_SHA}) | |
NUMERIC_SHA=$(echo "ibase=16; $(echo $SHORT_SHA | tr '[:lower:]' '[:upper:]')" | bc) | |
# Generate new version based on event type | |
if [ "${{ github.event_name }}" == "pull_request" ]; then | |
PR_NUMBER=${{ github.event.pull_request.number }} | |
NEW_VERSION="${MAIN_VERSION}rc${NUMERIC_SHA}.dev${PR_NUMBER}" | |
else | |
NEW_VERSION="${MAIN_VERSION}rc${NUMERIC_SHA}" | |
fi | |
echo "Extracted main version: $MAIN_VERSION" | |
echo "New version: $NEW_VERSION" | |
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV | |
# Update version in pyproject.toml | |
python3 -c " | |
import tomli, tomli_w | |
from pathlib import Path | |
toml_path = Path('pyproject.toml') | |
data = tomli.loads(toml_path.read_text()) | |
data['project']['version'] = '$NEW_VERSION' | |
toml_path.write_text(tomli_w.dumps(data)) | |
" | |
- name: Build the package | |
run: | | |
rm -rf dist/* | |
python3 -m build | |
- name: Publish to TestPyPI using Trusted Publisher | |
run: | | |
twine upload --repository testpypi dist/* --verbose | |
- name: Display Package Info | |
run: | | |
PACKAGE_NAME=$(python3 -c "import tomli; from pathlib import Path; \ | |
content = Path('pyproject.toml').read_text(); \ | |
print(tomli.loads(content)['project']['name'])") | |
echo "Successfully published $PACKAGE_NAME==$NEW_VERSION to TestPyPI!" | |
echo -e "Install it using:" | |
echo -e "\tpip install -i https://test.pypi.org/simple/ $PACKAGE_NAME==$NEW_VERSION" |