-
Notifications
You must be signed in to change notification settings - Fork 10
106 lines (89 loc) · 4.69 KB
/
upstream-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
name: Sync Release with Upstream
on:
schedule:
# Runs every 5 minutes
- cron: '*/5 * * * *'
# Allows you to manually trigger the workflow from the Actions tab
workflow_dispatch:
jobs:
sync:
runs-on: ubuntu-latest
steps:
- name: Checkout the forked repository
uses: actions/checkout@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
persist-credentials: false # We disable this because we will be using a different token
- name: Check for UPSTREAM_URL and UPSTREAM_SYNC_BRANCH_NAME in environment
id: check_variables
run: |
if [ -z "${{ vars.UPSTREAM_URL }}" ]; then
echo "UPSTREAM_URL environment variable does not exist."
echo "HAS_UPSTREAM=false" >> $GITHUB_ENV
else
echo "UPSTREAM_URL environment variable found."
echo "HAS_UPSTREAM=true" >> $GITHUB_ENV
echo "UPSTREAM_URL=${{ vars.UPSTREAM_URL }}" >> $GITHUB_ENV
fi
if [ -z "${{ vars.UPSTREAM_SYNC_BRANCH_NAME }}" ]; then
echo "UPSTREAM_SYNC_BRANCH_NAME environment variable does not exist."
echo "HAS_SYNC_BRANCH=false" >> $GITHUB_ENV
else
echo "UPSTREAM_SYNC_BRANCH_NAME environment variable found."
echo "HAS_SYNC_BRANCH=true" >> $GITHUB_ENV
echo "UPSTREAM_SYNC_BRANCH_NAME=${{ vars.UPSTREAM_SYNC_BRANCH_NAME }}" >> $GITHUB_ENV
fi
- name: Configure git for GitHub Actions
if: env.HAS_UPSTREAM == 'true' && env.HAS_SYNC_BRANCH == 'true'
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Add the upstream repository
if: env.HAS_UPSTREAM == 'true' && env.HAS_SYNC_BRANCH == 'true'
run: git remote add upstream ${{ env.UPSTREAM_URL }}
- name: Check for differences between local and upstream branch, excluding .github/workflows
if: env.HAS_UPSTREAM == 'true' && env.HAS_SYNC_BRANCH == 'true'
run: |
git fetch upstream
git fetch origin
# Check if the branch exists on the remote
if git show-ref --verify --quiet refs/remotes/origin/${{ env.UPSTREAM_SYNC_BRANCH_NAME }}; then
echo "Branch ${{ env.UPSTREAM_SYNC_BRANCH_NAME }} exists on remote."
# Compare origin branch with upstream, excluding .github/workflows
git diff --quiet origin/${{ env.UPSTREAM_SYNC_BRANCH_NAME }} upstream/main -- . ':(exclude).github/workflows' || echo "CHANGES_DETECTED=true" >> $GITHUB_ENV
else
echo "Branch ${{ env.UPSTREAM_SYNC_BRANCH_NAME }} does not exist on remote."
# Compare upstream/main with itself to force no changes
git diff --quiet upstream/main upstream/main -- . ':(exclude).github/workflows' || echo "CHANGES_DETECTED=false" >> $GITHUB_ENV
fi
- name: Fetch all branches from the upstream repository
if: env.HAS_UPSTREAM == 'true' && env.HAS_SYNC_BRANCH == 'true' && env.CHANGES_DETECTED == 'true'
run: git fetch upstream
- name: Set up authentication
if: env.HAS_UPSTREAM == 'true' && env.HAS_SYNC_BRANCH == 'true' && env.CHANGES_DETECTED == 'true'
run: |
git remote set-url origin https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_ACTOR: ${{ github.actor }}
- name: Merge upstream/main into forked repo branch
if: env.HAS_UPSTREAM == 'true' && env.HAS_SYNC_BRANCH == 'true' && env.CHANGES_DETECTED == 'true'
run: |
git checkout -B ${{ env.UPSTREAM_SYNC_BRANCH_NAME }} upstream/main
# GitHub Actions worker doesn't have the correct permissions to checked in
# modified workflow files. To workaround this, we'll remove all files in
# .github/workflows that doesn't exist on the remote
# Remove files not on the remote
rm -rf .github/workflows
# Check if the deploy branch exists on the remote
if git ls-remote --exit-code --heads origin ${{ env.UPSTREAM_SYNC_BRANCH_NAME }}; then
# Mirror remote workflows if they exist
if git ls-tree --name-only -r origin/${{ env.UPSTREAM_SYNC_BRANCH_NAME }}:.github/workflows &> /dev/null; then
git checkout origin/${{ env.UPSTREAM_SYNC_BRANCH_NAME }} .github/workflows
fi
fi
git commit -am "Prepare branch for deploy"
git push -f origin ${{ env.UPSTREAM_SYNC_BRANCH_NAME }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_ACTOR: ${{ github.actor }}