-
Notifications
You must be signed in to change notification settings - Fork 7
141 lines (123 loc) · 5.21 KB
/
daily-sync.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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 @music163/tango-core @music163/tango-designer @music163/tango-helpers
- 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