diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..aca31d1 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,71 @@ +name: "Publish to PyPI" + +on: + push: + branches: + - main + workflow_dispatch: + +jobs: + publish: + runs-on: ubuntu-latest + if: github.ref == 'refs/heads/main' + environment: release + permissions: + id-token: write # IMPORTANT: this permission is mandatory for trusted publishing + contents: read + + steps: + - name: Checkout repo + submodules + uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Install Python + uses: actions/setup-python@v4 + with: + python-version: "3.10" + + - name: Check if version already published + id: version-check + run: | + VERSION=$(grep '^version = ' pyproject.toml | sed 's/version = "\(.*\)"/\1/') + echo "Current version: $VERSION" + + # Check if this version exists on PyPI + HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "https://pypi.org/pypi/prefab-cloud-python/$VERSION/json") + + if [ "$HTTP_STATUS" = "200" ]; then + echo "Version $VERSION already exists on PyPI" + echo "skip=true" >> $GITHUB_OUTPUT + else + echo "Version $VERSION not found on PyPI, proceeding with publish" + echo "skip=false" >> $GITHUB_OUTPUT + fi + + - name: Install Poetry + if: steps.version-check.outputs.skip == 'false' + uses: snok/install-poetry@v1 + with: + virtualenvs-create: true + + - name: Install dependencies + if: steps.version-check.outputs.skip == 'false' + run: | + poetry install --no-interaction + + - name: Run tests + if: steps.version-check.outputs.skip == 'false' + run: poetry run pytest -k 'not integration' + env: + PREFAB_INTEGRATION_TEST_API_KEY: ${{ secrets.PREFAB_INTEGRATION_TEST_API_KEY }} + + - name: Build package + if: steps.version-check.outputs.skip == 'false' + run: poetry build + + - name: Publish to PyPI + if: steps.version-check.outputs.skip == 'false' + uses: pypa/gh-action-pypi-publish@release/v1 + with: + packages-dir: dist/