Update submodules #306
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Update submodules | |
on: | |
# Runs every day at midnight UTC | |
schedule: | |
- cron: '0 0 * * *' | |
# Allows you to run this workflow manually from the Actions tab | |
workflow_dispatch: | |
jobs: | |
update: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
pull-requests: write | |
env: | |
# The branch that will receive the submodule updates (will be created if it doesn't exist) | |
# Will automatically open a new PR (if no PR exists yet) | |
UPDATE_BRANCH: update-submodules | |
steps: | |
- name: Create app token | |
uses: actions/create-github-app-token@v1 | |
id: app-token | |
with: | |
app-id: ${{ vars.THEOPLAYER_BOT_APP_ID }} | |
private-key: ${{ secrets.THEOPLAYER_BOT_PRIVATE_KEY }} | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
token: ${{ steps.app-token.outputs.token }} | |
fetch-depth: 1 | |
- name: Configure Git user | |
run: | | |
git config user.name 'theoplayer-bot[bot]' | |
git config user.email '873105+theoplayer-bot[bot]@users.noreply.github.com' | |
- name: Create branch | |
run: git checkout -b $UPDATE_BRANCH | |
- name: Update submodules with upstream | |
# `git submodule update --remote` doesn't seem to work very well with non-main branches... | |
# Fetch and checkout the latest revision of each submodule manually. | |
run: | | |
git submodule update --init --single-branch | |
git submodule foreach 'git fetch --depth=1 origin $(git config -f $toplevel/.gitmodules submodule.$name.branch) && git checkout --quiet FETCH_HEAD' | |
- name: Commit changes | |
run: git commit --allow-empty -a -m 'Update submodules' | |
- name: Check for relevant doc changes | |
id: check_doc_changes | |
shell: bash | |
# https://stackoverflow.com/q/67724347/7976758 | |
# We only care about changes to CHANGELOG.md or inside /doc/ or /docs/ | |
run: | | |
compare () { | |
if [ -z "$3" ]; | |
then git diff --name-only --ignore-submodules=all --diff-filter=ACMR "$1" "$2" | |
else git diff --name-only --ignore-submodules=all --diff-filter=ACMR "$1" "$2" | awk -v r=$3 '{print "" r "/" $0}' | |
fi | |
for submodule in `git submodule | awk '{print $2}'` | |
do | |
old=$(git ls-tree $1 $submodule | awk '{print $3}') | |
new=$(git ls-tree $2 $submodule | awk '{print $3}') | |
(cd $submodule && compare $old $new $submodule) | |
done | |
} | |
change_count=$(compare "HEAD~1" "HEAD" | (grep -c -E 'README|CHANGELOG|/docs?/' ||:)) | |
if ((change_count > 0)); then | |
echo "changed=true" >> "$GITHUB_OUTPUT" | |
fi | |
- name: Push to branch | |
if: ${{ steps.check_doc_changes.outputs.changed }} | |
run: git push --force origin HEAD:$UPDATE_BRANCH | |
- name: Check if pull request already exists | |
if: ${{ steps.check_doc_changes.outputs.changed }} | |
id: check_pr_exists | |
shell: bash | |
run: | | |
pr_count=$(gh pr list --base main --head "$UPDATE_BRANCH" --state open --limit 1 --json number --jq length) | |
if ((pr_count > 0)); then | |
echo "exists=true" >> "$GITHUB_OUTPUT" | |
fi | |
env: | |
GH_TOKEN: ${{ steps.app-token.outputs.token }} | |
- name: Create pull request | |
if: ${{ steps.check_doc_changes.outputs.changed && !steps.check_pr_exists.outputs.exists }} | |
shell: bash | |
run: | | |
gh pr create \ | |
--base main \ | |
--head "$UPDATE_BRANCH" \ | |
--title "Update documentation from submodules" \ | |
--body "This PR pulls in the latest documentation changes from the external submodules." | |
env: | |
GH_TOKEN: ${{ steps.app-token.outputs.token }} |