update the build and publish CI #6
Workflow file for this run
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
| ## GitHub Actions Workflow: Build Package | |
| ## - Triggers: | |
| ## - Manual (workflow_dispatch): only 'dev' and 'custom' are allowed. | |
| ## - dev: updates version in pyproject.toml to X.Y.Z.devYYMMDD and builds. | |
| ## - custom: mutates version by appending the provided custom_suffix to base X.Y.Z and builds. | |
| ## - Tags (push): 'X.Y.Z', 'X.Y.ZrcN', 'X.Y.Z.postN' → no version mutation; build as-is. | |
| ## - Jobs: | |
| ## - validate: determines version_type and version_suffix (manual vs tags). | |
| ## - build: sets up Python + PDM, applies dev versioning only for manual dev builds, then builds and uploads wheel artifact. | |
| ## - release: on tags only, drafts a GitHub Release and attaches the built wheel. | |
| ## Notes: | |
| ## - Version mutation is performed only for manual dev builds; all tag builds keep the version from pyproject.toml. | |
| ## - PDM is set up via pdm-project/setup-pdm for consistency and caching. | |
| name: Build Package | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| version_type: | |
| description: 'Version type to use' | |
| required: true | |
| type: choice | |
| default: 'dev' | |
| options: | |
| - dev | |
| - custom | |
| custom_suffix: | |
| description: 'Custom version suffix (only used when version_type is custom)' | |
| required: false | |
| type: string | |
| push: | |
| tags: | |
| - '[0-9]+.[0-9]+.[0-9]+rc[0-9]+' | |
| - '[0-9]+.[0-9]+.[0-9]+' | |
| - '[0-9]+.[0-9]+.[0-9]+.post[0-9]+' | |
| jobs: | |
| validate: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| version_type: ${{ steps.check-tag.outputs.version_type || steps.set-default.outputs.version_type }} | |
| version_suffix: ${{ steps.check-tag.outputs.version_suffix || steps.set-default.outputs.version_suffix }} | |
| steps: | |
| - name: Check tag format | |
| id: check-tag | |
| if: startsWith(github.ref, 'refs/tags/') | |
| run: | | |
| TAG=${GITHUB_REF#refs/tags/} | |
| if [[ $TAG =~ ^[0-9]+\.[0-9]+\.[0-9]+\.post[0-9]+$ ]]; then | |
| echo "version_type=post" >> $GITHUB_OUTPUT | |
| POST_SUFFIX=${TAG#*post} | |
| echo "version_suffix=.post${POST_SUFFIX}" >> $GITHUB_OUTPUT | |
| elif [[ $TAG =~ ^[0-9]+\.[0-9]+\.[0-9]+rc[0-9]+$ ]]; then | |
| echo "version_type=rc" >> $GITHUB_OUTPUT | |
| RC_SUFFIX=${TAG#*rc} | |
| echo "version_suffix=rc${RC_SUFFIX}" >> $GITHUB_OUTPUT | |
| elif [[ $TAG =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then | |
| echo "version_type=release" >> $GITHUB_OUTPUT | |
| echo "version_suffix=" >> $GITHUB_OUTPUT | |
| else | |
| echo "Invalid tag format. Must be 'X.Y.Z' for release, 'X.Y.ZrcN' for release candidate, or 'X.Y.Z.postN' for post-release" | |
| exit 1 | |
| fi | |
| - name: Set default outputs | |
| id: set-default | |
| if: "!startsWith(github.ref, 'refs/tags/')" | |
| run: | | |
| VERSION_TYPE="${{ github.event.inputs.version_type || 'dev' }}" | |
| case "${VERSION_TYPE}" in | |
| "dev") | |
| echo "version_type=dev" >> $GITHUB_OUTPUT | |
| echo "version_suffix=.dev$(date +%y%m%d)" >> $GITHUB_OUTPUT | |
| ;; | |
| "custom") | |
| echo "version_type=custom" >> $GITHUB_OUTPUT | |
| echo "version_suffix=${{ github.event.inputs.custom_suffix }}" >> $GITHUB_OUTPUT | |
| ;; | |
| *) | |
| # Restrict manual triggers to dev/custom only; fallback to dev | |
| echo "version_type=dev" >> $GITHUB_OUTPUT | |
| echo "version_suffix=.dev$(date +%y%m%d)" >> $GITHUB_OUTPUT | |
| ;; | |
| esac | |
| build: | |
| needs: validate | |
| runs-on: ubuntu-latest | |
| outputs: | |
| wheel_name: ${{ steps.find-wheel.outputs.wheel_name }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.10' | |
| - name: Setup PDM | |
| uses: pdm-project/setup-pdm@v4 | |
| - name: Update version for dev/custom builds | |
| if: ${{ (needs.validate.outputs.version_type == 'dev' || needs.validate.outputs.version_type == 'custom') && !startsWith(github.ref, 'refs/tags/') }} | |
| run: | | |
| python .github/workflows/update_version.py "${{ needs.validate.outputs.version_type }}" "${{ needs.validate.outputs.version_suffix }}" | |
| - name: Build package | |
| run: | | |
| pdm build | |
| - name: Find wheel file | |
| id: find-wheel | |
| run: | | |
| WHEEL_FILE=$(ls dist/*.whl) | |
| WHEEL_NAME=$(basename $WHEEL_FILE .whl) | |
| echo "Found wheel file: $WHEEL_FILE" | |
| echo "wheel_file=$WHEEL_FILE" >> $GITHUB_OUTPUT | |
| echo "wheel_name=$WHEEL_NAME" >> $GITHUB_OUTPUT | |
| - name: Upload wheel artifacts | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: ${{ steps.find-wheel.outputs.wheel_name }} | |
| path: ${{ steps.find-wheel.outputs.wheel_file }} | |
| retention-days: 7 | |
| release: | |
| needs: [build, validate] | |
| if: startsWith(github.ref, 'refs/tags/') | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Download wheel artifact | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: ${{ needs.build.outputs.wheel_name }} | |
| path: dist | |
| - name: Create Release | |
| id: create_release | |
| uses: softprops/action-gh-release@v1 | |
| with: | |
| files: dist/*.whl | |
| generate_release_notes: false | |
| draft: true | |
| prerelease: ${{ needs.validate.outputs.version_type != 'release' }} | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |