Skip to content

Commit 4c6ee6b

Browse files
committed
feat: upstream sync workflow V2
1 parent 9ed484a commit 4c6ee6b

File tree

4 files changed

+156
-48
lines changed

4 files changed

+156
-48
lines changed

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66

77
# 🚨 GITHUB SECRETS REQUIRED:
88
# - UPDATE_FROM_TEMPLATE_PAT: A fine-grained Personal Access Token.
9-
# This token is used to access the project repository and create the Pull Request.
9+
# This token is used to commit and push to the project repository.
1010
# You can generate one from here: https://github.com/settings/tokens?type=beta
1111
# Set the Repository access to "Only select repositories" and select the project repository.
1212
# Set the following Repo permissions:
1313
# - Contents: Read & write (to commit and push the update branch to the project repository)
1414
# - Metadata: Read-only (mandatory by GitHub)
15-
# - Pull requests: Read & write (to create the Pull Request in the project repository)
1615
# - Workflows: Read and write (to create, update and delete workflows in the project repository)
1716
# Make sure to add it to the repo secrets with the name UPDATE_FROM_TEMPLATE_PAT:
1817
# - Go to Repository Settings > Secrets and variables > Actions > New repository secret
@@ -29,8 +28,14 @@ name: 🔄 Sync with template
2928

3029
on:
3130
schedule:
32-
- cron: '0 12 * * *' # Everyday at 12:00 UTC
31+
- cron: '0 12 * * 1-5' # At 12:00 UTC on every day-of-week from Monday through Friday
3332
workflow_dispatch:
33+
inputs:
34+
template-version:
35+
type: string
36+
description: 'Template release version to sync with (e.g. v1.0.0). Leave empty to sync with the latest release.'
37+
required: false
38+
default: ''
3439

3540
env:
3641
TEMPLATE_REPOSITORY: rootstrap/react-native-template
@@ -42,6 +47,7 @@ env:
4247
.github/workflows/deploy-docs.yml
4348
.github/workflows/new-template-version.yml
4449
.github/workflows/upstream-to-pr.yml
50+
.github/workflows/sync-with-upstream.yml
4551
README-project.md
4652
4753
jobs:
@@ -50,6 +56,7 @@ jobs:
5056
permissions:
5157
actions: write
5258
contents: read
59+
pull-requests: write
5360

