|
| 1 | +name: Sync CMP Directories |
| 2 | +on: |
| 3 | + workflow_dispatch: |
| 4 | + inputs: |
| 5 | + upstream: |
| 6 | + description: 'Upstream repository to sync directories from' |
| 7 | + default: 'https://github.com/openMF/kmp-project-template.git' |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + schedule: |
| 11 | + - cron: '0 0 * * 1' |
| 12 | + |
| 13 | +jobs: |
| 14 | + sync-directories: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + permissions: |
| 17 | + contents: write |
| 18 | + pull-requests: write |
| 19 | + |
| 20 | + steps: |
| 21 | + - name: Checkout repository |
| 22 | + uses: actions/checkout@v4 |
| 23 | + with: |
| 24 | + fetch-depth: 0 |
| 25 | + ref: dev |
| 26 | + |
| 27 | + - name: Setup Git config |
| 28 | + run: | |
| 29 | + git config --global user.name "github-actions[bot]" |
| 30 | + git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 31 | +
|
| 32 | + - name: Add upstream remote and fetch |
| 33 | + run: | |
| 34 | + git remote add upstream ${{ inputs.upstream }} || true |
| 35 | + git fetch upstream || exit 1 |
| 36 | +
|
| 37 | + - name: Check upstream/dev exists |
| 38 | + run: | |
| 39 | + if ! git rev-parse --verify upstream/dev >/dev/null 2>&1; then |
| 40 | + echo "Error: upstream/dev branch does not exist" |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | +
|
| 44 | + - name: Create and checkout temporary branch |
| 45 | + run: | |
| 46 | + TEMP_BRANCH="temp-sync-branch-${{ github.run_number }}" |
| 47 | + git checkout -b "$TEMP_BRANCH" upstream/dev || exit 1 |
| 48 | + echo "TEMP_BRANCH=$TEMP_BRANCH" >> $GITHUB_ENV |
| 49 | +
|
| 50 | + - name: Sync directories and files |
| 51 | + run: | |
| 52 | + # Declare directories and files to sync |
| 53 | + DIRS=( |
| 54 | + "cmp-android" |
| 55 | + "cmp-desktop" |
| 56 | + "cmp-ios" |
| 57 | + "cmp-web" |
| 58 | + "cmp-shared" |
| 59 | + "core-base" |
| 60 | + "build-logic" |
| 61 | + "fastlane" |
| 62 | + "scripts" |
| 63 | + "config" |
| 64 | + ".github" |
| 65 | + ".run" |
| 66 | + ) |
| 67 | + |
| 68 | + FILES=( |
| 69 | + "Gemfile" |
| 70 | + "Gemfile.lock" |
| 71 | + "ci-prepush.bat" |
| 72 | + "ci-prepush.sh" |
| 73 | + ) |
| 74 | + |
| 75 | + # Define exclusions |
| 76 | + declare -A EXCLUSIONS=( |
| 77 | + ["cmp-android"]="src/main/res dependencies src/main/ic_launcher-playstore.png google-services.json" |
| 78 | + ["cmp-web"]="src/jsMain/resources src/wasmJsMain/resources" |
| 79 | + ["cmp-desktop"]="icons" |
| 80 | + ["cmp-ios"]="iosApp/Assets.xcassets" |
| 81 | + ["root"]="secrets.env" |
| 82 | + ) |
| 83 | + |
| 84 | + # Function to check if path should be excluded |
| 85 | + should_exclude() { |
| 86 | + local dir=$1 |
| 87 | + local path=$2 |
| 88 | + |
| 89 | + # Check for root exclusions |
| 90 | + if [[ "$dir" == "." && -n "${EXCLUSIONS["root"]}" ]]; then |
| 91 | + local root_excluded_paths=(${EXCLUSIONS["root"]}) |
| 92 | + for excluded in "${root_excluded_paths[@]}"; do |
| 93 | + if [[ "$path" == *"$excluded"* ]]; then |
| 94 | + return 0 |
| 95 | + fi |
| 96 | + done |
| 97 | + fi |
| 98 | + |
| 99 | + # Check directory-specific exclusions |
| 100 | + if [[ -n "${EXCLUSIONS[$dir]}" ]]; then |
| 101 | + local excluded_paths=(${EXCLUSIONS[$dir]}) |
| 102 | + for excluded in "${excluded_paths[@]}"; do |
| 103 | + if [[ "$path" == *"$excluded"* ]]; then |
| 104 | + return 0 |
| 105 | + fi |
| 106 | + done |
| 107 | + fi |
| 108 | + return 1 |
| 109 | + } |
| 110 | + |
| 111 | + # Function to preserve excluded paths |
| 112 | + preserve_excluded() { |
| 113 | + local dir=$1 |
| 114 | + if [[ -n "${EXCLUSIONS[$dir]}" ]]; then |
| 115 | + local excluded_paths=(${EXCLUSIONS[$dir]}) |
| 116 | + for excluded in "${excluded_paths[@]}"; do |
| 117 | + local full_path="$dir/$excluded" |
| 118 | + if [[ -e "$full_path" ]]; then |
| 119 | + echo "Preserving excluded path: $full_path" |
| 120 | + local temp_path="temp_excluded/$full_path" |
| 121 | + mkdir -p "$(dirname "$temp_path")" |
| 122 | + cp -r "$full_path" "$(dirname "$temp_path")" |
| 123 | + fi |
| 124 | + done |
| 125 | + fi |
| 126 | + } |
| 127 | + |
| 128 | + # Function to restore excluded paths |
| 129 | + restore_excluded() { |
| 130 | + local dir=$1 |
| 131 | + if [[ -n "${EXCLUSIONS[$dir]}" ]]; then |
| 132 | + local excluded_paths=(${EXCLUSIONS[$dir]}) |
| 133 | + for excluded in "${excluded_paths[@]}"; do |
| 134 | + local full_path="$dir/$excluded" |
| 135 | + local temp_path="temp_excluded/$full_path" |
| 136 | + if [[ -e "$temp_path" ]]; then |
| 137 | + echo "Restoring excluded path: $full_path" |
| 138 | + mkdir -p "$(dirname "$full_path")" |
| 139 | + rm -rf "$full_path" |
| 140 | + cp -r "$temp_path" "$(dirname "$full_path")" |
| 141 | + fi |
| 142 | + done |
| 143 | + fi |
| 144 | + } |
| 145 | + |
| 146 | + # Function to preserve root-level excluded files |
| 147 | + preserve_root_files() { |
| 148 | + if [[ -n "${EXCLUSIONS["root"]}" ]]; then |
| 149 | + local excluded_paths=(${EXCLUSIONS["root"]}) |
| 150 | + for excluded in "${excluded_paths[@]}"; do |
| 151 | + if [[ -e "$excluded" ]]; then |
| 152 | + echo "Preserving root-level excluded file: $excluded" |
| 153 | + mkdir -p "temp_excluded/root" |
| 154 | + cp -r "$excluded" "temp_excluded/root/" |
| 155 | + fi |
| 156 | + done |
| 157 | + fi |
| 158 | + } |
| 159 | + |
| 160 | + # Function to restore root-level excluded files |
| 161 | + restore_root_files() { |
| 162 | + if [[ -n "${EXCLUSIONS["root"]}" ]]; then |
| 163 | + local excluded_paths=(${EXCLUSIONS["root"]}) |
| 164 | + for excluded in "${excluded_paths[@]}"; do |
| 165 | + if [[ -e "temp_excluded/root/$excluded" ]]; then |
| 166 | + echo "Restoring root-level excluded file: $excluded" |
| 167 | + cp -r "temp_excluded/root/$excluded" "./" |
| 168 | + fi |
| 169 | + done |
| 170 | + fi |
| 171 | + } |
| 172 | + |
| 173 | + # Create temp directory for exclusions |
| 174 | + mkdir -p temp_excluded |
| 175 | + |
| 176 | + # Preserve root-level exclusions before sync |
| 177 | + preserve_root_files |
| 178 | + |
| 179 | + # Switch to dev branch |
| 180 | + git checkout dev |
| 181 | + |
| 182 | + # Sync directories |
| 183 | + for dir in "${DIRS[@]}"; do |
| 184 | + if [ ! -d "$dir" ]; then |
| 185 | + echo "Creating $dir..." |
| 186 | + mkdir -p "$dir" |
| 187 | + fi |
| 188 | + |
| 189 | + # Preserve excluded paths before sync |
| 190 | + if [[ -d "$dir" ]]; then |
| 191 | + preserve_excluded "$dir" |
| 192 | + fi |
| 193 | + |
| 194 | + echo "Syncing $dir..." |
| 195 | + git checkout "${{ env.TEMP_BRANCH }}" -- "$dir" || exit 1 |
| 196 | + |
| 197 | + # Restore excluded paths after sync |
| 198 | + restore_excluded "$dir" |
| 199 | + done |
| 200 | + |
| 201 | + # Sync files |
| 202 | + for file in "${FILES[@]}"; do |
| 203 | + dir=$(dirname "$file") |
| 204 | + if ! should_exclude "$dir" "$file"; then |
| 205 | + echo "Syncing $file..." |
| 206 | + git checkout "${{ env.TEMP_BRANCH }}" -- "$file" || true |
| 207 | + else |
| 208 | + echo "Skipping excluded file: $file" |
| 209 | + fi |
| 210 | + done |
| 211 | + |
| 212 | + # Restore root-level excluded files |
| 213 | + restore_root_files |
| 214 | + |
| 215 | + # Cleanup temp directory |
| 216 | + rm -rf temp_excluded |
| 217 | +
|
| 218 | + - name: Clean up temporary branch |
| 219 | + if: always() |
| 220 | + run: git branch -D "${{ env.TEMP_BRANCH }}" || true |
| 221 | + |
| 222 | + - name: Check for changes |
| 223 | + id: check_changes |
| 224 | + run: | |
| 225 | + if [[ -n "$(git status --porcelain)" ]]; then |
| 226 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 227 | + else |
| 228 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 229 | + fi |
| 230 | +
|
| 231 | + - name: Create Pull Request |
| 232 | + if: steps.check_changes.outputs.has_changes == 'true' |
| 233 | + uses: peter-evans/create-pull-request@v7 |
| 234 | + with: |
| 235 | + token: ${{ secrets.PAT_TOKEN }} |
| 236 | + commit-message: "chore: Sync directories and files from upstream" |
| 237 | + title: "chore: Sync directories and files from upstream" |
| 238 | + body: | |
| 239 | + Automated sync of directories and files from upstream repository. |
| 240 | + |
| 241 | + Changes included in this sync: |
| 242 | + |
| 243 | + Directories: |
| 244 | + - cmp-android (excluding src/main/res, dependencies, ic_launcher-playstore.png, google-services.json) |
| 245 | + - cmp-desktop (excluding icons) |
| 246 | + - cmp-ios (excluding iosApp/Assets.xcassets) |
| 247 | + - cmp-web (excluding src/jsMain/resources, src/wasmJsMain/resources) |
| 248 | + - cmp-shared |
| 249 | + - build-logic |
| 250 | + - fastlane |
| 251 | + - scripts |
| 252 | + - config |
| 253 | + - .github |
| 254 | + - .run |
| 255 | + |
| 256 | + Files: |
| 257 | + - Gemfile |
| 258 | + - Gemfile.lock |
| 259 | + - ci-prepush.bat |
| 260 | + - ci-prepush.sh |
| 261 | + |
| 262 | + Root-level exclusions: |
| 263 | + - secrets.env |
| 264 | + |
| 265 | + Workflow run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 266 | + branch: sync-dirs-${{ github.run_number }} |
| 267 | + delete-branch: true |
| 268 | + labels: | |
| 269 | + sync |
| 270 | + automated pr |
| 271 | + base: dev |
0 commit comments