changeset-release.yml 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. name: Changeset Release
  2. run-name: Changeset Release ${{ github.actor != 'R00-B0T' && '- Create PR' || '- Update Changelog' }}
  3. on:
  4. workflow_dispatch:
  5. pull_request:
  6. types: [closed, opened, labeled]
  7. env:
  8. REPO_PATH: ${{ github.repository }}
  9. GIT_REF: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || 'main' }}
  10. jobs:
  11. # Job 1: Create version bump PR when changesets are merged to main
  12. changeset-pr-version-bump:
  13. if: >
  14. ( github.event_name == 'pull_request' &&
  15. github.event.pull_request.merged == true &&
  16. github.event.pull_request.base.ref == 'main' &&
  17. github.actor != 'R00-B0T' ) ||
  18. github.event_name == 'workflow_dispatch'
  19. runs-on: ubuntu-latest
  20. permissions:
  21. contents: write
  22. pull-requests: write
  23. steps:
  24. - name: Git Checkout
  25. uses: actions/checkout@v4
  26. with:
  27. fetch-depth: 0
  28. ref: ${{ env.GIT_REF }}
  29. - name: Setup Node.js
  30. uses: actions/setup-node@v4
  31. with:
  32. node-version: 20
  33. cache: 'npm'
  34. - name: Install Dependencies
  35. run: npm run install:all
  36. # Check if there are any new changesets to process
  37. - name: Check for changesets
  38. id: check-changesets
  39. run: |
  40. NEW_CHANGESETS=$(find .changeset -name "*.md" ! -name "README.md" | wc -l | tr -d ' ')
  41. echo "Changesets diff with previous version: $NEW_CHANGESETS"
  42. echo "new_changesets=$NEW_CHANGESETS" >> $GITHUB_OUTPUT
  43. # Create version bump PR using changesets/action if there are new changesets
  44. - name: Changeset Pull Request
  45. if: steps.check-changesets.outputs.new_changesets != '0'
  46. id: changesets
  47. uses: changesets/action@v1
  48. with:
  49. commit: "changeset version bump"
  50. title: "Changeset version bump"
  51. version: npm run version-packages # This performs the changeset version bump
  52. env:
  53. GITHUB_TOKEN: ${{ secrets.CROSS_REPO_ACCESS_TOKEN }}
  54. # Job 2: Process version bump PR created by R00-B0T
  55. changeset-pr-edit-approve:
  56. name: Auto approve and merge Bump version PRs
  57. runs-on: ubuntu-latest
  58. permissions:
  59. contents: write
  60. pull-requests: write
  61. if: >
  62. github.event_name == 'pull_request' &&
  63. github.event.pull_request.base.ref == 'main' &&
  64. github.actor == 'R00-B0T' &&
  65. contains(github.event.pull_request.title, 'Changeset version bump')
  66. steps:
  67. - name: Determine checkout ref
  68. id: checkout-ref
  69. run: |
  70. echo "Event action: ${{ github.event.action }}"
  71. echo "Actor: ${{ github.actor }}"
  72. echo "Head ref: ${{ github.head_ref }}"
  73. echo "PR SHA: ${{ github.event.pull_request.head.sha }}"
  74. if [[ "${{ github.event.action }}" == "opened" && "${{ github.actor }}" == "R00-B0T" ]]; then
  75. echo "Using branch ref: ${{ github.head_ref }}"
  76. echo "git_ref=${{ github.head_ref }}" >> $GITHUB_OUTPUT
  77. else
  78. echo "Using SHA ref: ${{ github.event.pull_request.head.sha }}"
  79. echo "git_ref=${{ github.event.pull_request.head.sha }}" >> $GITHUB_OUTPUT
  80. fi
  81. - name: Checkout Repo
  82. uses: actions/checkout@v4
  83. with:
  84. token: ${{ secrets.CROSS_REPO_ACCESS_TOKEN }}
  85. fetch-depth: 0
  86. ref: ${{ steps.checkout-ref.outputs.git_ref }}
  87. # Get current and previous versions to edit changelog entry
  88. - name: Get version
  89. id: get_version
  90. run: |
  91. VERSION=$(git show HEAD:package.json | jq -r '.version')
  92. echo "version=$VERSION" >> $GITHUB_OUTPUT
  93. PREV_VERSION=$(git show origin/main:package.json | jq -r '.version')
  94. echo "prev_version=$PREV_VERSION" >> $GITHUB_OUTPUT
  95. echo "version=$VERSION"
  96. echo "prev_version=$PREV_VERSION"
  97. # Update CHANGELOG.md with proper format
  98. - name: Update Changelog Format
  99. if: ${{ !contains(github.event.pull_request.labels.*.name, 'changelog-ready') }}
  100. env:
  101. VERSION: ${{ steps.get_version.outputs.version }}
  102. PREV_VERSION: ${{ steps.get_version.outputs.prev_version }}
  103. run: python .github/scripts/overwrite_changeset_changelog.py
  104. # Commit and push changelog updates
  105. - name: Push Changelog updates
  106. if: ${{ !contains(github.event.pull_request.labels.*.name, 'changelog-ready') }}
  107. run: |
  108. git config user.name "R00-B0T"
  109. git config user.email [email protected]
  110. echo "Running git add and commit..."
  111. git add CHANGELOG.md
  112. git commit -m "Updating CHANGELOG.md format"
  113. git status
  114. echo "--------------------------------------------------------------------------------"
  115. echo "Pushing to remote..."
  116. echo "--------------------------------------------------------------------------------"
  117. git push
  118. # Add label to indicate changelog has been formatted
  119. - name: Add changelog-ready label
  120. if: ${{ !contains(github.event.pull_request.labels.*.name, 'changelog-ready') }}
  121. uses: actions/github-script@v7
  122. with:
  123. github-token: ${{ secrets.GITHUB_TOKEN }}
  124. script: |
  125. await github.rest.issues.addLabels({
  126. owner: context.repo.owner,
  127. repo: context.repo.repo,
  128. issue_number: context.issue.number,
  129. labels: ['changelog-ready']
  130. });
  131. # Auto-approve PR only after it has been labeled
  132. - name: Auto approve PR
  133. if: contains(github.event.pull_request.labels.*.name, 'changelog-ready')
  134. uses: hmarr/auto-approve-action@v4
  135. with:
  136. review-message: "I'm approving since it's a bump version PR"
  137. # Auto-merge PR
  138. - name: Automerge on PR
  139. if: false # Needs enablePullRequestAutoMerge in repo settings to work contains(github.event.pull_request.labels.*.name, 'changelog-ready')
  140. run: gh pr merge --auto --merge ${{ github.event.pull_request.number }}
  141. env:
  142. GH_TOKEN: ${{ secrets.CROSS_REPO_ACCESS_TOKEN }}