forked from seapath/meta-seapath
-
Notifications
You must be signed in to change notification settings - Fork 0
89 lines (85 loc) · 3.5 KB
/
sync-fork.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
name: Sync fork
on:
workflow_dispatch: {}
schedule:
- cron: "15 3 * * *" # Run every day at 3:15 UTC
jobs:
sync:
runs-on: ubuntu-latest
steps:
- name: Checkout fork's default branch
uses: actions/checkout@v4
with:
fetch-depth: 0
path: "fork"
token: ${{ secrets.GH_PAT }}
- name: Checkout fork's configuration branch
uses: actions/checkout@v4
with:
path: "configuration"
ref: "renovate-and-workflow-files"
token: ${{ secrets.GH_PAT }}
- name: Determine Upstream clone URL
id: upstream-repo-clone-url
uses: actions/github-script@v7
with:
script: |
const { data } = await github.rest.repos.get({
owner: context.repo.owner,
repo: context.repo.repo,
});
if (data.fork) {
return data.parent.clone_url;
} else {
throw new Error('This repository is not a fork.');
}
result-encoding: string
- name: Determine Upstream default branch
id: upstream-repo-default-branch
uses: actions/github-script@v7
with:
script: |
const { data } = await github.rest.repos.get({
owner: context.repo.owner,
repo: context.repo.repo,
});
if (data.fork) {
return data.parent.default_branch;
} else {
throw new Error('This repository is not a fork.');
}
result-encoding: string
- name: Sync fork with upstream
run: |
set -ex
cd fork
git config --global user.name "github-actions[bot]"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
git remote add upstream ${{ steps.upstream-repo-clone-url.outputs.result }}
git fetch upstream ${{ steps.upstream-repo-default-branch.outputs.result }}
UPSTREAM_MOST_RECENT_COMMIT_HASH=$(git log upstream/${{ steps.upstream-repo-default-branch.outputs.result }} -n 1 --format="%H")
PREVIOUS_SYNC_COMMIT_HASH=$(cat ../configuration/upstream_commit_hash)
if [ "$PREVIOUS_SYNC_COMMIT_HASH" = "$UPSTREAM_MOST_RECENT_COMMIT_HASH" ]; then
echo "No need to sync, already up-to-date"
exit 0
fi
git reset --hard upstream/${{ steps.upstream-repo-default-branch.outputs.result }}
# Enforce the usage of our own config (renovate.json5)
git rm renovate.json* || true
# Avoid problems where an existing .gitignore file would prevent committing our configuration files
git rm .gitignore || true
# Delete existing workflows, we don't need to run them in our fork
rm -rf .github || true
# Instead of using "cp -r", rsync allows us to exclude the .git directory
rsync -av --exclude '.git' ../configuration/ .
rm upstream_commit_hash
git add .
git commit -m "add Renovate and custom workflow files"
git push --force-with-lease
cd ../configuration
# git config user.name "github-actions[bot]"
# git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
echo $UPSTREAM_MOST_RECENT_COMMIT_HASH > upstream_commit_hash
git add upstream_commit_hash
git commit -m "update commit hash to $UPSTREAM_MOST_RECENT_COMMIT_HASH"
git push