changeset-release.yml 5.9 KB

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