|
| 1 | +# Workflow to sync local repository with latest version of DUA template. |
| 2 | +# |
| 3 | +# Roberto Masocco <r.masocco@dotxautomation.com> |
| 4 | +# |
| 5 | +# March 9, 2026 |
| 6 | + |
| 7 | +# Copyright 2026 dotX Automation s.r.l. |
| 8 | +# |
| 9 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | +# you may not use this file except in compliance with the License. |
| 11 | +# You may obtain a copy of the License at |
| 12 | +# |
| 13 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +# |
| 15 | +# Unless required by applicable law or agreed to in writing, software |
| 16 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | +# See the License for the specific language governing permissions and |
| 19 | +# limitations under the License. |
| 20 | + |
| 21 | +name: Sync DUA template |
| 22 | + |
| 23 | +on: |
| 24 | + # Run every day at 00:01 |
| 25 | + schedule: |
| 26 | + - cron: "1 0 * * *" |
| 27 | + |
| 28 | + # Run when base branch is updated |
| 29 | + push: |
| 30 | + branches: |
| 31 | + - master |
| 32 | + |
| 33 | + # Run when manually triggered |
| 34 | + workflow_dispatch: |
| 35 | + |
| 36 | +env: |
| 37 | + BASE_BRANCH: master |
| 38 | + HEAD_BRANCH: chore/sync-dua-template |
| 39 | + GIT_AUTHOR_NAME: ${{ github.repository_owner }} |
| 40 | + GIT_AUTHOR_EMAIL: ${{ github.repository_owner }}@users.noreply.github.com |
| 41 | + REPO_TEMPLATE: dotX-Automation/dua-template |
| 42 | + THIS_REPO: ${{ github.repository }} |
| 43 | + |
| 44 | +jobs: |
| 45 | + sync-dua-template: |
| 46 | + # Do not run on the template repository itself |
| 47 | + if: github.repository != 'dotX-Automation/dua-template' |
| 48 | + |
| 49 | + name: Sync DUA template |
| 50 | + runs-on: ubuntu-latest |
| 51 | + continue-on-error: false |
| 52 | + |
| 53 | + steps: |
| 54 | + # Configure Git to reduce warnings |
| 55 | + - name: Configure Git to reduce warnings |
| 56 | + run: git config --global init.defaultBranch master |
| 57 | + |
| 58 | + # Clone the template repository (we need the full history of this) |
| 59 | + - name: Check out template repository |
| 60 | + uses: actions/checkout@v6 |
| 61 | + with: |
| 62 | + repository: ${{ env.REPO_TEMPLATE }} |
| 63 | + ref: 'master' |
| 64 | + token: ${{ github.token }} |
| 65 | + path: ${{ env.REPO_TEMPLATE }} |
| 66 | + fetch-depth: 0 |
| 67 | + |
| 68 | + # Clone the target repository |
| 69 | + - name: Check out ${{ github.repository }} |
| 70 | + uses: actions/checkout@v6 |
| 71 | + with: |
| 72 | + repository: ${{ github.repository }} |
| 73 | + ref: ${{ env.BASE_BRANCH }} |
| 74 | + token: ${{ github.token }} |
| 75 | + path: ${{ github.repository }} |
| 76 | + |
| 77 | + # Checkout a branch |
| 78 | + - name: Create branch in ${{ env.THIS_REPO }} |
| 79 | + run: | |
| 80 | + git -C "${THIS_REPO}" fetch origin "${HEAD_BRANCH}" || true |
| 81 | + git -C "${THIS_REPO}" branch -a |
| 82 | + git -C "${THIS_REPO}" checkout -B "${HEAD_BRANCH}" \ |
| 83 | + "remotes/origin/${HEAD_BRANCH}" || \ |
| 84 | + git -C "${THIS_REPO}" checkout -b "${HEAD_BRANCH}" |
| 85 | +
|
| 86 | + # Parse template contents and synchronize changes |
| 87 | + - name: Parse template contents and synchronize changes |
| 88 | + run: | |
| 89 | + _files="$(find ${REPO_TEMPLATE} \ |
| 90 | + ! -path "*/.git/*" \ |
| 91 | + ! -path "*/.github/workflows/*" \ |
| 92 | + ! -path "*/.vscode/*" \ |
| 93 | + ! -name ".gitignore" \ |
| 94 | + ! -name "LICENSE" \ |
| 95 | + ! -name "README.md" \ |
| 96 | + -type f \ |
| 97 | + -print)" |
| 98 | + _deleted_files="$(git -C ${REPO_TEMPLATE} log \ |
| 99 | + --diff-filter=D \ |
| 100 | + --name-only \ |
| 101 | + --pretty=format: | grep -v '^$')" |
| 102 | + for _deleted_file in ${_deleted_files}; do |
| 103 | + _file_path="${THIS_REPO}/${_deleted_file#${REPO_TEMPLATE}/}" |
| 104 | + if [[ -f "${_file_path}" ]]; then |
| 105 | + echo "INFO: Deleting ${_file_path}" |
| 106 | + rm -f "${_file_path}" |
| 107 | + fi |
| 108 | + done |
| 109 | + for _file in ${_files}; do |
| 110 | + _src="${_file}" |
| 111 | + _dst="${THIS_REPO}/${_file#${REPO_TEMPLATE}/}" |
| 112 | + _dst="${_dst%/*}/" |
| 113 | + mkdir -p "${_dst}" |
| 114 | + echo "INFO: Copying '${_src}' to '${_dst}'" |
| 115 | + cp "${_src}" "${_dst}" |
| 116 | + done |
| 117 | + git -C "${THIS_REPO}" diff |
| 118 | +
|
| 119 | + # Commit changes, push, and create a pull request |
| 120 | + - name: Push changes and create a pull request |
| 121 | + env: |
| 122 | + GH_TOKEN: ${{ github.token }} |
| 123 | + run: | |
| 124 | + if [[ -n $(git -C "${THIS_REPO}" status --porcelain) ]]; then |
| 125 | + git -C ${THIS_REPO} config user.name "${GIT_AUTHOR_NAME}" |
| 126 | + git -C ${THIS_REPO} config user.email "${GIT_AUTHOR_EMAIL}" |
| 127 | + git -C ${THIS_REPO} add . |
| 128 | + git -C ${THIS_REPO} commit -m "chore: sync to dua-template@${{ github.sha }}" |
| 129 | + if [[ -n $(git -C ${THIS_REPO} branch -r --list "origin/${HEAD_BRANCH}") ]]; then |
| 130 | + git -C ${THIS_REPO} push |
| 131 | + echo "INFO: Pushed changes to branch ${HEAD_BRANCH}" |
| 132 | + else |
| 133 | + git -C ${THIS_REPO} push -u origin "${HEAD_BRANCH}" |
| 134 | + pushd ${THIS_REPO} |
| 135 | + gh pr create \ |
| 136 | + --base "${BASE_BRANCH}" \ |
| 137 | + --head "${HEAD_BRANCH}" \ |
| 138 | + --title "Sync to dua-template@${{ github.sha }}" \ |
| 139 | + --body "Sync to dua-template@${{ github.sha }}." |
| 140 | + popd |
| 141 | + echo "INFO: Created new pull request from ${HEAD_BRANCH} to ${BASE_BRANCH}" |
| 142 | + fi |
| 143 | + else |
| 144 | + echo "INFO: No changes to commit" |
| 145 | + fi |
0 commit comments