|
| 1 | +# 🔗 Links: |
| 2 | +# Source file: https://github.com/rootstrap/react-native-template/blob/master/.github/project-workflows/sync-with-template.yml |
| 3 | + |
| 4 | +# ✍️ Description: |
| 5 | +# This workflow is used to keep the project up to date with the latest version of the template. |
| 6 | + |
| 7 | +# 🚨 GITHUB SECRETS REQUIRED: |
| 8 | +# - UPDATE_FROM_TEMPLATE_PAT: A fine-grained Personal Access Token. |
| 9 | +# This token is used to commit and push to the project 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 project repository. |
| 12 | +# Set the following Repo permissions: |
| 13 | +# - Contents: Read & write (to commit and push the update branch to the project repository) |
| 14 | +# - Metadata: Read-only (mandatory by GitHub) |
| 15 | +# - Workflows: Read and write (to create, update and delete workflows in the project 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 | +# - TEMPLATE_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 template |
| 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 | + 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: '' |
| 39 | + |
| 40 | +env: |
| 41 | + TEMPLATE_REPOSITORY: rootstrap/react-native-template |
| 42 | + DIFF_EXCLUDED_ROUTES: | |
| 43 | + cli |
| 44 | + docs |
| 45 | + ios |
| 46 | + android |
| 47 | + .github/workflows/deploy-docs.yml |
| 48 | + .github/workflows/new-template-version.yml |
| 49 | + .github/workflows/upstream-to-pr.yml |
| 50 | + .github/workflows/sync-with-upstream.yml |
| 51 | + README-project.md |
| 52 | +
|
| 53 | +jobs: |
| 54 | + sync: |
| 55 | + runs-on: ubuntu-latest |
| 56 | + permissions: |
| 57 | + actions: write |
| 58 | + contents: read |
| 59 | + pull-requests: write |
| 60 | + |
| 61 | + steps: |
| 62 | + - name: Check if Personal Access Token exists |
| 63 | + env: |
| 64 | + PAT: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }} |
| 65 | + if: env.PAT == '' |
| 66 | + run: | |
| 67 | + echo "UPDATE_FROM_TEMPLATE_PAT secret not found. Please create a fine-grained Personal Access Token following the instructions in the workflow file." |
| 68 | + exit 1 |
| 69 | + - name: Checkout project repository |
| 70 | + uses: actions/checkout@v3 |
| 71 | + with: |
| 72 | + fetch-depth: 0 |
| 73 | + path: project |
| 74 | + token: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }} |
| 75 | + - name: Get template version used in project from package.json |
| 76 | + run: | |
| 77 | + echo "PROJECT_TEMPLATE_VERSION=v$(jq -r 'if has("rsMetadata") then .rsMetadata.templateVersion else .osMetadata.initVersion end' project/package.json | sed 's/^.*@//')" >> $GITHUB_ENV |
| 78 | + - name: Set template version to sync with |
| 79 | + run: | |
| 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 |
| 87 | + run: | |
| 88 | + if [[ $TEMPLATE_UPDATE_VERSION == $PROJECT_TEMPLATE_VERSION ]]; then |
| 89 | + echo "Template is up to date" |
| 90 | + cd project |
| 91 | + gh run cancel ${{ github.run_id }} |
| 92 | + gh run watch ${{ github.run_id }} |
| 93 | + fi |
| 94 | + env: |
| 95 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 96 | + - name: Check if branch already exists |
| 97 | + run: | |
| 98 | + cd project |
| 99 | + git fetch origin |
| 100 | + BRANCH_NAME=update-template-${{ env.TEMPLATE_UPDATE_VERSION }} |
| 101 | + echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV |
| 102 | + git branch -r | grep -q "origin/$BRANCH_NAME" && echo "BRANCH_EXISTS=true" >> $GITHUB_ENV || echo "BRANCH_EXISTS=false" >> $GITHUB_ENV |
| 103 | + - name: Check if PR already exists |
| 104 | + run: | |
| 105 | + cd project |
| 106 | + prs=$(gh pr list \ |
| 107 | + --head "$BRANCH_NAME" \ |
| 108 | + --json title \ |
| 109 | + --jq 'length') |
| 110 | + if ((prs > 0)); then |
| 111 | + echo "PR_EXISTS=true" >> $GITHUB_ENV |
| 112 | + else |
| 113 | + echo "PR_EXISTS=false" >> $GITHUB_ENV |
| 114 | + fi |
| 115 | + env: |
| 116 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 117 | + - name: Install dependencies |
| 118 | + if: ${{ env.BRANCH_EXISTS == 'false' }} |
| 119 | + run: | |
| 120 | + sudo apt install wiggle |
| 121 | + - name: Checkout update release of template |
| 122 | + if: ${{ env.BRANCH_EXISTS == 'false' }} |
| 123 | + uses: actions/checkout@v3 |
| 124 | + with: |
| 125 | + repository: ${{ env.TEMPLATE_REPOSITORY }} |
| 126 | + ref: ${{ env.TEMPLATE_UPDATE_VERSION }} |
| 127 | + fetch-depth: 0 |
| 128 | + path: react-native-template |
| 129 | + - name: Get diff between latest release and used release |
| 130 | + if: ${{ env.BRANCH_EXISTS == 'false' }} |
| 131 | + run: | |
| 132 | + EXCLUDED_ROUTES="" |
| 133 | + for route in $DIFF_EXCLUDED_ROUTES; do |
| 134 | + EXCLUDED_ROUTES="$EXCLUDED_ROUTES ':(exclude)$route'" |
| 135 | + done |
| 136 | + cd react-native-template |
| 137 | + git diff ${{ env.PROJECT_TEMPLATE_VERSION }} -- . $(echo $EXCLUDED_ROUTES | xargs) > ../update.patch |
| 138 | + - name: Update template version in package.json |
| 139 | + if: ${{ env.BRANCH_EXISTS == 'false' }} |
| 140 | + run: | |
| 141 | + cd project |
| 142 | + jq 'del(.osMetadata)' package.json > tmp.json && mv tmp.json package.json |
| 143 | + PLAIN_VERSION=${TEMPLATE_UPDATE_VERSION#v} |
| 144 | + jq --arg version $PLAIN_VERSION '.rsMetadata.templateVersion = $version' package.json > tmp.json && mv tmp.json package.json |
| 145 | + - name: Apply diff to project repository |
| 146 | + if: ${{ env.BRANCH_EXISTS == 'false' }} |
| 147 | + run: | |
| 148 | + cd project |
| 149 | + git apply --reject ../update.patch |
| 150 | + continue-on-error: true |
| 151 | + - name: Solve conflicts with wiggle |
| 152 | + if: ${{ env.BRANCH_EXISTS == 'false' }} |
| 153 | + run: | |
| 154 | + cd project |
| 155 | + find . -iname '*.rej' -exec sh -c 'printf "%s\n" "${0%.*}"' {} ';' | xargs -I _ wiggle --replace --merge _ _.rej |
| 156 | + continue-on-error: true |
| 157 | + - name: Remove wiggle's backup and .rej files |
| 158 | + if: ${{ env.BRANCH_EXISTS == 'false' }} |
| 159 | + run: | |
| 160 | + cd project |
| 161 | + find . -not -path './.git/*' -type f -name '*.porig' -delete |
| 162 | + find . -not -path './.git/*' -type f -name '*.rej' -delete |
| 163 | + - name: Commit and push changes to the update branch |
| 164 | + if: ${{ env.BRANCH_EXISTS == 'false' }} |
| 165 | + run: | |
| 166 | + cd project |
| 167 | + git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 168 | + git config --global user.name "github-actions[bot]" |
| 169 | + git checkout -b ${{ env.BRANCH_NAME }} |
| 170 | + git add . |
| 171 | + git commit -m "chore: update template to ${{ env.TEMPLATE_UPDATE_VERSION }}" |
| 172 | + git push origin ${{ env.BRANCH_NAME }} |
| 173 | + echo "BRANCH_EXISTS=true" >> $GITHUB_ENV |
| 174 | + - name: 🎉 Create PR with changes |
| 175 | + if: ${{ env.BRANCH_EXISTS == 'true' && env.PR_EXISTS == 'false' }} |
| 176 | + run: | |
| 177 | + cd project |
| 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 }} |
| 179 | + env: |
| 180 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments