Publish to PyPI and GitHub #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 PyPI and GitHub | ||
on: | ||
workflow_dispatch: | ||
inputs: | ||
tag: | ||
description: New version tag | ||
required: true | ||
jobs: | ||
final-linting: | ||
uses: ./.github/workflows/lint.yml | ||
build-publish: | ||
name: Build and publish to PyPI | ||
runs-on: ubuntu-latest | ||
needs: final-linting | ||
environment: | ||
name: release | ||
url: https://pypi.org/p/airthings-ble | ||
permissions: | ||
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing | ||
contents: write | ||
steps: | ||
- name: Checkout source | ||
uses: actions/checkout@v4 | ||
with: | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
fetch-depth: 0 | ||
- name: Set up Python | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: '3.11' | ||
- name: Update pyproject.toml Version | ||
run: | | ||
pip install tomlkit | ||
python -c " | ||
import tomlkit | ||
tag = '${{ github.event.inputs.tag }}' | ||
with open('pyproject.toml', 'r') as file: | ||
content = tomlkit.parse(file.read()) | ||
content['tool']['poetry']['version'] = tag | ||
with open('pyproject.toml', 'w') as file: | ||
file.write(tomlkit.dumps(content)) | ||
" | ||
- name: Update __init__.py version | ||
run: | | ||
version="${{ github.event.inputs.tag }}" | ||
file="airthings_ble/__init__.py" | ||
sed -i "s/__version__\\s*=\\s*[\"'].*[\"']/__version__ = \"$version\"/" $file | ||
- name: Commit and Push Changes | ||
run: | | ||
git config --global user.email "github-actions[bot]@users.noreply.github.com" | ||
git config --global user.name "GitHub Actions" | ||
git add pyproject.toml airthings_ble/__init__.py | ||
git commit -m "Update version to ${{ github.event.inputs.tag }}" | ||
git push | ||
git tag ${{ github.event.inputs.tag }} | ||
git push --tags | ||
- name: Build source and wheel distributions | ||
run: | | ||
python -m pip install --upgrade build twine | ||
python -m build | ||
twine check --strict dist/* | ||
- name: Publish distribution to PyPI | ||
uses: pypa/gh-action-pypi-publish@release/v1 | ||
- name: Release | ||
uses: softprops/action-gh-release@v1 | ||
with: | ||
files: dist/${{ env.name }}/* | ||
tag_name: ${{ github.event.inputs.tag }} | ||
generate_release_notes: true | ||