|
| 1 | +--- |
| 2 | +# |
| 3 | +# - Run this workflow to pull changes from the template repository. |
| 4 | +# |
| 5 | +# - Clone this repository. Check out a branch |
| 6 | +# - Copy files from the template onto this clone |
| 7 | +# - Push the branch to this repository |
| 8 | +# - Create a pull request in this repository |
| 9 | +# |
| 10 | +name: Sync changes from template |
| 11 | + |
| 12 | +# TODO: |
| 13 | +# - Switch to gh. Check https://github.com/cli/cli/issues/297 for updates |
| 14 | + |
| 15 | +on: |
| 16 | + # Run at 0517 UTC each Friday |
| 17 | + schedule: |
| 18 | + - cron: "17 5 * * 5" |
| 19 | + |
| 20 | + # Run when this file changes |
| 21 | + push: |
| 22 | + paths: |
| 23 | + - .github/workflows/sync-from-template.yaml |
| 24 | + |
| 25 | + # Run when manually triggered |
| 26 | + workflow_dispatch: |
| 27 | + |
| 28 | +env: |
| 29 | + MAIN_BRANCH: main |
| 30 | + BASE_BRANCH: develop |
| 31 | + HEAD_BRANCH: chore/sync-from-template |
| 32 | + GIT_AUTHOR_NAME: ${{ github.repository_owner }} |
| 33 | + GIT_AUTHOR_EMAIL: ${{ github.repository_owner }}@users.noreply.github.com |
| 34 | + REPO_TEMPLATE: RISC-OS-Community/StandardRepoTemplate |
| 35 | + THIS_REPO: ${{ github.repository }} |
| 36 | + |
| 37 | +jobs: |
| 38 | + sync-from-template: |
| 39 | + # Do not run on the template repository itself |
| 40 | + if: github.repository != 'RISC-OS-Community/StandardRepoTemplate' |
| 41 | + name: Sync changes from RISC-OS-Community/StandardRepoTemplate |
| 42 | + runs-on: ubuntu-latest |
| 43 | + continue-on-error: true |
| 44 | + |
| 45 | + steps: |
| 46 | + # Clone the template repository |
| 47 | + - name: Check out template repository |
| 48 | + uses: actions/checkout@v2 |
| 49 | + with: |
| 50 | + repository: ${{ env.REPO_TEMPLATE }} |
| 51 | + token: ${{ github.token }} |
| 52 | + path: ${{ env.REPO_TEMPLATE }} |
| 53 | + ref: main |
| 54 | + |
| 55 | + # Configure git globals |
| 56 | + #- name: Configure git globals |
| 57 | + # run: | |
| 58 | + # git config --global init.defaultBranch ${MAIN_BRANCH} |
| 59 | + |
| 60 | + # Clone the target repository. Check out a branch |
| 61 | + - name: Check out ${{ github.repository }} |
| 62 | + uses: actions/checkout@v2 |
| 63 | + with: |
| 64 | + repository: ${{ github.repository }} |
| 65 | + token: ${{ github.token }} |
| 66 | + path: ${{ github.repository }} |
| 67 | + ref: main |
| 68 | + - name: Create branch in ${{ env.THIS_REPO }} |
| 69 | + run: | |
| 70 | + git -C "${THIS_REPO}" fetch origin "${HEAD_BRANCH}" || true |
| 71 | + git -C "${THIS_REPO}" branch -a |
| 72 | + git -C "${THIS_REPO}" checkout -B "${HEAD_BRANCH}" \ |
| 73 | + "remotes/origin/${HEAD_BRANCH}" || \ |
| 74 | + git -C "${THIS_REPO}" checkout -b "${HEAD_BRANCH}" |
| 75 | +
|
| 76 | + # Copy files from the template onto the target clone |
| 77 | + - name: Copy template contents |
| 78 | + run: | |
| 79 | + _files="$(find ${REPO_TEMPLATE} \ |
| 80 | + ! -path "*/.git/*" \ |
| 81 | + ! -path "*/.github/workflows/*" \ |
| 82 | + ! -name ".gitignore" \ |
| 83 | + ! -name "README.md" \ |
| 84 | + -type f \ |
| 85 | + -print)" |
| 86 | + for _file in ${_files}; do |
| 87 | + _src="${_file}" |
| 88 | + _dst="${THIS_REPO}/${_file#${REPO_TEMPLATE}/}" |
| 89 | + # TODO: Find a more robust / elegant way to get this :point_down: |
| 90 | + _dst="${_dst%/*}/" |
| 91 | + mkdir -p "${_dst}" |
| 92 | + echo "INFO: Copy '${_src}' to '${_dst}'." |
| 93 | + cp "${_src}" "${_dst}" |
| 94 | + done |
| 95 | + git -C "${THIS_REPO}" diff |
| 96 | +
|
| 97 | + # Commit changes, if there are any |
| 98 | + - name: Commit changes, if any |
| 99 | + run: | |
| 100 | + git -C ${THIS_REPO} config user.name "${GIT_AUTHOR_NAME}" |
| 101 | + git -C ${THIS_REPO} config \ |
| 102 | + user.email "${GIT_AUTHOR_EMAIL}" |
| 103 | + git -C ${THIS_REPO} add . |
| 104 | + git -C ${THIS_REPO} commit \ |
| 105 | + -m "Sync from template@${{ github.sha }}" |
| 106 | +
|
| 107 | + # Push the branch to the target repository |
| 108 | + - name: Push topic branch |
| 109 | + run: git -C ${THIS_REPO} push -u origin "${HEAD_BRANCH}" |
| 110 | + |
| 111 | + # Create a pull request in the target repository |
| 112 | + - name: Create pull request |
| 113 | + env: |
| 114 | + GITHUB_TOKEN: ${{ github.token }} |
| 115 | + GITHUB_USER: ${{ github.actor }} |
| 116 | + run: | |
| 117 | + pushd ${THIS_REPO} |
| 118 | + hub pull-request \ |
| 119 | + -b "${BASE_BRANCH}" \ |
| 120 | + -h "${HEAD_BRANCH}" \ |
| 121 | + --no-edit \ |
| 122 | + -m "Pull updates from ${REPO_TEMPLATE}" \ |
| 123 | + -m "Pull updates from ${REPO_TEMPLATE}" |
| 124 | + popd |
| 125 | +
|
0 commit comments