forked from obytes/react-native-template-obytes
-
Notifications
You must be signed in to change notification settings - Fork 3
136 lines (128 loc) · 6.24 KB
/
sync-with-upstream.yml
File metadata and controls
136 lines (128 loc) · 6.24 KB
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
# 🔗 Links:
# Source file: https://github.com/rootstrap/react-native-template/blob/master/.github/workflows/sync-with-upstream.yml
# ✍️ Description:
# This workflow is used to keep the template up to date with the another version of the upstream repository.
# 🚨 GITHUB SECRETS REQUIRED:
# - UPDATE_FROM_UPSTREAM_PAT: A fine-grained Personal Access Token.
# This token is used to commit and push to the template repository.
# You can generate one from here: https://github.com/settings/tokens?type=beta
# Set the Repository access to "Only select repositories" and select the template repository.
# Set the following Repo permissions:
# - Contents: Read & write (to commit and push the update branch to the template repository)
# - Metadata: Read-only (mandatory by GitHub)
# - Workflows: Read and write (to create, update and delete workflows in the template repository)
# Make sure to add it to the repo secrets with the name UPDATE_FROM_UPSTREAM_PAT:
# - Go to Repository Settings > Secrets and variables > Actions > New repository secret
# - Name: UPDATE_FROM_UPSTREAM_PAT
# - Value: The Personal Access Token you created
# ℹ️ Environment variables:
# - UPSTREAM_REPOSITORY: Repository to sync with
# - DIFF_EXCLUDED_ROUTES: List of files or directories to exclude from the diff.
# Any changes in these files or directories will be ignored
# and won't be incorporated to the Pull Request.
name: 🔄 Sync with upstream
on:
schedule:
- cron: "0 12 * * 1-5" # At 12:00 UTC on every day-of-week from Monday through Friday
workflow_dispatch:
inputs:
upstream-version:
type: string
description: "Upstream release version to sync with (e.g. v1.0.0). Leave empty to sync with the latest release."
required: false
default: ""
env:
UPSTREAM_REPOSITORY: obytes/react-native-template-obytes
DIFF_EXCLUDED_ROUTES:
| # Files/directories we don't want from the upstream repository
ios
android
.cursorrules
.github/workflows/eas-build-prod.yml
.github/workflows/eas-build-qa.yml
.github/workflows/new-app-version.yml
.github/workflows/new-github-release.yml
docs/public/reviews
src/app/login.tsx
src/components/card.tsx
jobs:
sync:
runs-on: ubuntu-latest
permissions:
actions: write
contents: read
pull-requests: write
steps:
- name: Check if Personal Access Token exists
env:
PAT: ${{ secrets.UPDATE_FROM_UPSTREAM_PAT }}
if: env.PAT == ''
run: |
echo "UPDATE_FROM_UPSTREAM_PAT secret not found. Please create a fine-grained Personal Access Token following the instructions in the workflow file."
exit 1
- name: Checkout template repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.UPDATE_FROM_UPSTREAM_PAT }}
- name: Set upstream version to sync with
run: |
if [ -z "${{ inputs.upstream-version }}" ]; then
UPSTREAM_UPDATE_VERSION=$(curl -s https://api.github.com/repos/${{ env.UPSTREAM_REPOSITORY }}/releases/latest | jq '.tag_name' | sed 's/\"//g')
else
UPSTREAM_UPDATE_VERSION=${{ inputs.upstream-version }}
fi
echo "UPSTREAM_UPDATE_VERSION=$UPSTREAM_UPDATE_VERSION" >> $GITHUB_ENV
- name: Check if branch already exists
run: |
git fetch origin
BRANCH_NAME=update-upstream-${{ env.UPSTREAM_UPDATE_VERSION }}
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
git branch -r | grep -q "origin/$BRANCH_NAME" && echo "BRANCH_EXISTS=true" >> $GITHUB_ENV || echo "BRANCH_EXISTS=false" >> $GITHUB_ENV
- name: Check if PR already exists
run: |
prs=$(gh pr list \
--head "$BRANCH_NAME" \
--json title \
--jq 'length')
if ((prs > 0)); then
echo "PR_EXISTS=true" >> $GITHUB_ENV
else
echo "PR_EXISTS=false" >> $GITHUB_ENV
fi
env:
GITHUB_TOKEN: ${{ secrets.UPDATE_FROM_UPSTREAM_PAT }}
- name: Checkout update release of upstream
if: ${{ env.BRANCH_EXISTS == 'false' }}
run: |
git remote add upstream https://github.com/${{ env.UPSTREAM_REPOSITORY }}.git
git fetch upstream $UPSTREAM_UPDATE_VERSION
- name: Merge latest release of upstream
if: ${{ env.BRANCH_EXISTS == 'false' }}
run: |
UPSTREAM_UPDATE_VERSION_HASH=$(git rev-parse --short FETCH_HEAD)
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
git merge $UPSTREAM_UPDATE_VERSION_HASH
continue-on-error: true
- name: Remove excluded files from the merge
if: ${{ env.BRANCH_EXISTS == 'false' }}
run: |
for route in $DIFF_EXCLUDED_ROUTES; do
git reset -- $route
done
- name: Commit and push changes to the update branch
if: ${{ env.BRANCH_EXISTS == 'false' }}
run: |
git add $(git diff --name-only --diff-filter=U)
git commit -m "chore: update upstream to ${{ env.UPSTREAM_UPDATE_VERSION }}" -n
git checkout -b ${{ env.BRANCH_NAME }}
git push origin ${{ env.BRANCH_NAME }}
git remote rm upstream
echo "BRANCH_EXISTS=true" >> $GITHUB_ENV
- name: 🎉 Create PR with changes
if: ${{ env.BRANCH_EXISTS == 'true' && env.PR_EXISTS == 'false' }}
run: |
gh pr create --title "chore: update upstream to ${{ env.UPSTREAM_UPDATE_VERSION }}" --body "Integrating latest changes from [obytes/react-native-template-obytes@${{ env.UPSTREAM_UPDATE_VERSION }}](https://github.com/obytes/react-native-template-obytes/releases/tag/${{ env.UPSTREAM_UPDATE_VERSION }})" --head ${{ env.BRANCH_NAME }}
env:
GITHUB_TOKEN: ${{ secrets.UPDATE_FROM_UPSTREAM_PAT }}