commit-bot.yml 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 any value.
  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. if [[ "${{ github.event_name }}" == "push" ]]; then
  32. if [[ ! -n "${{ vars.block_master }}" && "${{ github.ref_name }}" == "master" ]]; then
  33. branches="master"
  34. elif [[ ! -n "${{ vars.block_develop }}" && "${{ github.ref_name }}" == "develop" ]]; then
  35. branches="develop"
  36. else
  37. branches="${{ github.ref_name }}"
  38. fi
  39. else
  40. # from a schedule:
  41. if [[ ! -n "${{ vars.block_master }}" ]]; then
  42. branches="master"
  43. fi
  44. if [[ ! -n "${{ vars.block_develop }}" ]]; then
  45. branches="${branches} develop"
  46. fi
  47. fi
  48. echo "branches=$branches" >> $GITHUB_OUTPUT
  49. - name: Checkout master repository
  50. uses: actions/checkout@v4
  51. if: contains(steps.branches.outputs.branches, 'master')
  52. with:
  53. ref: master
  54. path: master
  55. persist-credentials: false
  56. - name: Checkout develop repository
  57. uses: actions/checkout@v4
  58. if: contains(steps.branches.outputs.branches, 'develop')
  59. with:
  60. ref: develop
  61. path: develop
  62. persist-credentials: false
  63. - name: Check for module updates
  64. run: |
  65. branches="${{ steps.branches.outputs.branches }}"
  66. # Set up Git
  67. git config --global user.name "boost-commitbot"
  68. git config --global user.email "boost-commitbot@example.com"
  69. # Update each branch
  70. for branch in $branches; do
  71. cd $branch
  72. module_paths=$(git config --file .gitmodules --get-regexp '^submodule\..*\.path$')
  73. while IFS=' ' read -r key path; do
  74. submodule_name=$(echo "$key" | awk -F '.' '{print $2}')
  75. submodule_path=$(echo "$path")
  76. url=$(git config --file .gitmodules --get-regexp "^submodule\.$submodule_name\.url$" | awk '{print $2}')
  77. if [[ ! "$url" =~ ^https:// ]]; then
  78. basicreponame=$(basename $url)
  79. url=${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY_OWNER}/${basicreponame}
  80. fi
  81. hash=$(git ls-remote "$url" "refs/heads/$branch" | cut -f 1)
  82. hash="${hash#"${hash%%[![:space:]]*}"}"
  83. hash="${hash%"${hash##*[![:space:]]}"}"
  84. commit_id="${hash:0:8}"
  85. previous_hash=$(git ls-tree HEAD "$submodule_path" | awk '{print $3}')
  86. previous_hash="${previous_hash#"${previous_hash%%[![:space:]]*}"}"
  87. previous_hash="${previous_hash%"${previous_hash##*[![:space:]]}"}"
  88. previous_commit_id="${previous_hash:0:8}"
  89. if [ "$hash" == "$previous_hash" ]; then
  90. echo "$submodule_name ($commit_id): OK"
  91. else
  92. echo "$submodule_name: $previous_commit_id -> $commit_id"
  93. set -x
  94. set +e
  95. git submodule update --init "$submodule_path"
  96. git submodule update --remote "$submodule_path"
  97. git add "$submodule_path"
  98. git commit -m "Update $submodule_name from $branch"
  99. set -e
  100. set +x
  101. fi
  102. done <<< "$module_paths"
  103. cd ..
  104. done
  105. - name: Push changes from master
  106. uses: ad-m/github-push-action@v0.8.0
  107. if: contains(steps.branches.outputs.branches, 'master')
  108. with:
  109. # github_token: ${{ secrets.GITHUB_TOKEN }}
  110. github_token: ${{ secrets.CI_PAT }}
  111. branch: master
  112. directory: master
  113. - name: Push changes from develop
  114. uses: ad-m/github-push-action@v0.8.0
  115. if: contains(steps.branches.outputs.branches, 'develop')
  116. with:
  117. # github_token: ${{ secrets.GITHUB_TOKEN }}
  118. github_token: ${{ secrets.CI_PAT }}
  119. branch: develop
  120. directory: develop
粤ICP备19079148号