Sync Upstream #15
This file contains hidden or 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 */6 * * *' | |
| workflow_dispatch: | |
| jobs: | |
| sync: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout Fork | |
| uses: actions/[email protected] | |
| with: | |
| ref: master | |
| fetch-depth: 0 | |
| - name: Fetch Upstream | |
| run: | | |
| git remote add upstream https://github.com/LmeSzinc/AzurLaneAutoScript.git | |
| git fetch upstream master | |
| - name: Check Update | |
| run: | | |
| UPSTREAM_COMMITS=$(git rev-list HEAD..upstream/master --count) | |
| echo "UPSTREAM_COMMITS=$UPSTREAM_COMMITS" >> $GITHUB_ENV | |
| if [ "$UPSTREAM_COMMITS" -eq "0" ]; then | |
| echo "No new commits in upstream. Exiting." | |
| exit 0 | |
| fi | |
| echo "New commits detected: $UPSTREAM_COMMITS" | |
| echo "UPDATE=true" >> $GITHUB_ENV | |
| - name: Save Current Commit | |
| if: env.UPDATE == 'true' | |
| run: echo "CURRENT_COMMIT=$(git rev-parse HEAD)" >> $GITHUB_ENV | |
| - name: Merge Upstream | |
| if: env.UPDATE == 'true' | |
| run: | | |
| git config --global user.name "sync[bot]" | |
| git config --global user.email "sync[bot]@users.noreply.github.com" | |
| if git merge upstream/master; then | |
| echo "MERGE_SUCCESS=true" >> $GITHUB_ENV | |
| else | |
| echo "MERGE_SUCCESS=false" >> $GITHUB_ENV | |
| fi | |
| - name: Rollback | |
| if: env.MERGE_SUCCESS == 'false' | |
| run: | | |
| git merge --abort || true | |
| git reset --hard ${{ env.CURRENT_COMMIT }} | |
| git clean -fd | |
| echo "Merge failed. Rollback applied." >&2 | |
| exit 1 | |
| - name: Push | |
| if: env.MERGE_SUCCESS == 'true' | |
| run: git push origin master |