|
| 1 | +name: Publish to PyPI (with Approval) |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ["Build and Test Distribution"] |
| 6 | + types: |
| 7 | + - completed |
| 8 | + |
| 9 | +jobs: |
| 10 | + publish-pypi: |
| 11 | + if: github.event.workflow_run.conclusion == 'success' |
| 12 | + runs-on: ubuntu-latest |
| 13 | + environment: |
| 14 | + name: pypi-production |
| 15 | + url: https://pypi.org/project/nemoguardrails/ |
| 16 | + permissions: |
| 17 | + contents: write |
| 18 | + id-token: write |
| 19 | + |
| 20 | + steps: |
| 21 | + - name: Checkout repository for tag detection |
| 22 | + uses: actions/checkout@v4 |
| 23 | + with: |
| 24 | + fetch-depth: 0 |
| 25 | + |
| 26 | + - name: Detect version tag and extract info |
| 27 | + id: version |
| 28 | + run: | |
| 29 | + COMMIT_SHA="${{ github.event.workflow_run.head_sha }}" |
| 30 | +
|
| 31 | + TAG_NAME=$(git tag --points-at "$COMMIT_SHA" | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+' | head -1) |
| 32 | +
|
| 33 | + if [ -z "$TAG_NAME" ]; then |
| 34 | + echo "❌ No version tag found at commit $COMMIT_SHA" |
| 35 | + echo "Available tags at this commit:" |
| 36 | + git tag --points-at "$COMMIT_SHA" || echo " (none)" |
| 37 | + exit 1 |
| 38 | + fi |
| 39 | +
|
| 40 | + VERSION="${TAG_NAME#v}" |
| 41 | + echo "version=${VERSION}" >> $GITHUB_OUTPUT |
| 42 | + echo "tag=${TAG_NAME}" >> $GITHUB_OUTPUT |
| 43 | + echo "artifact_name=${TAG_NAME}-build" >> $GITHUB_OUTPUT |
| 44 | + echo "✅ Detected version tag: $TAG_NAME" |
| 45 | +
|
| 46 | + - name: Checkout repository |
| 47 | + uses: actions/checkout@v4 |
| 48 | + with: |
| 49 | + ref: ${{ steps.version.outputs.tag }} |
| 50 | + |
| 51 | + - name: Validate version matches tag |
| 52 | + run: | |
| 53 | + VERSION_IN_FILE=$(grep '^version = ' pyproject.toml | sed 's/version = "\(.*\)"/\1/') |
| 54 | + TAG_VERSION="${{ steps.version.outputs.version }}" |
| 55 | + if [ "$VERSION_IN_FILE" != "$TAG_VERSION" ]; then |
| 56 | + echo "❌ Version mismatch: pyproject.toml=$VERSION_IN_FILE, tag=$TAG_VERSION" |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | + echo "✅ Version validated: $VERSION_IN_FILE matches tag $TAG_VERSION" |
| 60 | +
|
| 61 | + - name: Download artifact |
| 62 | + uses: actions/download-artifact@v4 |
| 63 | + with: |
| 64 | + name: ${{ steps.version.outputs.artifact_name }} |
| 65 | + path: dist |
| 66 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 67 | + repository: ${{ github.repository }} |
| 68 | + run-id: ${{ github.event.workflow_run.id }} |
| 69 | + |
| 70 | + - name: List files |
| 71 | + run: ls -la dist/ |
| 72 | + |
| 73 | + - name: Publish to PyPI |
| 74 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 75 | + with: |
| 76 | + verbose: true |
| 77 | + packages-dir: dist/ |
| 78 | + attestations: true |
| 79 | + |
| 80 | + - name: Create GitHub Release |
| 81 | + env: |
| 82 | + GH_TOKEN: ${{ github.token }} |
| 83 | + run: | |
| 84 | + TAG_NAME="${{ steps.version.outputs.tag }}" |
| 85 | +
|
| 86 | + git config --global user.name "github-actions[bot]" |
| 87 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 88 | +
|
| 89 | + CHANGELOG_SECTION=$(awk -v version="${{ steps.version.outputs.version }}" ' |
| 90 | + /^## \[/ { |
| 91 | + if (found) exit |
| 92 | + if ($0 ~ "\\[" version "\\]") { |
| 93 | + found=1 |
| 94 | + next |
| 95 | + } |
| 96 | + } |
| 97 | + found && /^## \[/ { exit } |
| 98 | + found { print } |
| 99 | + ' CHANGELOG.md || echo "No changelog entry found for this version.") |
| 100 | +
|
| 101 | + echo "$CHANGELOG_SECTION" > release_notes.md |
| 102 | +
|
| 103 | + if gh release view "$TAG_NAME" --repo ${{ github.repository }} >/dev/null 2>&1; then |
| 104 | + echo "ℹ️ Release $TAG_NAME already exists, skipping creation" |
| 105 | + else |
| 106 | + if gh release create "$TAG_NAME" \ |
| 107 | + --draft \ |
| 108 | + --title "$TAG_NAME" \ |
| 109 | + --notes-file release_notes.md \ |
| 110 | + --repo ${{ github.repository }}; then |
| 111 | + echo "✅ Release $TAG_NAME created successfully" |
| 112 | + else |
| 113 | + echo "❌ Failed to create release $TAG_NAME" >&2 |
| 114 | + rm -f release_notes.md |
| 115 | + exit 1 |
| 116 | + fi |
| 117 | + fi |
| 118 | +
|
| 119 | + rm -f release_notes.md |
0 commit comments