Skip to content

daily-sync

daily-sync #151

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 }}
version_changed: ${{ steps.check_upstream.outputs.version_changed }}
push_success: ${{ steps.push_changes.outputs.push_success }}
commit_hash: ${{ steps.push_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 "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 "upstream_changes=${upstream_changes}"
version_changes=$(git diff upstream/main ${last_hash} -- packages/**/package.json)
echo "version_changes=${version_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
if [[ $version_changes ]]; then
echo "version_changed=true" >> $GITHUB_OUTPUT
echo "New release in packages"
else
echo "version_changed=false" >> $GITHUB_OUTPUT
echo "No release 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
cat changed.diff
# reset related files to upstream
git restore --source upstream/main apps/playground
rm -rf ./src ./config
mv apps/playground/{.[^.]*,*} ./ -f -v
if [[ $? -gt 0 ]]; then
cp apps/playground/{.[^.]*,*} ./ -r -v
fi
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, manual merge is required"
git status | grep .rej$ | xargs cat
exit 1
fi
- name: Setup Node
if: ${{ steps.check_upstream.outputs.upstream_changed == 'true' || steps.check_upstream.outputs.version_changed == 'true' }}
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Update dependencies
if: ${{ steps.check_upstream.outputs.upstream_changed == 'true' || steps.check_upstream.outputs.version_changed == 'true' }}
run: |
npm install
npm update $(grep -Eow '@music163/[^"]+?' package.json | tr '\n' ' ')
- name: Push changes
id: push_changes
if: ${{ steps.check_upstream.outputs.upstream_changed == 'true' || steps.check_upstream.outputs.version_changed == 'true' }}
run: |
# get upstream current hash
current_hash=$(git rev-parse upstream/main)
echo "upstream commit hash ${current_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 "committed hash ${commit_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.push_success == 'true' }}
uses: ./.github/workflows/deploy.yml
secrets: inherit