|
| 1 | +name: Publish Python package |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - "v*" |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + target: |
| 10 | + description: "Where to publish" |
| 11 | + required: true |
| 12 | + default: "testpypi" |
| 13 | + type: choice |
| 14 | + options: |
| 15 | + - testpypi |
| 16 | + - pypi |
| 17 | + |
| 18 | +# Default least-privilege; publish-* jobs override with id-token: write. |
| 19 | +permissions: |
| 20 | + contents: read |
| 21 | + |
| 22 | +jobs: |
| 23 | + build: |
| 24 | + name: Build distributions |
| 25 | + runs-on: ubuntu-latest |
| 26 | + |
| 27 | + steps: |
| 28 | + - name: Check out source |
| 29 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 |
| 30 | + - name: Set up Python |
| 31 | + uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6 |
| 32 | + with: |
| 33 | + python-version: "3.12" |
| 34 | + |
| 35 | + - name: Install build tool |
| 36 | + run: python -m pip install --upgrade pip build |
| 37 | + |
| 38 | + - name: Build sdist and wheel |
| 39 | + run: python -m build |
| 40 | + |
| 41 | + - name: Upload distribution artifacts |
| 42 | + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 |
| 43 | + with: |
| 44 | + name: python-package-distributions |
| 45 | + path: dist/ |
| 46 | + |
| 47 | + publish-to-testpypi: |
| 48 | + name: Publish to TestPyPI |
| 49 | + needs: build |
| 50 | + runs-on: ubuntu-latest |
| 51 | + if: github.event_name == 'workflow_dispatch' && inputs.target == 'testpypi' |
| 52 | + environment: |
| 53 | + name: testpypi |
| 54 | + url: https://test.pypi.org/p/langchain-runcycles |
| 55 | + permissions: |
| 56 | + id-token: write |
| 57 | + contents: read |
| 58 | + |
| 59 | + steps: |
| 60 | + - name: Download distribution artifacts |
| 61 | + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 |
| 62 | + with: |
| 63 | + name: python-package-distributions |
| 64 | + path: dist/ |
| 65 | + |
| 66 | + - name: Publish to TestPyPI |
| 67 | + uses: pypa/gh-action-pypi-publish@cef221092ed1bacb1cc03d23a2d87d1d172e277b # release/v1 |
| 68 | + with: |
| 69 | + repository-url: https://test.pypi.org/legacy/ |
| 70 | + |
| 71 | + publish-to-pypi: |
| 72 | + name: Publish to PyPI |
| 73 | + needs: build |
| 74 | + runs-on: ubuntu-latest |
| 75 | + if: startsWith(github.ref, 'refs/tags/v') || (github.event_name == 'workflow_dispatch' && inputs.target == 'pypi') |
| 76 | + environment: |
| 77 | + name: pypi |
| 78 | + url: https://pypi.org/p/langchain-runcycles |
| 79 | + permissions: |
| 80 | + id-token: write |
| 81 | + contents: read |
| 82 | + |
| 83 | + steps: |
| 84 | + - name: Download distribution artifacts |
| 85 | + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 |
| 86 | + with: |
| 87 | + name: python-package-distributions |
| 88 | + path: dist/ |
| 89 | + |
| 90 | + - name: Publish to PyPI |
| 91 | + uses: pypa/gh-action-pypi-publish@cef221092ed1bacb1cc03d23a2d87d1d172e277b # release/v1 |
| 92 | + |
| 93 | + create-release: |
| 94 | + name: Create GitHub Release |
| 95 | + needs: publish-to-pypi |
| 96 | + runs-on: ubuntu-latest |
| 97 | + if: startsWith(github.ref, 'refs/tags/v') |
| 98 | + permissions: |
| 99 | + contents: write |
| 100 | + |
| 101 | + steps: |
| 102 | + - name: Check out source |
| 103 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 |
| 104 | + |
| 105 | + - name: Extract release notes from CHANGELOG |
| 106 | + id: notes |
| 107 | + run: | |
| 108 | + version="${GITHUB_REF_NAME#v}" |
| 109 | + # Extract the section between this version's heading and the next "## [" heading. |
| 110 | + # Uses string functions instead of regex to stay portable across awk variants. |
| 111 | + notes=$(awk -v v="$version" ' |
| 112 | + BEGIN { start = "## [" v "]"; flag = 0 } |
| 113 | + substr($0, 1, length(start)) == start { flag = 1; next } |
| 114 | + substr($0, 1, 4) == "## [" { flag = 0 } |
| 115 | + flag { print } |
| 116 | + ' CHANGELOG.md) |
| 117 | + if [ -z "$(printf '%s' "$notes" | tr -d '[:space:]')" ]; then |
| 118 | + echo "::warning::No CHANGELOG entry found for v${version} — using fallback body" |
| 119 | + notes="Release v${version}. See commit history for changes." |
| 120 | + fi |
| 121 | + { |
| 122 | + echo "notes<<EOF_GH_OUTPUT" |
| 123 | + printf '%s\n' "$notes" |
| 124 | + echo "EOF_GH_OUTPUT" |
| 125 | + } >> "$GITHUB_OUTPUT" |
| 126 | +
|
| 127 | + - name: Create GitHub Release |
| 128 | + uses: softprops/action-gh-release@b4309332981a82ec1c5618f44dd2e27cc8bfbfda # v3.0.0 |
| 129 | + with: |
| 130 | + name: ${{ github.ref_name }} |
| 131 | + body: ${{ steps.notes.outputs.notes }} |
| 132 | + draft: false |
| 133 | + prerelease: ${{ contains(github.ref_name, '-') }} |
0 commit comments