|
| 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 | +# - 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_TEMPLATE_PAT: |
| 17 | +# - Go to Repository Settings > Secrets and variables > Actions > New repository secret |
| 18 | +# - Name: UPDATE_FROM_TEMPLATE_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 * * *' # Everyday at 12:00 UTC |
| 32 | + workflow_dispatch: |
| 33 | + |
| 34 | +env: |
| 35 | + UPSTREAM_REPOSITORY: obytes/react-native-template-obytes |
| 36 | + DIFF_EXCLUDED_ROUTES: | |
| 37 | + ios |
| 38 | + android |
| 39 | +
|
| 40 | +jobs: |
| 41 | + sync: |
| 42 | + runs-on: ubuntu-latest |
| 43 | + permissions: |
| 44 | + actions: write |
| 45 | + contents: read |
| 46 | + pull-requests: read |
| 47 | + issues: write |
| 48 | + |
| 49 | + steps: |
| 50 | + - name: Check if Personal Access Token exists |
| 51 | + env: |
| 52 | + PAT: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }} |
| 53 | + if: env.PAT == '' |
| 54 | + run: | |
| 55 | + echo "UPDATE_FROM_TEMPLATE_PAT secret not found. Please create a fine-grained Personal Access Token following the instructions in the workflow file." |
| 56 | + exit 1 |
| 57 | + - name: Checkout template repository |
| 58 | + uses: actions/checkout@v3 |
| 59 | + with: |
| 60 | + fetch-depth: 0 |
| 61 | + token: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }} |
| 62 | + - name: Get latest release of upstream from GitHub |
| 63 | + run: | |
| 64 | + echo "UPSTREAM_LATEST_VERSION=$(curl -s https://api.github.com/repos/${{ env.UPSTREAM_REPOSITORY }}/releases/latest | jq '.tag_name' | sed 's/\"//g')" >> $GITHUB_ENV |
| 65 | + - name: Check if branch already exists |
| 66 | + run: | |
| 67 | + git fetch origin |
| 68 | + BRANCH_NAME=update-upstream-${{ env.UPSTREAM_LATEST_VERSION }} |
| 69 | + echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV |
| 70 | + git branch -r | grep -q "origin/$BRANCH_NAME" && echo "BRANCH_EXISTS=true" >> $GITHUB_ENV || echo "BRANCH_EXISTS=false" >> $GITHUB_ENV |
| 71 | + - name: Check if PR already exists |
| 72 | + run: | |
| 73 | + prs=$(gh pr list \ |
| 74 | + --head "$BRANCH_NAME" \ |
| 75 | + --json title \ |
| 76 | + --jq 'length') |
| 77 | + if ((prs > 0)); then |
| 78 | + echo "PR_EXISTS=true" >> $GITHUB_ENV |
| 79 | + else |
| 80 | + echo "PR_EXISTS=false" >> $GITHUB_ENV |
| 81 | + fi |
| 82 | + env: |
| 83 | + GITHUB_TOKEN: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }} |
| 84 | + - name: Checkout latest release of upstream |
| 85 | + if: ${{ env.BRANCH_EXISTS == 'false' }} |
| 86 | + run: | |
| 87 | + git remote add upstream https://github.com/${{ env.UPSTREAM_REPOSITORY }}.git |
| 88 | + git fetch upstream |
| 89 | + - name: Merge latest release of upstream |
| 90 | + if: ${{ env.BRANCH_EXISTS == 'false' }} |
| 91 | + run: | |
| 92 | + UPSTREAM_LATEST_VERSION_HASH=$(git ls-remote --tags upstream | grep $UPSTREAM_LATEST_VERSION | sed -n 2p | cut -f1) |
| 93 | + git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 94 | + git config --global user.name "github-actions[bot]" |
| 95 | + git merge --no-commit --no-ff $UPSTREAM_LATEST_VERSION_HASH |
| 96 | + for route in $DIFF_EXCLUDED_ROUTES; do |
| 97 | + git reset -- $route |
| 98 | + done |
| 99 | + continue-on-error: true |
| 100 | + - name: Commit and push changes to the update branch |
| 101 | + if: ${{ env.BRANCH_EXISTS == 'false' }} |
| 102 | + run: | |
| 103 | + git checkout -b ${{ env.BRANCH_NAME }} |
| 104 | + git add . |
| 105 | + git commit -m "chore: update upstream to ${{ env.UPSTREAM_LATEST_VERSION }}" |
| 106 | + git push origin ${{ env.BRANCH_NAME }} |
| 107 | + echo "BRANCH_EXISTS=true" >> $GITHUB_ENV |
| 108 | + - name: 🎉 Create PR with changes |
| 109 | + if: ${{ env.BRANCH_EXISTS == 'true' && env.PR_EXISTS == 'false' }} |
| 110 | + run: | |
| 111 | + 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 }} |
| 112 | + env: |
| 113 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments