changeset-release.yml 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. name: Changeset Release
  2. run-name: Changeset Release ${{ github.actor != 'kilocode-bot' && '- 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. NODE_VERSION: 20.19.2
  11. PNPM_VERSION: 10.8.1
  12. jobs:
  13. # Job 1: Create version bump PR when changesets are merged to main
  14. changeset-pr-version-bump:
  15. if: >
  16. ( github.event_name == 'pull_request' &&
  17. github.event.pull_request.merged == true &&
  18. github.event.pull_request.base.ref == 'main' &&
  19. !contains(github.event.pull_request.title, 'Changeset version bump') &&
  20. github.actor != 'kilocode-bot' ) ||
  21. github.event_name == 'workflow_dispatch'
  22. runs-on: ubuntu-latest
  23. permissions:
  24. contents: write
  25. pull-requests: write
  26. steps:
  27. - name: Git Checkout
  28. uses: actions/checkout@v4
  29. with:
  30. fetch-depth: 0
  31. ref: ${{ env.GIT_REF }}
  32. - name: Install pnpm
  33. uses: pnpm/action-setup@v4
  34. with:
  35. version: ${{ env.PNPM_VERSION }}
  36. - name: Setup Node.js
  37. uses: actions/setup-node@v4
  38. with:
  39. node-version: ${{ env.NODE_VERSION }}
  40. cache: "pnpm"
  41. - name: Install Dependencies
  42. run: pnpm install
  43. # Check if there are any new changesets to process
  44. - name: Check for changesets
  45. id: check-changesets
  46. run: |
  47. NEW_CHANGESETS=$(find .changeset -name "*.md" ! -name "README.md" | wc -l | tr -d ' ')
  48. echo "Changesets diff with previous version: $NEW_CHANGESETS"
  49. echo "new_changesets=$NEW_CHANGESETS" >> $GITHUB_OUTPUT
  50. # Create version bump PR using changesets/action if there are new changesets
  51. - name: Changeset Pull Request
  52. if: steps.check-changesets.outputs.new_changesets != '0'
  53. id: changesets
  54. uses: changesets/action@v1
  55. with:
  56. commit: "changeset version bump"
  57. title: "Changeset version bump"
  58. version: pnpm changeset:version # This performs the changeset version bump
  59. env:
  60. GITHUB_TOKEN: ${{ secrets.CROSS_REPO_ACCESS_TOKEN }}
  61. # Remove changelog-ready label from changeset PR if present
  62. - name: Remove changelog-ready label from changeset PR
  63. if: steps.changesets.outputs.pullRequestNumber
  64. uses: actions/github-script@v7
  65. with:
  66. github-token: ${{ secrets.CROSS_REPO_ACCESS_TOKEN }}
  67. script: |
  68. try {
  69. await github.rest.issues.removeLabel({
  70. owner: context.repo.owner,
  71. repo: context.repo.repo,
  72. issue_number: ${{ steps.changesets.outputs.pullRequestNumber }},
  73. name: 'changelog-ready'
  74. });
  75. console.log('Removed changelog-ready label from changeset PR');
  76. } catch (error) {
  77. if (error.status === 404) {
  78. console.log('changelog-ready label was not present on changeset PR');
  79. } else {
  80. throw error;
  81. }
  82. }
  83. # Get current and previous versions to edit changelog entry
  84. - name: Get version
  85. id: get_version
  86. run: |
  87. VERSION=$(git show HEAD:src/package.json | jq -r '.version')
  88. echo "version=$VERSION" >> $GITHUB_OUTPUT
  89. PREV_VERSION=$(git show origin/main:src/package.json | jq -r '.version')
  90. echo "prev_version=$PREV_VERSION" >> $GITHUB_OUTPUT
  91. echo "version=$VERSION"
  92. echo "prev_version=$PREV_VERSION"
  93. # Update CHANGELOG.md with proper format
  94. - name: Update Changelog Format
  95. if: ${{ !contains(github.event.pull_request.labels.*.name, 'changelog-ready') }}
  96. env:
  97. VERSION: ${{ steps.get_version.outputs.version }}
  98. PREV_VERSION: ${{ steps.get_version.outputs.prev_version }}
  99. run: |
  100. echo "Running changelog formatting script..."
  101. echo "VERSION: $VERSION"
  102. echo "PREV_VERSION: $PREV_VERSION"
  103. echo "Current changelog start:"
  104. head -20 CHANGELOG.md
  105. echo "===================="
  106. python .github/scripts/overwrite_changeset_changelog.py
  107. echo "===================="
  108. echo "Updated changelog start:"
  109. head -20 CHANGELOG.md
  110. # Commit and push changelog updates
  111. - name: Push Changelog updates
  112. if: ${{ !contains(github.event.pull_request.labels.*.name, 'changelog-ready') }}
  113. run: |
  114. git config user.name "kilocode-bot"
  115. git config user.email [email protected]
  116. echo "Running git add and commit..."
  117. git add CHANGELOG.md
  118. git commit -m "Updating CHANGELOG.md format"
  119. git status
  120. echo "--------------------------------------------------------------------------------"
  121. echo "Pushing to remote..."
  122. echo "--------------------------------------------------------------------------------"
  123. git push --set-upstream origin HEAD
  124. # Add label to indicate changelog has been formatted
  125. - name: Add changelog-ready label
  126. if: ${{ steps.changesets.outputs.pullRequestNumber && !contains(github.event.pull_request.labels.*.name, 'changelog-ready') }}
  127. uses: actions/github-script@v7
  128. with:
  129. github-token: ${{ secrets.CROSS_REPO_ACCESS_TOKEN }}
  130. script: |
  131. await github.rest.issues.addLabels({
  132. owner: context.repo.owner,
  133. repo: context.repo.repo,
  134. issue_number: ${{ steps.changesets.outputs.pullRequestNumber }},
  135. labels: ['changelog-ready']
  136. });