Sync Upstream #31
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: Sync Upstream | |
on: | |
schedule: | |
- cron: '0 0 * * 1' # Runs every Monday at 00:00 UTC | |
workflow_dispatch: # Allows manual triggering of the workflow | |
jobs: | |
sync: | |
permissions: | |
actions: write | |
checks: write | |
contents: write | |
deployments: write | |
issues: write | |
packages: write | |
pull-requests: write | |
repository-projects: write | |
security-events: write | |
statuses: write | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
with: | |
fetch-depth: 0 | |
- name: Set up Git | |
run: | | |
git config --global user.name 'GitHub Actions Bot' | |
git config --global user.email '[email protected]' | |
- name: Add upstream remote | |
run: git remote add upstream https://github.com/jhipster/jhipster.github.io.git || true | |
- name: Fetch upstream | |
run: git fetch upstream | |
- name: Create new branch | |
run: | | |
SHORT_COMMIT_HASH=$(git rev-parse --short upstream/main) | |
BRANCH_NAME=sync-$SHORT_COMMIT_HASH | |
EXISTS=$(git ls-remote --heads origin $BRANCH_NAME | wc -l) | |
if [ $EXISTS -eq 1 ]; then | |
echo "ブランチ $BRANCH_NAME は既に存在します" | |
echo "branch_exists=true" >> $GITHUB_ENV | |
exit 0 | |
fi | |
git checkout -b $BRANCH_NAME origin/main | |
echo "branch_name=$BRANCH_NAME" >> $GITHUB_ENV | |
- name: Merge upstream/main | |
if: ${{ env.branch_exists != 'true' }} | |
run: | | |
OUTPUT=$(git merge upstream/main --allow-unrelated-histories --no-edit || true) | |
if echo $OUTPUT | grep -q 'Already up to date.'; then | |
echo "すでに最新です" | |
echo "merge_status=up_to_date" >> $GITHUB_ENV | |
fi | |
- name: Commit merge conflicts | |
if: ${{ env.branch_exists != 'true' && env.merge_status != 'up_to_date' }} | |
run: git commit -a --no-edit || true | |
- name: Find conflict files | |
if: ${{ env.branch_exists != 'true' && env.merge_status != 'up_to_date' }} | |
id: find-conflict-files | |
run: | | |
CONFLICT_FILES=$(git diff HEAD^ HEAD --name-only --diff-filter=M | while read file; do if grep -q '<<<<<<< HEAD' "$file"; then echo -n "- [ ] $file\\n"; fi; done) | |
echo "checkbox_conflict_files=${CONFLICT_FILES}" >> $GITHUB_ENV | |
- name: Push new branch | |
if: ${{ env.branch_exists != 'true' && env.merge_status != 'up_to_date' }} | |
run: | | |
git push origin HEAD | |
- name: Create pull request | |
if: ${{ env.branch_exists != 'true' && env.merge_status != 'up_to_date' }} | |
run: | | |
gh pr create --title "Merge ${{ env.branch_name }} into main" --base main --head ${{ env.branch_name }} --repo ${{ github.repository }} --body $'フォーク元が更新されましたのでマージしてください。\n\n以下はコンフリクトがあります。\n\n${{ env.checkbox_conflict_files }}\n\n修正をPushしてコンフリクトを解消してください。' | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |