Skip to content

Commit 50e2998

Browse files
committed
feat: upstream sync workflow V2
1 parent 9ed484a commit 50e2998

File tree

4 files changed

+115
-34
lines changed

4 files changed

+115
-34
lines changed

.github/project-workflows/sync-with-template.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ env:
4242
.github/workflows/deploy-docs.yml
4343
.github/workflows/new-template-version.yml
4444
.github/workflows/upstream-to-pr.yml
45+
.github/workflows/sync-with-template.yml
4546
README-project.md
4647
4748
jobs:
@@ -71,7 +72,7 @@ jobs:
7172
- name: Get latest release of template from GitHub
7273
run: |
7374
echo "TEMPLATE_LATEST_VERSION=$(curl -s https://api.github.com/repos/${{ env.TEMPLATE_REPOSITORY }}/releases/latest | jq '.tag_name' | sed 's/\"//g')" >> $GITHUB_ENV
74-
- name: Check if the template is up to date
75+
- name: Check if the project is up to date
7576
run: |
7677
if [[ $TEMPLATE_LATEST_VERSION == $PROJECT_TEMPLATE_VERSION ]]; then
7778
echo "Template is up to date"
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# 🔗 Links:
2+
# Source file: https://github.com/rootstrap/react-native-template/blob/master/.github/workflows/sync-with-upstream.yml
3+
4+
# ✍️ Description:
5+
# This workflow is used to keep the template up to date with the latest version of the upstream repository.
6+
7+
# 🚨 GITHUB SECRETS REQUIRED:
8+
# - UPDATE_FROM_TEMPLATE_PAT: A fine-grained Personal Access Token.
9+
# This token is used to access the template repository and create the Pull Request.
10+
# You can generate one from here: https://github.com/settings/tokens?type=beta
11+
# Set the Repository access to "Only select repositories" and select the template repository.
12+
# Set the following Repo permissions:
13+
# - Contents: Read & write (to commit and push the update branch to the template repository)
14+
# - Metadata: Read-only (mandatory by GitHub)
15+
# - Pull requests: Read & write (to create the Pull Request in the template repository)
16+
# - Workflows: Read and write (to create, update and delete workflows in the template repository)
17+
# Make sure to add it to the repo secrets with the name UPDATE_FROM_TEMPLATE_PAT:
18+
# - Go to Repository Settings > Secrets and variables > Actions > New repository secret
19+
# - Name: UPDATE_FROM_TEMPLATE_PAT
20+
# - Value: The Personal Access Token you created
21+
22+
# ℹ️ Environment variables:
23+
# - UPSTREAM_REPOSITORY: Repository to sync with
24+
# - DIFF_EXCLUDED_ROUTES: List of files or directories to exclude from the diff.
25+
# Any changes in these files or directories will be ignored
26+
# and won't be incorporated to the Pull Request.
27+
28+
name: 🔄 Sync with upstream
29+
30+
on:
31+
schedule:
32+
- cron: '0 12 * * *' # Everyday at 12:00 UTC
33+
workflow_dispatch:
34+
35+
env:
36+
UPSTREAM_REPOSITORY: obytes/react-native-template-obytes
37+
DIFF_EXCLUDED_ROUTES: |
38+
ios
39+
android
40+
41+
jobs:
42+
sync:
43+
runs-on: ubuntu-latest
44+
permissions:
45+
actions: write
46+
contents: read
47+
48+
steps:
49+
- name: Check if Personal Access Token exists
50+
env:
51+
PAT: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }}
52+
if: env.PAT == ''
53+
run: |
54+
echo "UPDATE_FROM_TEMPLATE_PAT secret not found. Please create a fine-grained Personal Access Token following the instructions in the workflow file."
55+
exit 1
56+
- name: Checkout template repository
57+
uses: actions/checkout@v3
58+
with:
59+
fetch-depth: 0
60+
token: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }}
61+
- name: Get latest release of upstream from GitHub
62+
run: |
63+
echo "UPSTREAM_LATEST_VERSION=$(curl -s https://api.github.com/repos/${{ env.UPSTREAM_REPOSITORY }}/releases/latest | jq '.tag_name' | sed 's/\"//g')" >> $GITHUB_ENV
64+
- name: Check if branch already exists
65+
run: |
66+
git fetch origin
67+
BRANCH_NAME=update-upstream-${{ env.UPSTREAM_LATEST_VERSION }}
68+
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
69+
git branch -r | grep -q "origin/$BRANCH_NAME" && echo "BRANCH_EXISTS=true" >> $GITHUB_ENV || echo "BRANCH_EXISTS=false" >> $GITHUB_ENV
70+
- name: Check if PR already exists
71+
run: |
72+
prs=$(gh pr list \
73+
--head "$BRANCH_NAME" \
74+
--json title \
75+
--jq 'length')
76+
if ((prs > 0)); then
77+
echo "PR_EXISTS=true" >> $GITHUB_ENV
78+
else
79+
echo "PR_EXISTS=false" >> $GITHUB_ENV
80+
fi
81+
env:
82+
GITHUB_TOKEN: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }}
83+
- name: Checkout latest release of upstream
84+
if: ${{ env.BRANCH_EXISTS == 'false' }}
85+
run: |
86+
git remote add upstream https://github.com/${{ env.UPSTREAM_REPOSITORY }}.git
87+
git fetch upstream
88+
- name: Merge latest release of upstream
89+
if: ${{ env.BRANCH_EXISTS == 'false' }}
90+
run: |
91+
UPSTREAM_LATEST_VERSION_HASH=$(git ls-remote --tags upstream | grep $UPSTREAM_LATEST_VERSION | sed -n 2p | cut -f1)
92+
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
93+
git config --global user.name "github-actions[bot]"
94+
git merge --no-commit --no-ff $UPSTREAM_LATEST_VERSION_HASH
95+
for route in $DIFF_EXCLUDED_ROUTES; do
96+
git reset -- $route
97+
done
98+
- name: Commit and push changes to the update branch
99+
if: ${{ env.BRANCH_EXISTS == 'false' }}
100+
run: |
101+
git checkout -b ${{ env.BRANCH_NAME }}
102+
git add .
103+
git commit -m "chore: update upstream to ${{ env.UPSTREAM_LATEST_VERSION }}"
104+
git push origin ${{ env.BRANCH_NAME }}
105+
echo "BRANCH_EXISTS=true" >> $GITHUB_ENV
106+
- name: 🎉 Create PR with changes
107+
if: ${{ env.BRANCH_EXISTS == 'true' && env.PR_EXISTS == 'false' }}
108+
run: |
109+
gh pr create --title "chore: update upstream to ${{ env.UPSTREAM_LATEST_VERSION }}" --body "Integrating latest changes from [obytes/react-native-template-obytes@${{ env.UPSTREAM_LATEST_VERSION }}](https://github.com/obytes/react-native-template-obytes/releases/tag/${{ env.UPSTREAM_LATEST_VERSION }})" --head ${{ env.BRANCH_NAME }}
110+
env:
111+
GITHUB_TOKEN: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }}

.github/workflows/upstream-to-pr.yml

Lines changed: 0 additions & 22 deletions
This file was deleted.

cli/setup-project.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,6 @@ const updateProjectConfig = async (projectName) => {
9393
const updateGitHubWorkflows = (projectName) => {
9494
// Update useful workflows
9595
projectFilesManager.replaceFilesContent([
96-
{
97-
fileName: '.github/workflows/upstream-to-pr.yml',
98-
replacements: [
99-
{
100-
searchValue: UPSTREAM_REPOSITORY,
101-
replaceValue: TEMPLATE_REPOSITORY,
102-
},
103-
],
104-
},
10596
{
10697
fileName: '.github/workflows/new-template-version.yml',
10798
replacements: [
@@ -146,8 +137,8 @@ const updateGitHubWorkflows = (projectName) => {
146137
},
147138
]);
148139

149-
// Remove upstream update workflow, intended to be used only in the template repository
150-
projectFilesManager.removeFiles(['.github/workflows/upstream-to-pr.yml']);
140+
// Remove upstream sync workflow, intended to be used only in the template repository
141+
projectFilesManager.removeFiles(['.github/workflows/sync-with-upstream.yml']);
151142

152143
// Enable sync with template workflow
153144
projectFilesManager.renameFiles([

0 commit comments

Comments
 (0)