Skip to content

Commit 0174c1d

Browse files
committed
feat: upstream sync workflow V2
1 parent 9ed484a commit 0174c1d

File tree

4 files changed

+100
-37
lines changed

4 files changed

+100
-37
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
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,7 +28,7 @@ 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:
3433

3534
env:
@@ -42,6 +41,7 @@ env:
4241
.github/workflows/deploy-docs.yml
4342
.github/workflows/new-template-version.yml
4443
.github/workflows/upstream-to-pr.yml
44+
.github/workflows/sync-with-template.yml
4545
README-project.md
4646
4747
jobs:
@@ -50,6 +50,7 @@ jobs:
5050
permissions:
5151
actions: write
5252
contents: read
53+
pull-requests: write
5354

5455
steps:
5556
- name: Check if Personal Access Token exists
@@ -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"
@@ -165,4 +166,4 @@ jobs:
165166
cd project
166167
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 }}
167168
env:
168-
GITHUB_TOKEN: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }}
169+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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: None
8+
9+
# ℹ️ Environment variables:
10+
# - UPSTREAM_REPOSITORY: Repository to sync with
11+
# - DIFF_EXCLUDED_ROUTES: List of files or directories to exclude from the diff.
12+
# Any changes in these files or directories will be ignored
13+
# and won't be incorporated to the Pull Request.
14+
15+
name: 🔄 Sync with upstream
16+
17+
on:
18+
schedule:
19+
- cron: '0 12 * * 1-5' # At 12:00 UTC on every day-of-week from Monday through Friday
20+
workflow_dispatch:
21+
22+
env:
23+
UPSTREAM_REPOSITORY: obytes/react-native-template-obytes
24+
DIFF_EXCLUDED_ROUTES: |
25+
ios
26+
android
27+
28+
jobs:
29+
sync:
30+
runs-on: ubuntu-latest
31+
permissions:
32+
actions: write
33+
contents: write
34+
pull-requests: write
35+
36+
steps:
37+
- name: Checkout template repository
38+
uses: actions/checkout@v3
39+
with:
40+
fetch-depth: 0
41+
- name: Get latest release of upstream from GitHub
42+
run: |
43+
echo "UPSTREAM_LATEST_VERSION=$(curl -s https://api.github.com/repos/${{ env.UPSTREAM_REPOSITORY }}/releases/latest | jq '.tag_name' | sed 's/\"//g')" >> $GITHUB_ENV
44+
- name: Check if branch already exists
45+
run: |
46+
git fetch origin
47+
BRANCH_NAME=update-upstream-${{ env.UPSTREAM_LATEST_VERSION }}
48+
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
49+
git branch -r | grep -q "origin/$BRANCH_NAME" && echo "BRANCH_EXISTS=true" >> $GITHUB_ENV || echo "BRANCH_EXISTS=false" >> $GITHUB_ENV
50+
- name: Check if PR already exists
51+
run: |
52+
prs=$(gh pr list \
53+
--head "$BRANCH_NAME" \
54+
--json title \
55+
--jq 'length')
56+
if ((prs > 0)); then
57+
echo "PR_EXISTS=true" >> $GITHUB_ENV
58+
else
59+
echo "PR_EXISTS=false" >> $GITHUB_ENV
60+
fi
61+
env:
62+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
63+
- name: Checkout latest release of upstream
64+
if: ${{ env.BRANCH_EXISTS == 'false' }}
65+
run: |
66+
git remote add upstream https://github.com/${{ env.UPSTREAM_REPOSITORY }}.git
67+
git fetch upstream $UPSTREAM_LATEST_VERSION
68+
- name: Merge latest release of upstream
69+
if: ${{ env.BRANCH_EXISTS == 'false' }}
70+
run: |
71+
UPSTREAM_LATEST_VERSION_HASH=$(git rev-parse --short FETCH_HEAD)
72+
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
73+
git config --global user.name "github-actions[bot]"
74+
git merge $UPSTREAM_LATEST_VERSION_HASH
75+
for route in $DIFF_EXCLUDED_ROUTES; do
76+
git reset -- $route
77+
done
78+
continue-on-error: true
79+
- name: Commit and push changes to the update branch
80+
if: ${{ env.BRANCH_EXISTS == 'false' }}
81+
run: |
82+
git add .
83+
git commit -m "chore: update upstream to ${{ env.UPSTREAM_LATEST_VERSION }}"
84+
git checkout -b ${{ env.BRANCH_NAME }}
85+
git push origin ${{ env.BRANCH_NAME }}
86+
git remote rm upstream
87+
echo "BRANCH_EXISTS=true" >> $GITHUB_ENV
88+
- name: 🎉 Create PR with changes
89+
if: ${{ env.BRANCH_EXISTS == 'true' && env.PR_EXISTS == 'false' }}
90+
run: |
91+
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 }}
92+
env:
93+
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)