Upload Python Package #161
Workflow file for this run
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
# This workflows will upload a Python Package using Twine when a release is created | |
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries | |
name: Upload Python Package | |
on: | |
release: | |
types: [created, edited] | |
jobs: | |
tests: | |
if: startsWith(github.ref, 'refs/tags/v') | |
runs-on: ${{ matrix.os }} | |
strategy: | |
matrix: | |
python-version: ["3.12"] | |
os: [ubuntu-latest] | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up Python ${{ matrix.python-version }} | |
uses: actions/setup-python@v5 | |
with: | |
python-version: ${{ matrix.python-version }} | |
- name: Install tox | |
run: | | |
python -m pip install --upgrade pip | |
pip install tox | |
- name: Run the Tests | |
run: | | |
tox -e tests | |
check_version_tag: | |
name: Check if the version tag is correct | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out Git repository | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Set up Python ${{ matrix.python-version }} | |
uses: actions/setup-python@v5 | |
with: | |
python-version: "3.12" | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install tox | |
pip install -r requirements.txt -r docs/requirements.txt | |
- name: Build JSON Schemas | |
run: tox -e generate_json_schemas | |
env: | |
TARGET_VERSION: ${{ github.ref_name }} | |
- name: Check version tag | |
run: | | |
python -m docs.compatibility.versioning --gh-version ${{ github.ref_name }} \ | |
--gh-token ${{ secrets.GITHUB_TOKEN }} --major-bump-disallowed | |
json_schemas: | |
name: Generate JSON-Schemas | |
runs-on: ubuntu-latest | |
needs: [tests, check_version_tag] | |
concurrency: build-n-publish-json-schemas | |
steps: | |
- name: Check out Git repository | |
uses: actions/checkout@v4 | |
- name: Set up Python ${{ matrix.python-version }} | |
uses: actions/setup-python@v5 | |
with: | |
python-version: "3.12" | |
- name: Install tox | |
run: | | |
python -m pip install --upgrade pip | |
pip install tox | |
- name: Build JSON Schemas | |
run: tox -e generate_json_schemas | |
env: | |
TARGET_VERSION: ${{ github.ref_name }} | |
- name: Push schemas to BO4E-Schemas repo | |
uses: cpina/github-action-push-to-another-repository@v1.7.2 | |
env: | |
API_TOKEN_GITHUB: ${{ secrets.BO4E_PYTHON_GENERATE_SCHEMAS }} # this token expires on 2024-10-09 | |
# token with repo scope | |
# https://github.com/Hochfrequenz/BO4E-python/settings/secrets/actions/BO4E_PYTHON_GENERATE_SCHEMAS | |
with: | |
source-directory: "json_schemas" | |
target-directory: "src/bo4e_schemas" | |
destination-github-username: "Hochfrequenz" | |
destination-repository-name: "BO4E-Schemas" | |
user-email: bo4e-schemas@hochfrequenz.de | |
target-branch: main | |
commit-message: "Update JSON-Schemas - BO4E ${{ github.ref_name }}" | |
# This step uses the github API to automatically create a release with the same version tag | |
# in the BO4E-Schemas repo. For this we have to retrieve the commits SHA via the API first. | |
# For this, we use the lightweight tool jq (https://stedolan.github.io/jq/) to parse the JSON | |
# response from the API. | |
- name: Install dependencies for creating release | |
run: | | |
pip install requests click | |
- name: Tag and release the new version in BO4E-Schemas | |
run: python bo4e_schemas_create_release.py | |
env: | |
GITHUB_TOKEN: ${{ secrets.BO4E_PYTHON_GENERATE_SCHEMAS }} # this token expires on 2024-10-09 | |
VERSION: ${{ github.ref_name }} | |
docs: | |
name: ππ Build and deploy documentation to GitHub Pages | |
# This setup is inspired by | |
# https://github.com/KernelTuner/kernel_tuner/blob/master/.github/workflows/docs-on-release.yml | |
runs-on: ubuntu-latest | |
needs: [tests, check_version_tag] | |
concurrency: build-n-publish-docs | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 # otherwise, you will fail to push refs to dest repo | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: "3.12" | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install tox | |
pip install -r requirements.txt | |
# Note: The sphinx action below can only install a single requirements file. | |
- name: Build JSON Schemas | |
run: tox -e generate_json_schemas | |
env: | |
TARGET_VERSION: ${{ github.ref_name }} | |
- name: Build BO4E package | |
# Note: This step necessary to correctly set the version in the JSON-Schema-links | |
run: | | |
pip install -e . | |
python -c "import bo4e; print(bo4e.__gh_version__)" | |
- name: Run kroki with docker | |
run: | | |
docker compose up -d | |
- name: Build the documentation | |
uses: sphinx-notes/pages@v2 | |
# Note: This action has a newer version (v3 atm), but it doesn't has the feature to specify the target path. | |
# We need that in order to be able to store (and deploy) multiple versions of the documentation. | |
with: | |
requirements_path: docs/requirements.txt | |
documentation_path: docs/ | |
target_path: ${{ github.ref_name }} | |
target_branch: gh-pages | |
sphinx_options: -W -j auto | |
env: | |
SPHINX_DOCS_RELEASE: ${{ github.ref_name }} | |
SPHINX_DOCS_VERSION: ${{ github.ref_name }} | |
- name: Export LATEST_TAG | |
run: | | |
echo "LATEST_TAG=$(curl -qsSL \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
"${{ github.api_url }}/repos/${{ github.repository }}/releases/latest" \ | |
| jq -r .tag_name)" >> $GITHUB_ENV | |
- name: Redirect stable to new release if the new release is marked as latest | |
if: ${{ env.LATEST_TAG == github.ref_name }} | |
run: | | |
echo "Redirecting stable to new version ${{ github.ref_name }}" | |
rm -rf stable | |
ln -s ${{ github.ref_name }} stable | |
git add stable | |
git commit -m "Redirect stable to new version ${{ github.ref_name }}" | |
- name: Push changes | |
uses: ad-m/github-push-action@master | |
with: | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
branch: gh-pages | |
build-n-publish: | |
name: Build and publish Python π distributions π¦ to PyPI and TestPyPI | |
runs-on: ubuntu-latest | |
# Specifying a GitHub environment, which is strongly recommended by PyPI: https://docs.pypi.org/trusted-publishers/adding-a-publisher/ | |
environment: release | |
permissions: | |
# IMPORTANT: this permission is mandatory for trusted publishing | |
id-token: write | |
needs: [tests, json_schemas, docs] | |
concurrency: build-n-publish-distributions | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: "3.10.x" | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install -r dev_requirements/requirements-packaging.txt | |
- name: Build wheel and source distributions | |
run: python -m build | |
- name: Publish distribution π¦ to PyPI | |
if: startsWith(github.ref, 'refs/tags/v') | |
uses: pypa/gh-action-pypi-publish@release/v1 |