Skip to content

ci: updated workflow #11

ci: updated workflow

ci: updated workflow #11

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:
name: Build package
runs-on: ubuntu-latest
outputs:
version: ${{ steps.build.outputs.version }}
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.12'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install poetry
poetry install
- name: Build package
id: build
run: |
poetry build
echo "version=$(poetry version -s)" >> $GITHUB_OUTPUT
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: dist
path: dist/
check_version:
name: Check version
needs: build
runs-on: ubuntu-latest
outputs:
is_new_version: ${{ steps.check.outputs.is_new_version }}
steps:
- name: Check package version
id: check
run: |
version="${{ needs.build.outputs.version }}"
status_code=$(curl -o /dev/null -s -w "%{http_code}" https://pypi.org/project/${{ env.PACKAGE_NAME }}/${version}/)
echo "status_code=${status_code}"
if [ "$status_code" = "200" ]; then
is_new_version=false
else
is_new_version=true
fi
echo "is_new_version=${is_new_version}" >> $GITHUB_OUTPUT
publish:
name: Publish to PyPI
needs: [build, check_version]
runs-on: ubuntu-latest
environment: pypi
permissions:
id-token: write
if: needs.check_version.outputs.is_new_version == 'true'
steps:
- name: Configure repository URL
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: Download artifact
uses: actions/download-artifact@v4
with:
name: dist
path: dist/
- name: Publish package
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: ${{ steps.configure.outputs.repository_url }}