commit-bot.yml 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. name: Commit Bot
  2. # Instructions
  3. #
  4. # - One-time setup: create a personal access token with permissions. Then configure it here
  5. # as secrets.CI_PAT. https://github.com/boostorg/boost/settings/secrets/actions
  6. # The reason is explained in https://github.com/orgs/community/discussions/25702
  7. # "If an action pushes code using the repository's GITHUB_TOKEN, a new workflow will not run"
  8. #
  9. # - Processing of either the 'master' or 'develop' branch may be stopped by creating the variables
  10. # vars.block_master or vars.block_develop with the value "yes" (case-insensitive).
  11. # https://github.com/boostorg/boost/settings/variables/actions
  12. #
  13. # To avoid infinite loops, don't trigger on "push"
  14. on:
  15. schedule:
  16. - cron: "0,30 * * * *"
  17. concurrency:
  18. group: ${{format('commit-bot-{0}:{1}', github.repository, github.ref)}}
  19. cancel-in-progress: true
  20. jobs:
  21. update-modules:
  22. runs-on: ubuntu-latest
  23. name: Commit Bot
  24. if: github.repository == 'boostorg/boost'
  25. steps:
  26. - name: Check for module updates
  27. id: branches
  28. run: |
  29. set -xe
  30. branches=""
  31. block_master="${{ vars.block_master }}"
  32. block_develop="${{ vars.block_develop }}"
  33. if [[ "${{ github.event_name }}" == "push" ]]; then
  34. # The job doesn't run on "push" so this will not execute.
  35. if [[ ! ${block_master,,} =~ yes && "${{ github.ref_name }}" == "master" ]]; then
  36. branches="master"
  37. elif [[ ! ${block_develop,,} =~ yes && "${{ github.ref_name }}" == "develop" ]]; then
  38. branches="develop"
  39. fi
  40. else
  41. # from a schedule:
  42. if [[ ! ${block_master,,} =~ yes ]]; then
  43. branches="master"
  44. fi
  45. if [[ ! ${block_develop,,} =~ yes ]]; then
  46. branches="${branches} develop"
  47. fi
  48. fi
  49. echo "branches=$branches" >> $GITHUB_OUTPUT
  50. echo "branches is ${branches}"
  51. - name: Checkout master repository
  52. uses: actions/checkout@v4
  53. if: contains(steps.branches.outputs.branches, 'master')
  54. with:
  55. ref: master
  56. path: master
  57. persist-credentials: false
  58. - name: Checkout develop repository
  59. uses: actions/checkout@v4
  60. if: contains(steps.branches.outputs.branches, 'develop')
  61. with:
  62. ref: develop
  63. path: develop
  64. persist-credentials: false
  65. - name: Check for module updates
  66. run: |
  67. branches="${{ steps.branches.outputs.branches }}"
  68. # Set up Git
  69. git config --global user.name "boost-commitbot"
  70. git config --global user.email "boost-commitbot@example.com"
  71. # Update each branch
  72. for branch in $branches; do
  73. cd $branch
  74. module_paths=$(git config --file .gitmodules --get-regexp '^submodule\..*\.path$')
  75. while IFS=' ' read -r key path; do
  76. submodule_name=$(echo "$key" | awk -F '.' '{print $2}')
  77. submodule_path=$(echo "$path")
  78. url=$(git config --file .gitmodules --get-regexp "^submodule\.$submodule_name\.url$" | awk '{print $2}')
  79. if [[ ! "$url" =~ ^https:// ]]; then
  80. basicreponame=$(basename $url)
  81. url=${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY_OWNER}/${basicreponame}
  82. fi
  83. hash=$(git ls-remote "$url" "refs/heads/$branch" | cut -f 1)
  84. hash="${hash#"${hash%%[![:space:]]*}"}"
  85. hash="${hash%"${hash##*[![:space:]]}"}"
  86. commit_id="${hash:0:8}"
  87. previous_hash=$(git ls-tree HEAD "$submodule_path" | awk '{print $3}')
  88. previous_hash="${previous_hash#"${previous_hash%%[![:space:]]*}"}"
  89. previous_hash="${previous_hash%"${previous_hash##*[![:space:]]}"}"
  90. previous_commit_id="${previous_hash:0:8}"
  91. if [ "$hash" == "$previous_hash" ]; then
  92. echo "$submodule_name ($commit_id): OK"
  93. else
  94. echo "$submodule_name: $previous_commit_id -> $commit_id"
  95. set -x
  96. set +e
  97. git submodule update --init "$submodule_path"
  98. git submodule update --remote "$submodule_path"
  99. git add "$submodule_path"
  100. git commit -m "Update $submodule_name from $branch"
  101. set -e
  102. set +x
  103. fi
  104. done <<< "$module_paths"
  105. cd ..
  106. done
  107. - name: Push changes from master
  108. uses: ad-m/github-push-action@v0.8.0
  109. if: contains(steps.branches.outputs.branches, 'master')
  110. with:
  111. # github_token: ${{ secrets.GITHUB_TOKEN }}
  112. github_token: ${{ secrets.CI_PAT }}
  113. branch: master
  114. directory: master
  115. - name: Push changes from develop
  116. uses: ad-m/github-push-action@v0.8.0
  117. if: contains(steps.branches.outputs.branches, 'develop')
  118. with:
  119. # github_token: ${{ secrets.GITHUB_TOKEN }}
  120. github_token: ${{ secrets.CI_PAT }}
  121. branch: develop
  122. directory: develop
粤ICP备19079148号