sync-release-branch.yaml 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. name: Sync release branch
  2. concurrency:
  3. group: sync-release-branch-${{ github.event_name }}-${{ github.ref }}
  4. cancel-in-progress: true
  5. on:
  6. schedule:
  7. - cron: '0 2 * * *'
  8. workflow_dispatch:
  9. permissions:
  10. contents: read
  11. jobs:
  12. create-pr:
  13. runs-on: ${{ endsWith(github.repository, '/emqx') && 'ubuntu-22.04' || fromJSON('["self-hosted","ephemeral","linux","x64"]') }}
  14. strategy:
  15. fail-fast: false
  16. matrix:
  17. branch:
  18. - release-58
  19. env:
  20. SYNC_BRANCH: ${{ matrix.branch }}
  21. defaults:
  22. run:
  23. shell: bash
  24. permissions:
  25. contents: write
  26. pull-requests: write
  27. steps:
  28. - uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
  29. with:
  30. fetch-depth: 0
  31. - name: merge and create PR
  32. env:
  33. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  34. run: |
  35. set -euxo pipefail
  36. NEW_BRANCH_NAME=sync-${SYNC_BRANCH}-$(date +"%Y%m%d-%H%M%S")
  37. git config --global user.name "${GITHUB_ACTOR}"
  38. git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
  39. git checkout -b ${NEW_BRANCH_NAME}
  40. git merge origin/${SYNC_BRANCH} 2>&1 | tee merge.log
  41. DIFF=$(git rev-list --count master...HEAD)
  42. if [ $DIFF -eq 0 ]; then
  43. echo "No changes in ${SYNC_BRANCH}"
  44. exit 0
  45. fi
  46. git push origin ${NEW_BRANCH_NAME}:${NEW_BRANCH_NAME}
  47. for pr in $(gh pr list --state open --base master --label sync-release-branch --search "Sync ${SYNC_BRANCH} in:title" --repo ${{ github.repository }} --json number --jq '.[] | .number'); do
  48. gh pr close $pr --repo ${{ github.repository }} --delete-branch || true
  49. done
  50. gh pr create --title "Sync ${SYNC_BRANCH}" --body "Sync ${SYNC_BRANCH}" --base master --head ${NEW_BRANCH_NAME} --label sync-release-branch --repo ${{ github.repository }}
  51. - name: Send notification to Slack
  52. if: failure()
  53. env:
  54. SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
  55. run: |
  56. awk '{printf "%s\\n", $0}' merge.log > merge.log.1
  57. cat <<EOF > payload.json
  58. {
  59. "blocks": [
  60. {
  61. "type": "section",
  62. "text": {
  63. "type": "mrkdwn",
  64. "text": "Automatic sync of ${SYNC_BRANCH} branch failed: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
  65. }
  66. },
  67. {
  68. "type": "section",
  69. "text": {
  70. "type": "mrkdwn",
  71. "text": "\`\`\`$(cat merge.log.1)\`\`\`"
  72. }
  73. }
  74. ]
  75. }
  76. EOF
  77. curl -X POST -H 'Content-type: application/json' --data @payload.json "$SLACK_WEBHOOK_URL"