Automated Update #209
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: Automated Update | |
on: | |
schedule: | |
- cron: "0 0 * * *" | |
workflow_dispatch: | |
permissions: | |
contents: write | |
pull-requests: write | |
env: | |
BRANCH_UPDATE: auto-update | |
IDENTITY_NAME: github-actions[bot] | |
IDENTITY_MAIL: 41898282+github-actions[bot]@users.noreply.github.com | |
jobs: | |
get_parent: | |
name: Get Parent Repository | |
runs-on: ubuntu-latest | |
outputs: | |
url: ${{ steps.parse_url.outputs.parent_url }} | |
repo: ${{ steps.parse_details.outputs.parent_repo }} | |
branch: ${{ steps.parse_details.outputs.parent_branch }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
- name: Parse parent url | |
id: parse_url | |
run: | | |
parent_url=$(git log -1 --grep='Merged from https://github.com/' | sed -n -e 's/\s*Merged from //p' | head -1 | cut -d' ' -f1) | |
echo "parent_url=$parent_url" >> $GITHUB_OUTPUT | |
- name: Parse parent details | |
id: parse_details | |
if: steps.parse_url.outputs.parent_url != '' | |
run: | | |
parent_repo=$(echo ${{ steps.parse_url.outputs.parent_url }} | grep -o -P '(?<=https:\/\/github\.com\/).*(?=\/tree\/)') | |
parent_branch=$(echo ${{ steps.parse_url.outputs.parent_url }} | grep -o -P '(?<=\/tree\/).*') | |
echo "parent_repo=$parent_repo" >> $GITHUB_OUTPUT | |
echo "parent_branch=$parent_branch" >> $GITHUB_OUTPUT | |
update: | |
name: Update Document | |
needs: [ get_parent ] | |
runs-on: ubuntu-latest | |
if: | | |
needs.get_parent.outputs.repo != '' && | |
needs.get_parent.outputs.branch != '' | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
with: | |
token: ${{ secrets.UPDATE_TOKEN == '' && secrets.GITHUB_TOKEN || secrets.UPDATE_TOKEN }} | |
fetch-depth: 0 | |
- name: Extract branch name | |
run: | |
echo "BRANCH_CURRENT=$GITHUB_REF_NAME" >> $GITHUB_ENV | |
- name: Create update branch | |
run: git switch $BRANCH_UPDATE 2>/dev/null || git switch -c $BRANCH_UPDATE | |
- name: Setup git identity | |
run: | | |
git config user.name $IDENTITY_NAME | |
git config user.email $IDENTITY_MAIL | |
- name: Update document | |
run: | | |
curl -s https://raw.githubusercontent.com/${{ needs.get_parent.outputs.repo }}/${{ needs.get_parent.outputs.branch }}/scripts/update.sh -o ../update.sh | |
bash ../update.sh | |
rm ../update.sh | |
- name: Check changes | |
id: check_changes | |
run: | | |
has_changes=$(git diff --exit-code --quiet HEAD origin/$BRANCH_CURRENT &> /dev/null && echo false || (git diff --exit-code --quiet HEAD origin/$BRANCH_UPDATE &> /dev/null && echo false || echo true)) | |
echo "UPDATE_AVAILABLE=$has_changes" >> $GITHUB_OUTPUT | |
- name: Push changes | |
if: steps.check_changes.outputs.UPDATE_AVAILABLE == 'true' | |
run: git push --set-upstream origin $BRANCH_UPDATE | |
- name: Create pull request | |
if: steps.check_changes.outputs.UPDATE_AVAILABLE == 'true' | |
continue-on-error: true | |
run: | | |
gh pr reopen $BRANCH_UPDATE || gh pr create --base main --title "chore: :twisted_rightwards_arrows: Merge changes from template" --body "Merged from https://github.com/${{ needs.get_parent.outputs.repo }}/tree/${{ needs.get_parent.outputs.branch }}" | |
env: | |
GITHUB_TOKEN: ${{ secrets.UPDATE_TOKEN == '' && secrets.GITHUB_TOKEN || secrets.UPDATE_TOKEN }} |