Skip to content

Commit

Permalink
feat: upstream sync workflow V2
Browse files Browse the repository at this point in the history
  • Loading branch information
asdolo committed Dec 2, 2024
1 parent 9ed484a commit 4c6ee6b
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 48 deletions.
42 changes: 27 additions & 15 deletions .github/project-workflows/sync-with-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@

# 🚨 GITHUB SECRETS REQUIRED:
# - UPDATE_FROM_TEMPLATE_PAT: A fine-grained Personal Access Token.
# This token is used to access the project repository and create the Pull Request.
# This token is used to commit and push to the project 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 project repository.
# Set the following Repo permissions:
# - Contents: Read & write (to commit and push the update branch to the project repository)
# - Metadata: Read-only (mandatory by GitHub)
# - Pull requests: Read & write (to create the Pull Request in the project repository)
# - Workflows: Read and write (to create, update and delete workflows in the project repository)
# Make sure to add it to the repo secrets with the name UPDATE_FROM_TEMPLATE_PAT:
# - Go to Repository Settings > Secrets and variables > Actions > New repository secret
Expand All @@ -29,8 +28,14 @@ name: 🔄 Sync with template

on:
schedule:
- cron: '0 12 * * *' # Everyday at 12:00 UTC
- cron: '0 12 * * 1-5' # At 12:00 UTC on every day-of-week from Monday through Friday
workflow_dispatch:
inputs:
template-version:
type: string
description: 'Template release version to sync with (e.g. v1.0.0). Leave empty to sync with the latest release.'
required: false
default: ''

env:
TEMPLATE_REPOSITORY: rootstrap/react-native-template
Expand All @@ -42,6 +47,7 @@ env:
.github/workflows/deploy-docs.yml
.github/workflows/new-template-version.yml
.github/workflows/upstream-to-pr.yml
.github/workflows/sync-with-upstream.yml
README-project.md
jobs:
Expand All @@ -50,6 +56,7 @@ jobs:
permissions:
actions: write
contents: read
pull-requests: write

steps:
- name: Check if Personal Access Token exists
Expand All @@ -68,12 +75,17 @@ jobs:
- name: Get template version used in project from package.json
run: |
echo "PROJECT_TEMPLATE_VERSION=v$(jq -r 'if has("rsMetadata") then .rsMetadata.templateVersion else .osMetadata.initVersion end' project/package.json | sed 's/^.*@//')" >> $GITHUB_ENV
- name: Get latest release of template from GitHub
- name: Set template version to sync with
run: |
echo "TEMPLATE_LATEST_VERSION=$(curl -s https://api.github.com/repos/${{ env.TEMPLATE_REPOSITORY }}/releases/latest | jq '.tag_name' | sed 's/\"//g')" >> $GITHUB_ENV
- name: Check if the template is up to date
if [ -z "${{ inputs.template-version }}" ]; then
TEMPLATE_UPDATE_VERSION=$(curl -s https://api.github.com/repos/${{ env.TEMPLATE_REPOSITORY }}/releases/latest | jq '.tag_name' | sed 's/\"//g')
else
TEMPLATE_UPDATE_VERSION=${{ inputs.template-version }}
fi
echo "TEMPLATE_UPDATE_VERSION=$TEMPLATE_UPDATE_VERSION" >> $GITHUB_ENV
- name: Check if the project is up to date
run: |
if [[ $TEMPLATE_LATEST_VERSION == $PROJECT_TEMPLATE_VERSION ]]; then
if [[ $TEMPLATE_UPDATE_VERSION == $PROJECT_TEMPLATE_VERSION ]]; then
echo "Template is up to date"
cd project
gh run cancel ${{ github.run_id }}
Expand All @@ -85,7 +97,7 @@ jobs:
run: |
cd project
git fetch origin
BRANCH_NAME=update-template-${{ env.TEMPLATE_LATEST_VERSION }}
BRANCH_NAME=update-template-${{ env.TEMPLATE_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
Expand All @@ -101,17 +113,17 @@ jobs:
echo "PR_EXISTS=false" >> $GITHUB_ENV
fi
env:
GITHUB_TOKEN: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Install dependencies
if: ${{ env.BRANCH_EXISTS == 'false' }}
run: |
sudo apt install wiggle
- name: Checkout latest release of template
- name: Checkout update release of template
if: ${{ env.BRANCH_EXISTS == 'false' }}
uses: actions/checkout@v3
with:
repository: ${{ env.TEMPLATE_REPOSITORY }}
ref: ${{ env.TEMPLATE_LATEST_VERSION }}
ref: ${{ env.TEMPLATE_UPDATE_VERSION }}
fetch-depth: 0
path: react-native-template
- name: Get diff between latest release and used release
Expand All @@ -128,7 +140,7 @@ jobs:
run: |
cd project
jq 'del(.osMetadata)' package.json > tmp.json && mv tmp.json package.json
PLAIN_VERSION=${TEMPLATE_LATEST_VERSION#v}
PLAIN_VERSION=${TEMPLATE_UPDATE_VERSION#v}
jq --arg version $PLAIN_VERSION '.rsMetadata.templateVersion = $version' package.json > tmp.json && mv tmp.json package.json
- name: Apply diff to project repository
if: ${{ env.BRANCH_EXISTS == 'false' }}
Expand Down Expand Up @@ -156,13 +168,13 @@ jobs:
git config --global user.name "github-actions[bot]"
git checkout -b ${{ env.BRANCH_NAME }}
git add .
git commit -m "chore: update template to ${{ env.TEMPLATE_LATEST_VERSION }}"
git commit -m "chore: update template to ${{ env.TEMPLATE_UPDATE_VERSION }}"
git push origin ${{ env.BRANCH_NAME }}
echo "BRANCH_EXISTS=true" >> $GITHUB_ENV
- name: 🎉 Create PR with changes
if: ${{ env.BRANCH_EXISTS == 'true' && env.PR_EXISTS == 'false' }}
run: |
cd project
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 }}
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 }}
env:
GITHUB_TOKEN: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
127 changes: 127 additions & 0 deletions .github/workflows/sync-with-upstream.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# 🔗 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: |
ios
android
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@v3
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.GITHUB_TOKEN }}
- 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 commit -m "chore: update upstream to ${{ env.UPSTREAM_UPDATE_VERSION }}"
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.GITHUB_TOKEN }}
22 changes: 0 additions & 22 deletions .github/workflows/upstream-to-pr.yml

This file was deleted.

13 changes: 2 additions & 11 deletions cli/setup-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,6 @@ const updateProjectConfig = async (projectName) => {
const updateGitHubWorkflows = (projectName) => {
// Update useful workflows
projectFilesManager.replaceFilesContent([
{
fileName: '.github/workflows/upstream-to-pr.yml',
replacements: [
{
searchValue: UPSTREAM_REPOSITORY,
replaceValue: TEMPLATE_REPOSITORY,
},
],
},
{
fileName: '.github/workflows/new-template-version.yml',
replacements: [
Expand Down Expand Up @@ -146,8 +137,8 @@ const updateGitHubWorkflows = (projectName) => {
},
]);

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

// Enable sync with template workflow
projectFilesManager.renameFiles([
Expand Down

0 comments on commit 4c6ee6b

Please sign in to comment.