Skip to content

Publish to PyPI

Publish to PyPI #10

Workflow file for this run

#schema: https://github.com/softprops/github-actions-schemas/blob/master/workflow.json
name: "Publish to PyPI"
on:
release:
types:
- published
workflow_dispatch:
inputs:
tag_name:
description: 'Release tag'
required: true
repository:
description: "The PyPI repository."
required: true
options:
- pypi
- testpypi
skip_version_check:
description: "Skip checks that pixelator version matches tag"
default: false
# Needed for PyPI OIDC permissions
permissions:
id-token: write
actions: write
contents: read
jobs:
pre_job:
name: Collect metadata
runs-on: ubuntu-latest
outputs:
tag: ${{ steps.get_version_tag.outputs.tag }}
pypi_repo_url: ${{ steps.get_version_tag.outputs.pypi_repo_url }}
steps:
- name: Get version tag & repo url
id: get_version_tag
uses: actions/github-script@v6
with:
script: |
const inputs = ${{ toJSON(inputs) }}
const gh_event = ${{ toJSON(github.event) }}
let tag = null
let pypi_repo_url = null
if (inputs.repository == "testpypi") {
pypi_repo_url = "https://test.pypi.org/legacy/"
} else {
pypi_repo_url = "https://upload.pypi.org/legacy/"
}
if (inputs.tag_name != null) {
tag = inputs.tag_name
} else if (gh_event.release != null) {
tag = gh_event.release.tag_name
} else {
core.setFailed('Cannot determine release tag!')
}
core.setOutput('tag', tag)
core.setOutput('pypi_repo_url', pypi_repo_url)
build:
name: Build
runs-on: ubuntu-latest
needs: [ pre_job ]
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 10
fetch-tags: true
- name: Set up Python environment
uses: actions/setup-python@v4
id: setup-python
with:
python-version: '3.10'
#----------------------------------------------
# install & configure poetry
#----------------------------------------------
- name: Install Poetry
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
- name: Install poetry plugins
run: |
poetry self add "poetry-dynamic-versioning[plugin]"
#----------------------------------------------
# load cached venv if cache exists
#----------------------------------------------
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v3
with:
path: .venv
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
#----------------------------------------------
# install dev dependencies if cache does not exist
#----------------------------------------------
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --only dev
#----------------------------------------------
# Build package
#----------------------------------------------
- name: Build package
run: poetry build
#----------------------------------------------
# Verify build version
#----------------------------------------------
- name: Install package
run: |
pip install --no-input dist/*.whl
- name: Verify build version
if: ${{ inputs.skip_version_check != 'true' }}
run: |
python -c "import pixelator; assert 'v' + str(pixelator.__version__) == '${{ needs.pre_job.outputs.tag }}', 'Version mismatch: pixelator = v%s vs tag = %s' % (pixelator.__version__, '${{ needs.pre_job.outputs.tag }}')"
VERSION="$( pixelator --version | sed 's/pixelator, version //g' )"
TAG='${{ needs.pre_job.outputs.tag }}'
if [[ ! v$VERSION =~ $TAG ]]; then
echo "Version mismatch: pixelator = v$VERSION vs tag = $TAG";
exit 1;
fi
#----------------------------------------------
# archive python package build output
#----------------------------------------------
- name: Archive production artifacts
uses: actions/upload-artifact@v3
with:
name: dist-${{ needs.pre_job.outputs.tag }}
path: |
dist
pypi-publish:
name: Upload to PyPI
runs-on: ubuntu-latest
needs: [pre_job, build]
environment: release
steps:
- name: Set up Python environment
uses: actions/setup-python@v4
id: setup-python
with:
python-version: '3.10'
- name: Download build artifacts
uses: actions/download-artifact@v3
with:
name: dist-${{ needs.pre_job.outputs.tag }}
path: dist
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: ${{ needs.pre_job.outputs.pypi_repo_url }}
verbose: true
print-hash: true
skip-existing: true