:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(ci): create pull request for CLI releases (#99214)
Frank Hamande3cfb4d35cb9
diff --git a/.github/workflows/release-cli.yml b/.github/workflows/release-cli.ymlindex ee973b6a059b..c2602c6be455 100644--- a/.github/workflows/release-cli.yml+++ b/.github/workflows/release-cli.yml@@ -32,7 +32,7 @@ jobs:- name: Checkout repositoryuses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2with:- ref: master+ ref: ${{ github.sha }}sparse-checkout: cli/.sampo/changesets- name: Check for changesets@@ -114,29 +114,29 @@ jobs:git init -qgit remote add origin "$repo_url"- # Fetch the exact tag and branch instead of using actions/checkout,+ # Fetch the exact tag and triggering commit instead of using actions/checkout,# which would fetch every branch/tag in this large monorepo.git fetch --depth=1 --filter=blob:none --no-tags origin \"refs/tags/$base_tag:refs/tags/$base_tag" \- "refs/heads/master:refs/remotes/origin/master"+ "$GITHUB_SHA:refs/remotes/origin/release"{echo "## posthog-cli release preview"echoecho "- Base: \`$base_tag\`"- echo "- Head: \`master\`"+ echo "- Head: \`$GITHUB_SHA\`"echoecho "### CLI diff"echoecho "\`\`\`text"- git diff --shortstat "$base_tag..origin/master" -- cli cli/.sampo+ git diff --shortstat "$base_tag..origin/release" -- cli cli/.sampoecho "\`\`\`"echoecho "<details>"echo "<summary>Changed CLI files</summary>"echoecho "\`\`\`text"- git diff --stat "$base_tag..origin/master" -- cli cli/.sampo+ git diff --stat "$base_tag..origin/release" -- cli cli/.sampoecho "\`\`\`"echo "</details>"echo@@ -144,7 +144,7 @@ jobs:echo "<summary>Pending CLI changesets</summary>"echoecho "\`\`\`text"- git diff --name-only "$base_tag..origin/master" -- ':(glob)cli/.sampo/changesets/*.md'+ git diff --name-only "$base_tag..origin/release" -- ':(glob)cli/.sampo/changesets/*.md'echo "\`\`\`"echo "</details>"} >> "$GITHUB_STEP_SUMMARY"@@ -173,8 +173,8 @@ jobs:"alertContext": "The release stopped before it asked for approval. The changesets stay on master until another CLI change lands."}- release:- name: Prepare CLI release+ prepare-release:+ name: Prepare CLI release pull requestneeds: [check-changesets, cli-release-preview, notify-approval-needed]runs-on: ubuntu-latesttimeout-minutes: 30@@ -182,10 +182,10 @@ jobs:environment: Release SDKpermissions:contents: write+ pull-requests: writeoutputs:new-version: ${{ steps.prepare-release.outputs.new-version }}- release-tag: ${{ steps.release-metadata.outputs.release-tag }}- release-commit: ${{ steps.commit-release.outputs.commit-hash }}+ release-pr: ${{ steps.release-pr.outputs.url }}steps:- name: Get GitHub App tokenid: app-token@@ -264,10 +264,8 @@ jobs:echo "new-version=$new_version" >> "$GITHUB_OUTPUT"echo "Prepared posthog-cli v$new_version"- # This job commits straight to master, so nothing stands between what Sampo- # writes and master's own markdown gates. Sampo indents a multi-paragraph- # changeset under its changelog bullet and leaves that indent on the blank- # lines between the paragraphs, which the format check rejects.+ # Sampo can leave blank lines indented in multi-paragraph changelog entries.+ # Format before the release pull request is created so its checks see the final change.- name: Install pnpm dependenciesif: steps.prepare-release.outputs.new-version != ''uses: ./.github/actions/pnpm-install@@ -276,8 +274,7 @@ jobs:# Same two passes as `hogli format:markdown`, called directly because hogli# needs a Python venv this job does not set up. The last command re-runs the- # gate, so an unfixable violation stops the release instead of reddening- # master for every open PR.+ # gate, so an unfixable violation stops the release before the pull request opens.- name: Format the changelogif: steps.prepare-release.outputs.new-version != ''run: |@@ -329,33 +326,142 @@ jobs:exit 1fi- - name: Commit release changes+ - name: Reset release branch to master+ if: steps.check-changes.outputs.committed == 'true'+ env:+ GH_TOKEN: ${{ steps.app-token.outputs.token }}+ RELEASE_BRANCH: chore/release-posthog-cli+ run: |+ TARGET_SHA=$(git rev-parse HEAD)+ if gh api "repos/${{ github.repository }}/git/ref/heads/$RELEASE_BRANCH" >/dev/null 2>&1; then+ gh api -X PATCH "repos/${{ github.repository }}/git/refs/heads/$RELEASE_BRANCH" \+ -f sha="$TARGET_SHA" -F force=true+ else+ gh api "repos/${{ github.repository }}/git/refs" \+ -f ref="refs/heads/$RELEASE_BRANCH" -f sha="$TARGET_SHA"+ fi++ - name: Commit release changes to release branchid: commit-releaseif: steps.check-changes.outputs.committed == 'true'uses: planetscale/ghcommit-action@25309d8005ac7c3bcd61d3fe19b69e0fe47dbdde # v0.2.20with:commit_message: 'chore(cli): release v${{ steps.prepare-release.outputs.new-version }}'repo: ${{ github.repository }}- branch: master+ branch: chore/release-posthog-clienv:GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}- - name: Sync checkout to release commit+ - name: Create or update release pull request+ id: release-prif: steps.commit-release.outputs.commit-hash != ''env:- COMMIT_HASH: ${{ steps.commit-release.outputs.commit-hash }}+ GH_TOKEN: ${{ steps.app-token.outputs.token }}+ NEW_VERSION: ${{ steps.prepare-release.outputs.new-version }}+ RELEASE_COMMIT: ${{ steps.commit-release.outputs.commit-hash }}+ RELEASE_BRANCH: chore/release-posthog-cli+ REPOSITORY: ${{ github.repository }}run: |- git fetch origin master- git reset --hard "$COMMIT_HASH"+ body_file="$RUNNER_TEMP/release-pr.md"+ cat > "$body_file" <<EOF+ ## Problem- - name: Set release metadata- id: release-metadata- if: steps.commit-release.outputs.commit-hash != ''+ The CLI release version bump must pass the normal pull request checks before it reaches master.++ ## Changes++ - Prepare posthog-cli v$NEW_VERSION for release.++ ## How did you test this code?++ - The release workflow validates the generated changelog and package changes before it opens this pull request.++ ## Automatic notifications++ - [ ] Publish to changelog?++ ## Docs update++ None.++ ## 🤖 Agent context++ **Autonomy:** Fully autonomous+ EOF++ release_owner=${REPOSITORY%%/*}+ existing_prs=$(gh api --paginate "repos/$REPOSITORY/pulls?state=open&base=master&head=$release_owner:$RELEASE_BRANCH" --jq '.[].number')+ if [ "$(printf '%s\n' "$existing_prs" | sed '/^$/d' | wc -l)" -gt 1 ]; then+ echo "::error::More than one open CLI release pull request exists"+ exit 1+ fi+ existing_pr=$(printf '%s\n' "$existing_prs" | sed -n '1p')+ if [ -n "$existing_pr" ]; then+ existing_pr_data=$(gh api "repos/$REPOSITORY/pulls/$existing_pr")+ existing_head_repo=$(printf '%s' "$existing_pr_data" | jq -r '.head.repo.full_name // empty')+ existing_head_sha=$(printf '%s' "$existing_pr_data" | jq -r '.head.sha')+ if [ "$existing_head_repo" != "$REPOSITORY" ] || [ "$existing_head_sha" != "$RELEASE_COMMIT" ]; then+ echo "::error::The existing CLI release pull request does not contain the generated release commit"+ exit 1+ fi+ gh pr edit "$existing_pr" --title "chore(cli): release v$NEW_VERSION" --body-file "$body_file"+ pr_url=$(gh pr view "$existing_pr" --json url --jq '.url')+ else+ pr_url=$(gh pr create \+ --title "chore(cli): release v$NEW_VERSION" \+ --body-file "$body_file" \+ --base master \+ --head "$RELEASE_BRANCH")+ fi++ echo "url=$pr_url" >> "$GITHUB_OUTPUT"++ - name: Notify Slack - Release pull request created+ if: steps.release-pr.outputs.url != '' && needs.notify-approval-needed.outputs.slack_ts != ''+ uses: posthog/.github/.github/actions/slack-thread-reply@03565438486849ac0d19543de36c248a27d32a2f+ with:+ slack_bot_token: ${{ secrets.SLACK_CLIENT_LIBRARIES_BOT_TOKEN }}+ slack_channel_id: ${{ vars.SLACK_APPROVALS_CLIENT_LIBRARIES_CHANNEL_ID }}+ thread_ts: ${{ needs.notify-approval-needed.outputs.slack_ts }}+ message: 'Release pull request created: ${{ steps.release-pr.outputs.url }}'+ emoji_reaction: 'eyes'++ # A separate release App approves the generated version bump. The PR still waits+ # for its checks and the Trunk merge queue before it can reach master.+ - name: Get release approval token+ id: release-approval-token+ if: steps.release-pr.outputs.url != ''+ uses: actions/create-github-app-token@1b10c78c7865c340bc4f6099eb2f838309f1e8c3 # v3.1.1+ with:+ client-id: ${{ vars.GH_APP_POSTHOG_RELEASER_APP_ID }}+ private-key: ${{ secrets.GH_APP_POSTHOG_RELEASER_PRIVATE_KEY }}++ - name: Approve and request Trunk merge+ if: steps.release-approval-token.outputs.token != ''env:- NEW_VERSION: ${{ steps.prepare-release.outputs.new-version }}+ GH_TOKEN: ${{ steps.release-approval-token.outputs.token }}+ RELEASE_COMMIT: ${{ steps.commit-release.outputs.commit-hash }}+ RELEASE_PR: ${{ steps.release-pr.outputs.url }}+ REPOSITORY: ${{ github.repository }}run: |- release_tag="posthog-cli/v$NEW_VERSION"- echo "release-tag=$release_tag" >> "$GITHUB_OUTPUT"+ # The automated approval must cover the generated commit only. Another head means+ # something else pushed to the release branch, so stop instead of approving it.+ pr_data=$(gh pr view "$RELEASE_PR" --json number,headRefOid)+ pr_number=$(printf '%s' "$pr_data" | jq -r '.number')+ pr_head=$(printf '%s' "$pr_data" | jq -r '.headRefOid')+ if [ "$pr_head" != "$RELEASE_COMMIT" ]; then+ echo "::error::The release pull request head $pr_head is not the generated release commit $RELEASE_COMMIT"+ exit 1+ fi++ # commit_id binds the approval to that commit, so a later push leaves an+ # outdated review rather than an approval of the new head.+ gh api "repos/$REPOSITORY/pulls/$pr_number/reviews" \+ -f event=APPROVE \+ -f commit_id="$RELEASE_COMMIT" \+ -f body='Automated approval for the generated CLI release version bump.' >/dev/null++ gh pr comment "$RELEASE_PR" --body '/trunk merge'- name: Send failure event to PostHogif: ${{ failure() }}@@ -371,12 +477,12 @@ jobs:"ref": "${{ github.ref }}","repository": "${{ github.repository }}","sdk": "posthog-cli",- "jobName": "release",+ "jobName": "prepare-release","packageName": "posthog-cli","packageVersion": "${{ steps.prepare-release.outputs.new-version || 'unknown' }}","actionUrl": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}","alertText": ":rotating_light: Failed to prepare release posthog-cli@v${{ steps.prepare-release.outputs.new-version || 'unknown' }} :rotating_light:",- "alertContext": "The version bump step failed before cargo-dist ran."+ "alertContext": "The version bump pull request could not be prepared."}- name: Notify Slack - Failed@@ -387,10 +493,114 @@ jobs:slack_bot_token: ${{ secrets.SLACK_CLIENT_LIBRARIES_BOT_TOKEN }}slack_channel_id: ${{ vars.SLACK_APPROVALS_CLIENT_LIBRARIES_CHANNEL_ID }}thread_ts: ${{ needs.notify-approval-needed.outputs.slack_ts }}- message: "❌ Failed to prepare `posthog-cli@v${{ steps.prepare-release.outputs.new-version || 'unknown' }}`. <https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}|View workflow run>"+ message: "❌ Failed to prepare the release pull request for `posthog-cli@v${{ steps.prepare-release.outputs.new-version || 'unknown' }}`. <https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}|View workflow run>"emoji_reaction: 'x'- # Keep cargo-dist publishing in this workflow so the Release approval gates the full release path.+ # A merged release pull request deletes the consumed changesets, which starts this+ # workflow again. Only that release commit may continue to publishing.+ release:+ name: Identify merged CLI release+ needs: check-changesets+ if: needs.check-changesets.outputs.has-changesets == 'false'+ runs-on: ubuntu-latest+ timeout-minutes: 5+ permissions:+ contents: read+ pull-requests: read+ outputs:+ new-version: ${{ steps.release-metadata.outputs.new-version }}+ release-tag: ${{ steps.release-metadata.outputs.release-tag }}+ release-commit: ${{ steps.release-metadata.outputs.release-commit }}+ steps:+ - name: Checkout repository+ uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2+ with:+ ref: ${{ github.sha }}+ fetch-depth: 2++ - name: Identify release commit+ id: release-metadata+ env:+ GH_TOKEN: ${{ github.token }}+ RELEASE_APP: releaser-posthog-cli[bot]+ RELEASE_BRANCH: chore/release-posthog-cli+ RELEASE_COMMIT: ${{ github.sha }}+ REPOSITORY: ${{ github.repository }}+ run: |+ release_prs=$(gh api --paginate "repos/$REPOSITORY/commits/$RELEASE_COMMIT/pulls" \+ --jq '.[] | select(.merged_at != null and .base.ref == "master" and .head.ref == "chore/release-posthog-cli") | .number')+ if [ "$(printf '%s\n' "$release_prs" | sed '/^$/d' | wc -l)" -ne 1 ]; then+ echo "::error::Expected one merged CLI release pull request for $RELEASE_COMMIT"+ exit 1+ fi+ release_pr=$(printf '%s\n' "$release_prs" | sed -n '1p')++ pr_data=$(gh api "repos/$REPOSITORY/pulls/$release_pr")+ if [ "$(printf '%s' "$pr_data" | jq -r '.user.login')" != "$RELEASE_APP" ]; then+ echo "::error::The release pull request was not created by $RELEASE_APP"+ exit 1+ fi++ release_head=$(printf '%s' "$pr_data" | jq -r '.head.sha')+ if [ "$(gh api "repos/$REPOSITORY/commits/$release_head" --jq '.author.login // empty')" != "$RELEASE_APP" ]; then+ echo "::error::The release pull request head was not created by $RELEASE_APP"+ exit 1+ fi++ release_files=$(gh api --paginate "repos/$REPOSITORY/pulls/$release_pr/files" --jq '.[].filename')+ unexpected=$(printf '%s\n' "$release_files" | grep -Ev '^(cli/Cargo\.toml|cli/Cargo\.lock|cli/CHANGELOG\.md|cli/\.sampo/changesets/[^/]+\.md)$' || true)+ if [ -n "$unexpected" ]; then+ echo "::error::Unexpected files in the CLI release pull request:"+ echo "$unexpected"+ exit 1+ fi++ removed_changesets=$(gh api --paginate "repos/$REPOSITORY/pulls/$release_pr/files" \+ --jq '.[] | select(.status == "removed" and (.filename | test("^cli/\\.sampo/changesets/[^/]+\\.md$"))) | .filename')+ if [ -z "$removed_changesets" ]; then+ echo "::error::The CLI release pull request did not remove a changeset"+ exit 1+ fi++ if ! printf '%s\n' "$release_files" | grep -qx 'cli/Cargo.lock'; then+ echo "::error::The CLI release pull request did not update cli/Cargo.lock"+ exit 1+ fi++ new_version=$(grep '^version = ' cli/Cargo.toml | head -1 | sed 's/version = "\(.*\)"/\1/')+ release_tag="posthog-cli/v$new_version"+ if git ls-remote --exit-code --tags origin "refs/tags/$release_tag" >/dev/null 2>&1; then+ echo "Release tag $release_tag already exists; skipping publication"+ exit 0+ fi++ echo "new-version=$new_version" >> "$GITHUB_OUTPUT"+ echo "release-tag=$release_tag" >> "$GITHUB_OUTPUT"+ echo "release-commit=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"++ - name: Send failure event to PostHog+ if: ${{ failure() }}+ continue-on-error: true+ uses: PostHog/posthog-github-action@58dea254b598fb5d469c0699c98af8288a7f7650 # v1.2.0+ with:+ posthog-token: '${{ secrets.POSTHOG_PROJECT_API_KEY }}'+ event: 'posthog-sdk-github-release-workflow-failure'+ properties: >-+ {+ "commitSha": "${{ github.sha }}",+ "jobStatus": "${{ job.status }}",+ "ref": "${{ github.ref }}",+ "repository": "${{ github.repository }}",+ "sdk": "posthog-cli",+ "jobName": "identify-merged-release",+ "packageName": "posthog-cli",+ "packageVersion": "unknown",+ "actionUrl": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}",+ "alertText": ":rotating_light: Failed to identify the merged posthog-cli release :rotating_light:",+ "alertContext": "The merged release did not pass the checks required before publishing."+ }++ # Publish only after the reviewed release pull request has merged into master.# Run 'dist plan' (or host) to determine what tasks we need to doplan-release:name: Plan cargo-dist release