5461
steps:
5562
- name: Check if Personal Access Token exists
@@ -68,12 +75,17 @@ jobs:
6875
- name: Get template version used in project from package.json
6976
run: |
7077
echo "PROJECT_TEMPLATE_VERSION=v$(jq -r 'if has("rsMetadata") then .rsMetadata.templateVersion else .osMetadata.initVersion end' project/package.json | sed 's/^.*@//')" >> $GITHUB_ENV
71-
- name: Get latest release of template from GitHub
78+
- name: Set template version to sync with
7279
run: |
73-
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
80+
if [ -z "${{ inputs.template-version }}" ]; then
81+
TEMPLATE_UPDATE_VERSION=$(curl -s https://api.github.com/repos/${{ env.TEMPLATE_REPOSITORY }}/releases/latest | jq '.tag_name' | sed 's/\"//g')
82+
else
83+
TEMPLATE_UPDATE_VERSION=${{ inputs.template-version }}
84+
fi
85+
echo "TEMPLATE_UPDATE_VERSION=$TEMPLATE_UPDATE_VERSION" >> $GITHUB_ENV
86+
- name: Check if the project is up to date
7587
run: |
76-
if [[ $TEMPLATE_LATEST_VERSION == $PROJECT_TEMPLATE_VERSION ]]; then
88+
if [[ $TEMPLATE_UPDATE_VERSION == $PROJECT_TEMPLATE_VERSION ]]; then
7789
echo "Template is up to date"
7890
cd project
7991
gh run cancel ${{ github.run_id }}
@@ -85,7 +97,7 @@ jobs:
8597
run: |
8698
cd project
8799
git fetch origin
88-
BRANCH_NAME=update-template-${{ env.TEMPLATE_LATEST_VERSION }}
100+
BRANCH_NAME=update-template-${{ env.TEMPLATE_UPDATE_VERSION }}
89101
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
90102
git branch -r | grep -q "origin/$BRANCH_NAME" && echo "BRANCH_EXISTS=true" >> $GITHUB_ENV || echo "BRANCH_EXISTS=false" >> $GITHUB_ENV
91103
- name: Check if PR already exists
@@ -101,17 +113,17 @@ jobs:
101113
echo "PR_EXISTS=false" >> $GITHUB_ENV
102114
fi
103115
env:
104-
GITHUB_TOKEN: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }}
116+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
105117
- name: Install dependencies
106118
if: ${{ env.BRANCH_EXISTS == 'false' }}
107119
run: |
108120
sudo apt install wiggle
109-
- name: Checkout latest release of template
121+
- name: Checkout update release of template
110122
if: ${{ env.BRANCH_EXISTS == 'false' }}
111123
uses: actions/checkout@v3
112124
with:
113125
repository: ${{ env.TEMPLATE_REPOSITORY }}
114-
ref: ${{ env.TEMPLATE_LATEST_VERSION }}
126+
ref: ${{ env.TEMPLATE_UPDATE_VERSION }}
115127
fetch-depth: 0
116128
path: react-native-template
117129
- name: Get diff between latest release and used release
@@ -128,7 +140,7 @@ jobs:
128140
run: |
129141
cd project
130142
jq 'del(.osMetadata)' package.json > tmp.json && mv tmp.json package.json
131-
PLAIN_VERSION=${TEMPLATE_LATEST_VERSION#v}
143+
PLAIN_VERSION=${TEMPLATE_UPDATE_VERSION#v}
132144
jq --arg version $PLAIN_VERSION '.rsMetadata.templateVersion = $version' package.json > tmp.json && mv tmp.json package.json
133145
- name: Apply diff to project repository
134146
if: ${{ env.BRANCH_EXISTS == 'false' }}
@@ -156,13 +168,13 @@ jobs:
156168
git config --global user.name "github-actions[bot]"
157169
git checkout -b ${{ env.BRANCH_NAME }}
158170
git add .
159-
git commit -m "chore: update template to ${{ env.TEMPLATE_LATEST_VERSION }}"
171+
git commit -m "chore: update template to ${{ env.TEMPLATE_UPDATE_VERSION }}"
160172
git push origin ${{ env.BRANCH_NAME }}
161173
echo "BRANCH_EXISTS=true" >> $GITHUB_ENV
162174
- name: 🎉 Create PR with changes
163175
if: ${{ env.BRANCH_EXISTS == 'true' && env.PR_EXISTS == 'false' }}
164176
run: |
165177
cd project
166-
gh pr create --title "chore: update template to ${{ env.TEMPLATE_LATEST_VERSION }}" --body "Integrating latest changes from [rootstrap/react-native-template@${{ env.TEMPLATE_LATEST_VERSION }}](https://github.com/rootstrap/react-native-template/releases/tag/${{ env.TEMPLATE_LATEST_VERSION }})" --head ${{ env.BRANCH_NAME }}
178+
gh pr create --title "chore: update template to ${{ env.TEMPLATE_UPDATE_VERSION }}" --body "Integrating latest changes from [rootstrap/react-native-template@${{ env.TEMPLATE_UPDATE_VERSION }}](https://github.com/rootstrap/react-native-template/releases/tag/${{ env.TEMPLATE_UPDATE_VERSION }})" --head ${{ env.BRANCH_NAME }}
167179
env:
168-
GITHUB_TOKEN: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }}
180+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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 another version of the upstream repository.
6+
7+
# 🚨 GITHUB SECRETS REQUIRED:
8+
# - UPDATE_FROM_UPSTREAM_PAT: A fine-grained Personal Access Token.
9+
# This token is used to commit and push to the template repository.
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+
# - Workflows: Read and write (to create, update and delete workflows in the template repository)
16+
# Make sure to add it to the repo secrets with the name UPDATE_FROM_UPSTREAM_PAT:
17+
# - Go to Repository Settings > Secrets and variables > Actions > New repository secret
18+
# - Name: UPDATE_FROM_UPSTREAM_PAT
19+
# - Value: The Personal Access Token you created
20+
21+
# ℹ️ Environment variables:
22+
# - UPSTREAM_REPOSITORY: Repository to sync with
23+
# - DIFF_EXCLUDED_ROUTES: List of files or directories to exclude from the diff.
24+
# Any changes in these files or directories will be ignored
25+
# and won't be incorporated to the Pull Request.
26+
27+
name: 🔄 Sync with upstream
28+
29+
on:
30+
schedule:
31+
- cron: '0 12 * * 1-5' # At 12:00 UTC on every day-of-week from Monday through Friday
32+
workflow_dispatch:
33+
inputs:
34+
upstream-version:
35+
type: string
36+
description: 'Upstream release version to sync with (e.g. v1.0.0). Leave empty to sync with the latest release.'
37+
required: false
38+
default: ''
39+
40+
env:
41+
UPSTREAM_REPOSITORY: obytes/react-native-template-obytes
42+
DIFF_EXCLUDED_ROUTES: |
43+
ios
44+
android
45+
46+
jobs:
47+
sync:
48+
runs-on: ubuntu-latest
49+
permissions:
50+
actions: write
51+
contents: read
52+
pull-requests: write
53+
54+
steps:
55+
- name: Check if Personal Access Token exists
56+
env:
57+
PAT: ${{ secrets.UPDATE_FROM_UPSTREAM_PAT }}
58+
if: env.PAT == ''
59+
run: |
60+
echo "UPDATE_FROM_UPSTREAM_PAT secret not found. Please create a fine-grained Personal Access Token following the instructions in the workflow file."
61+
exit 1
62+
- name: Checkout template repository
63+
uses: actions/checkout@v3
64+
with:
65+
fetch-depth: 0
66+
token: ${{ secrets.UPDATE_FROM_UPSTREAM_PAT }}
67+
- name: Set upstream version to sync with
68+
run: |
69+
if [ -z "${{ inputs.upstream-version }}" ]; then
70+
UPSTREAM_UPDATE_VERSION=$(curl -s https://api.github.com/repos/${{ env.UPSTREAM_REPOSITORY }}/releases/latest | jq '.tag_name' | sed 's/\"//g')
71+
else
72+
UPSTREAM_UPDATE_VERSION=${{ inputs.upstream-version }}
73+
fi
74+
echo "UPSTREAM_UPDATE_VERSION=$UPSTREAM_UPDATE_VERSION" >> $GITHUB_ENV
75+
- name: Check if branch already exists
76+
run: |
77+
git fetch origin
78+
BRANCH_NAME=update-upstream-${{ env.UPSTREAM_UPDATE_VERSION }}
79+
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
80+
git branch -r | grep -q "origin/$BRANCH_NAME" && echo "BRANCH_EXISTS=true" >> $GITHUB_ENV || echo "BRANCH_EXISTS=false" >> $GITHUB_ENV
81+
- name: Check if PR already exists
82+
run: |
83+
prs=$(gh pr list \
84+
--head "$BRANCH_NAME" \
85+
--json title \
86+
--jq 'length')
87+
if ((prs > 0)); then
88+
echo "PR_EXISTS=true" >> $GITHUB_ENV
89+
else
90+
echo "PR_EXISTS=false" >> $GITHUB_ENV
91+
fi
92+
env:
93+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
94+
- name: Checkout update release of upstream
95+
if: ${{ env.BRANCH_EXISTS == 'false' }}
96+
run: |
97+
git remote add upstream https://github.com/${{ env.UPSTREAM_REPOSITORY }}.git
98+
git fetch upstream $UPSTREAM_UPDATE_VERSION
99+
- name: Merge latest release of upstream
100+
if: ${{ env.BRANCH_EXISTS == 'false' }}
101+
run: |
102+
UPSTREAM_UPDATE_VERSION_HASH=$(git rev-parse --short FETCH_HEAD)
103+
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
104+
git config --global user.name "github-actions[bot]"
105+
git merge $UPSTREAM_UPDATE_VERSION_HASH
106+
continue-on-error: true
107+
- name: Remove excluded files from the merge
108+
if: ${{ env.BRANCH_EXISTS == 'false' }}
109+
run: |
110+
for route in $DIFF_EXCLUDED_ROUTES; do
111+
git reset -- $route
112+
done
113+
- name: Commit and push changes to the update branch
114+
if: ${{ env.BRANCH_EXISTS == 'false' }}
115+
run: |
116+
git add .
117+
git commit -m "chore: update upstream to ${{ env.UPSTREAM_UPDATE_VERSION }}"
118+
git checkout -b ${{ env.BRANCH_NAME }}
119+
git push origin ${{ env.BRANCH_NAME }}
120+
git remote rm upstream
121+
echo "BRANCH_EXISTS=true" >> $GITHUB_ENV
122+
- name: 🎉 Create PR with changes
123+
if: ${{ env.BRANCH_EXISTS == 'true' && env.PR_EXISTS == 'false' }}
124+
run: |
125+
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 }}
126+
env:
127+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.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)