summaryrefslogtreecommitdiff
path: root/.github/workflows/commit-bot.yml
blob: 12987021a143ffa6865a057e4861fc5e24b2f965 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
name: Commit Bot

# Instructions
#
# - One-time setup: create a personal access token with permissions. Then configure it here
#   as secrets.CI_PAT. https://github.com/boostorg/boost/settings/secrets/actions
#   The reason is explained in https://github.com/orgs/community/discussions/25702
#   "If an action pushes code using the repository's GITHUB_TOKEN, a new workflow will not run"
#
# - Processing of either the 'master' or 'develop' branch may be stopped by creating the variables
#   vars.block_master or vars.block_develop with the value "yes" (case-insensitive).
#   https://github.com/boostorg/boost/settings/variables/actions
#

# To avoid infinite loops, don't trigger on "push"
on:
  schedule:
    - cron: "0,30 * * * *"

concurrency:
  group: ${{format('commit-bot-{0}:{1}', github.repository, github.ref)}}
  cancel-in-progress: true

jobs:
  update-modules:
    runs-on: ubuntu-latest
    name: Commit Bot
    if: github.repository == 'boostorg/boost'

    steps:
      - name: Check for module updates
        id: branches
        run: |
            set -xe
            branches=""
            block_master="${{ vars.block_master }}"
            block_develop="${{ vars.block_develop }}"
            if [[ "${{ github.event_name }}" == "push" ]]; then
                # The job doesn't run on "push" so this will not execute.
                if [[ ! ${block_master,,} =~ yes && "${{ github.ref_name }}" == "master" ]]; then
                    branches="master"
                elif [[ ! ${block_develop,,} =~ yes && "${{ github.ref_name }}" == "develop" ]]; then
                    branches="develop"
                fi
            else
                # from a schedule:
                if [[ ! ${block_master,,} =~ yes ]]; then
                    branches="master"
                fi
                if [[ ! ${block_develop,,} =~ yes ]]; then
                    branches="${branches} develop"
                fi
            fi
            echo "branches=$branches" >> $GITHUB_OUTPUT
            echo "branches is ${branches}"

      - name: Checkout master repository
        uses: actions/checkout@v7
        if: contains(steps.branches.outputs.branches, 'master')
        with:
          ref: master
          path: master
          persist-credentials: false

      - name: Checkout develop repository
        uses: actions/checkout@v7
        if: contains(steps.branches.outputs.branches, 'develop')
        with:
          ref: develop
          path: develop
          persist-credentials: false

      - name: Check for module updates
        run: |
          branches="${{ steps.branches.outputs.branches }}"

          # Set up Git
          git config --global user.name "boost-commitbot"
          git config --global user.email "boost-commitbot@example.com"

          # Update each branch
          for branch in $branches; do
            cd $branch
            module_paths=$(git config --file .gitmodules --get-regexp '^submodule\..*\.path$')
            while IFS=' ' read -r key path; do
              submodule_name=$(echo "$key" | awk -F '.' '{print $2}')
              submodule_path=$(echo "$path")
              url=$(git config --file .gitmodules --get-regexp "^submodule\.$submodule_name\.url$" | awk '{print $2}')
              if [[ ! "$url" =~ ^https:// ]]; then
                  basicreponame=$(basename $url)
                  url=${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY_OWNER}/${basicreponame}
              fi
              hash=$(git ls-remote "$url" "refs/heads/$branch" | cut -f 1)
              hash="${hash#"${hash%%[![:space:]]*}"}"
              hash="${hash%"${hash##*[![:space:]]}"}"
              commit_id="${hash:0:8}"
              previous_hash=$(git ls-tree HEAD "$submodule_path" | awk '{print $3}')
              previous_hash="${previous_hash#"${previous_hash%%[![:space:]]*}"}"
              previous_hash="${previous_hash%"${previous_hash##*[![:space:]]}"}"
              previous_commit_id="${previous_hash:0:8}"
              if [ "$hash" == "$previous_hash" ]; then
                  echo "$submodule_name ($commit_id): OK"
              else
                  echo "$submodule_name: $previous_commit_id -> $commit_id"
                  set -x
                  set +e
                  git submodule update --init "$submodule_path"
                  git submodule update --remote "$submodule_path"
                  git add "$submodule_path"
                  git commit -m "Update $submodule_name from $branch"
                  set -e
                  set +x
              fi
            done <<< "$module_paths"
            cd ..
          done

      - name: Push changes from master
        uses: ad-m/github-push-action@v1
        if: contains(steps.branches.outputs.branches, 'master')
        with:
          # github_token: ${{ secrets.GITHUB_TOKEN }}
          github_token: ${{ secrets.CI_PAT }}
          branch: master
          directory: master

      - name: Push changes from develop
        uses: ad-m/github-push-action@v1
        if: contains(steps.branches.outputs.branches, 'develop')
        with:
          # github_token: ${{ secrets.GITHUB_TOKEN }}
          github_token: ${{ secrets.CI_PAT }}
          branch: develop
          directory: develop