|
| 1 | +name: LangChain CD - Publish to PyPI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + paths: |
| 7 | + - ".github/workflows/langchain-publish.yml" |
| 8 | + - "langchain/**" |
| 9 | + |
| 10 | +jobs: |
| 11 | + build: |
| 12 | + name: Build distribution |
| 13 | + runs-on: ubuntu-latest |
| 14 | + permissions: |
| 15 | + contents: read |
| 16 | + outputs: |
| 17 | + version-exists: ${{ steps.version.outputs.exists }} |
| 18 | + package-version: ${{ steps.version.outputs.version }} |
| 19 | + steps: |
| 20 | + - name: Checkout repository |
| 21 | + uses: actions/checkout@v6 |
| 22 | + |
| 23 | + - name: Set up Python |
| 24 | + uses: actions/setup-python@v6 |
| 25 | + with: |
| 26 | + python-version: "3.12" |
| 27 | + |
| 28 | + - name: Install uv |
| 29 | + uses: astral-sh/setup-uv@v8.1.0 |
| 30 | + |
| 31 | + - name: Check PyPI version availability |
| 32 | + id: version |
| 33 | + working-directory: ./langchain |
| 34 | + run: | |
| 35 | + PACKAGE=$(python -c "import tomllib; f=open('pyproject.toml','rb'); d=tomllib.load(f); print(d['project']['name'])") |
| 36 | + VERSION=$(python -c "import tomllib; f=open('pyproject.toml','rb'); d=tomllib.load(f); print(d['project']['version'])") |
| 37 | + echo "package=${PACKAGE}" >> "$GITHUB_OUTPUT" |
| 38 | + echo "version=${VERSION}" >> "$GITHUB_OUTPUT" |
| 39 | + RESULT=$(python - "$PACKAGE" "$VERSION" <<'PY' |
| 40 | + import sys |
| 41 | + import urllib.error |
| 42 | + import urllib.parse |
| 43 | + import urllib.request |
| 44 | +
|
| 45 | + package = sys.argv[1] |
| 46 | + version = sys.argv[2] |
| 47 | + url = f"https://pypi.org/pypi/{urllib.parse.quote(package)}/{version}/json" |
| 48 | + try: |
| 49 | + with urllib.request.urlopen(url, timeout=10): |
| 50 | + pass |
| 51 | + except urllib.error.HTTPError as exc: |
| 52 | + if exc.code == 404: |
| 53 | + print("missing") |
| 54 | + sys.exit(0) |
| 55 | + print(f"PyPI returned HTTP {exc.code} while checking {url}", file=sys.stderr) |
| 56 | + sys.exit(2) |
| 57 | + except Exception as exc: |
| 58 | + print(f"Failed to check PyPI for {url}: {exc}", file=sys.stderr) |
| 59 | + sys.exit(2) |
| 60 | + print("exists") |
| 61 | + PY |
| 62 | + ) |
| 63 | + case "$RESULT" in |
| 64 | + exists) |
| 65 | + echo "exists=true" >> "$GITHUB_OUTPUT" |
| 66 | + echo "${PACKAGE} ${VERSION} already exists on PyPI; skipping publish." |
| 67 | + ;; |
| 68 | + missing) |
| 69 | + echo "exists=false" >> "$GITHUB_OUTPUT" |
| 70 | + echo "${PACKAGE} ${VERSION} is available on PyPI; building distribution." |
| 71 | + ;; |
| 72 | + *) |
| 73 | + echo "Unexpected PyPI availability result: $RESULT" |
| 74 | + exit 1 |
| 75 | + ;; |
| 76 | + esac |
| 77 | +
|
| 78 | + - name: Build package |
| 79 | + if: steps.version.outputs.exists == 'false' |
| 80 | + working-directory: ./langchain |
| 81 | + run: uv build |
| 82 | + |
| 83 | + - name: Upload distribution artifact |
| 84 | + if: steps.version.outputs.exists == 'false' |
| 85 | + uses: actions/upload-artifact@v7 |
| 86 | + with: |
| 87 | + name: python-package-distributions |
| 88 | + path: langchain/dist/ |
| 89 | + |
| 90 | + publish-pypi: |
| 91 | + name: Publish to PyPI |
| 92 | + needs: build |
| 93 | + if: needs.build.outputs.version-exists == 'false' |
| 94 | + runs-on: ubuntu-latest |
| 95 | + steps: |
| 96 | + - name: Download distributions |
| 97 | + uses: actions/download-artifact@v8 |
| 98 | + with: |
| 99 | + name: python-package-distributions |
| 100 | + path: dist/ |
| 101 | + |
| 102 | + - name: Publish to PyPI |
| 103 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 104 | + with: |
| 105 | + packages-dir: dist/ |
| 106 | + password: ${{ secrets.PYPI_API_TOKEN }} |
0 commit comments