Skip to content

ci: updated workflow #7

ci: updated workflow

ci: updated workflow #7

name: Publish Python Package
on:
push:
branches: [ main, test, develop ]
pull_request:
types:
- opened
env:
PACKAGE_NAME: crawlab-sdk
PYPI_REPOSITORY_URL: https://test.pypi.org/legacy/
PYPI_TEST_REPOSITORY_URL: https://test.pypi.org/legacy/
jobs:
build_and_publish:
name: Build and publish to PyPI
runs-on: ubuntu-latest
environment: pypi
permissions:
id-token: write
outputs:
is_new_version: ${{ steps.check_version.outputs.is_new_version }}
repository_url: ${{ steps.configure.outputs.repository_url }}
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: '3.12'
- name: Configure settings
id: configure
run: |
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
echo "repository_url=${{ env.PYPI_REPOSITORY_URL }}" >> $GITHUB_OUTPUT
else
echo "repository_url=${{ env.PYPI_TEST_REPOSITORY_URL }}" >> $GITHUB_OUTPUT
fi
- name: Install dependencies
id: install
run: |
python -m pip install --upgrade pip
pip install poetry
poetry install
- name: Build package
id: build
run: |
poetry build
- name: Check package version
id: check_version
run: |
version=$(poetry version -s)
status_code=$(curl -o /dev/null -s -w "%{http_code}" https://pypi.org/project/${{ env.PACKAGE_NAME }}/${version}/)
if [ "$status_code" = "403" ]; then
echo "is_new_version=true" >> $GITHUB_OUTPUT
else
echo "is_new_version=false" >> $GITHUB_OUTPUT
fi
echo "is_new_version=${is_new_version}"
- name: Publish package
id: publish
if: ${{ always() && steps.check_version.outputs.is_new_version == 'true' }}
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: ${{ steps.configure.outputs.repository_url }}