Skip to content

Commit

Permalink
Refactor pypi-publish workflow to use gh-action for publishing
Browse files Browse the repository at this point in the history
This commit refactors the pypi-publish workflow in the Github Actions. The changes made include using `gh-action-pypi-publish` for both PyPI and TestPyPI publication, instead of invoking `poetry publish` directly. This enhances readability and consistency in the workflow file.
  • Loading branch information
mdbecker committed Jun 26, 2023
1 parent 2e2a0ed commit 712aa3e
Showing 1 changed file with 37 additions and 44 deletions.
81 changes: 37 additions & 44 deletions .github/workflows/pypi-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,59 +15,52 @@ jobs:
pypi-publish:
needs: run-test
runs-on: ubuntu-latest
environment: release # Make sure this matches the environment name you set on PyPI
environment: release

permissions:
id-token: write

steps:
- name: Check out repository
uses: actions/checkout@v3

- name: Extract release version
id: release-version
run: echo "::set-output name=version::${{ github.event.release.tag_name }}"
- name: Check out repository
uses: actions/checkout@v3

- name: Update and validate version in pyproject.toml
run: |
# Replacing the placeholder version with the actual version from the tag
sed -i "s/version = \"0.0.0\"/version = \"${{ steps.release-version.outputs.version }}\"/" pyproject.toml
# Validate that the version follows semantic versioning
if ! echo "${{ steps.release-version.outputs.version }}" | grep -Pq '^\d+(\.\d+){2}(-rc\.\d+)?$'; then
echo "Invalid version format. Should be semantic versioning."
exit 1
fi
- name: Extract release version
id: release-version
run: echo "::set-output name=version::${{ github.event.release.tag_name }}"

- name: Install Poetry
uses: snok/install-poetry@v1.2
- name: Update and validate version in pyproject.toml
run: |
sed -i "s/version = \"0.0.0\"/version = \"${{ steps.release-version.outputs.version }}\"/" pyproject.toml
if ! echo "${{ steps.release-version.outputs.version }}" | grep -Pq '^\d+(\.\d+){2}(-rc\.\d+)?$'; then
echo "Invalid version format. Should be semantic versioning."
exit 1
fi
- name: Build Python package
run: poetry build
- name: Install Poetry
uses: snok/install-poetry@v1.2

- name: Authenticate using OIDC
id: authenticate
uses: pypa/gh-action-pypi-publish@release/v1
with:
oidc: true
- name: Build Python package
run: poetry build

- name: Configure and Publish to TestPyPI if release candidate
if: contains(steps.release-version.outputs.version, 'rc')
run: poetry config repositories.testpypi https://test.pypi.org/legacy/ && poetry publish --repository testpypi --username=__token__ --password=${{ steps.authenticate.outputs.pypi_token }}
- name: Publish to TestPyPI if release candidate
if: contains(steps.release-version.outputs.version, 'rc')
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: https://test.pypi.org/legacy/

- name: Publish to PyPI if not release candidate
if: "!contains(steps.release-version.outputs.version, 'rc')"
run: poetry publish --username=__token__ --password=${{ steps.authenticate.outputs.pypi_token }}
- name: Publish to PyPI if not release candidate
if: "!contains(steps.release-version.outputs.version, 'rc')"
uses: pypa/gh-action-pypi-publish@release/v1

- name: Validate package on TestPyPI
if: contains(steps.release-version.outputs.version, 'rc')
run: |
PACKAGE_NAME="gull-api"
PACKAGE_VERSION="${{ steps.release-version.outputs.version }}"
curl --head --fail "https://test.pypi.org/project/${PACKAGE_NAME}/${PACKAGE_VERSION}/"
- name: Validate package on TestPyPI
if: contains(steps.release-version.outputs.version, 'rc')
run: |
PACKAGE_NAME="gull-api"
PACKAGE_VERSION="${{ steps.release-version.outputs.version }}"
curl --head --fail "https://test.pypi.org/project/${PACKAGE_NAME}/${PACKAGE_VERSION}/"
- name: Validate package on PyPI
if: "!contains(steps.release-version.outputs.version, 'rc')"
run: |
PACKAGE_NAME="gull-api"
PACKAGE_VERSION="${{ steps.release-version.outputs.version }}"
curl --head --fail "https://pypi.org/project/${PACKAGE_NAME}/${PACKAGE_VERSION}/"
- name: Validate package on PyPI
if: "!contains(steps.release-version.outputs.version, 'rc')"
run: |
PACKAGE_NAME="gull-api"
PACKAGE_VERSION="${{ steps.release-version.outputs.version }}"
curl --head --fail "https://pypi.org/project/${PACKAGE_NAME}/${PACKAGE_VERSION}/"

0 comments on commit 712aa3e

Please sign in to comment.