Skip to content

daily-sync

daily-sync #3

Workflow file for this run

name: daily-sync
permissions:
contents: write
actions: read
on:
# TODO: sync changes when upstream repo is updated (e.g. webhooks)
schedule:
- cron: '30 9 * * 1-5'
workflow_dispatch:
jobs:
sync_upstream:
runs-on: ubuntu-latest
outputs:
upstream_changed: ${{ steps.check_upstream.outputs.upstream_changed }}
push_success: ${{ steps.sync_changes.outputs.push_success }}
commit_hash: ${{ steps.sync_changes.outputs.commit_hash }}
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Check upstream modified
id: check_upstream
run: |
# get last sync hash
last_hash=$(git log --grep 'feat: sync upstream changes' -1 | grep -P '(?<=updated to NetEase/tango@).+$' -o)
if [[ -z $last_hash ]]; then
echo "Can't find the last commit"
echo "upstream_changed=false" >> $GITHUB_OUTPUT
exit 0
fi
echo "::debug::get last commit hash ${last_hash}"
echo "last_hash=${last_hash}" >> $GITHUB_OUTPUT
# add upstream to remote
if [[ $(git remote | grep upstream) ]]; then
git remote remove upstream
fi
git remote add upstream https://github.com/NetEase/tango.git
git fetch upstream
# check if upstream has changed
upstream_changes=$(git diff upstream/main ${last_hash} -- apps/playground)
echo "::debug::${upstream_changes}"
if [[ $upstream_changes ]]; then
echo "upstream_changed=true" >> $GITHUB_OUTPUT
echo "Files changed in apps/playground"
else
echo "upstream_changed=false" >> $GITHUB_OUTPUT
echo "No changes from upstream"
fi
- name: Sync upstream changes
id: sync_changes
if: ${{ steps.check_upstream.outputs.upstream_changed == 'true' }}
env:
LAST_HASH: ${{ steps.check_upstream.outputs.last_hash }}
run: |
last_hash=$LAST_HASH
# get changed files
# changed=$(git diff ${last_hash} -- apps/playground ./*)
# git diff upstream/main -- apps/playground/* HEAD -- ./* | sed -e "s|apps/playground/||g" > changed.diff
git diff ${last_hash}:apps/playground/ HEAD -- . ':!README.md' ':!package-lock.json' ':!.github' | sed -e "s|apps/playground/||g" > changed.diff
echo "::debug::$(cat changed.diff)"
# reset related files to upstream
git restore --source upstream/main apps/playground
rm -rf ./src ./config
mv apps/playground/{.*,*} ./ -f -v
rm -rf ./apps
# apply changes
git apply ./changed.diff --reject
rm ./changed.diff
# check if all files are patched without any conflicts
if [[ $(git status | grep .rej$) ]]; then
# print rejected files to console
echo "::error::Some files are rejected"
git status | grep .rej$ | xargs cat
exit 1
fi
- name: Setup Node
if: ${{ steps.check_upstream.outputs.upstream_changed == 'true' }}
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Update dependencies
if: ${{ steps.check_upstream.outputs.upstream_changed == 'true' }}
run: |
npm install
npm update @music163/tango-core @music163/tango-designer @music163/tango-helpers
- name: Push changes
if: ${{ steps.check_upstream.outputs.upstream_changed == 'true' }}
run: |
# get upstream current hash
current_hash=$(git rev-parse upstream/main)
echo "::debug::upstream commit hash ${last_hash}"
nl=$'\n';
# commit changes
git add .
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git commit -m "feat: sync upstream changes${nl}${nl}updated to NetEase/tango@${current_hash}"
commit_hash=$(git rev-parse HEAD)
echo "::debug::committed hash ${last_hash}"
echo "commit_hash=${commit_hash}" >> $GITHUB_OUTPUT
git push
echo "push_success=true" >> $GITHUB_OUTPUT
echo "Commit has been pushed successfully"
deploy:
needs: sync_upstream
if: ${{ needs.sync_upstream.outputs.upstream_changed == 'true' && needs.sync_upstream.outputs.push_success == 'true' }}
uses: ./.github/workflows/deploy.yml
secrets: inherit