-
Notifications
You must be signed in to change notification settings - Fork 0
277 lines (244 loc) · 11.4 KB
/
sync-config-common.yml
File metadata and controls
277 lines (244 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#-----------------------------------------------------------------------------------------------------
# sync-config-common.yml Modify test
#-----------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------
# GitHub Actions Workflow: Sync Configuration Files
# Author: dadavidtseng
# Purpose: Automatically sync configuration files from ConfigCommon repository
#-----------------------------------------------------------------------------------------------------
name: Sync Config Files from dadavidtseng/ConfigCommon
# Grant write permissions to allow committing changes back to the repository
permissions:
contents: write
on:
# Triggered automatically when ConfigCommon repo pushes to main
repository_dispatch:
types: [config-common-updated]
workflow_dispatch:
inputs:
# Option to sync .gitignore file
sync_gitignore:
description: 'sync .gitignore'
required: false
default: true
type: boolean
# Source filename for .gitignore in the remote repository
gitignore_source:
description: '.gitignore source file name (e.g., UnrealEngine.gitignore, DaemonEngine.gitignore)'
required: false
default: 'DaemonEngine.gitignore'
type: string
# Option to sync .gitattributes file
sync_gitattributes:
description: 'sync .gitattributes'
required: false
default: true
type: boolean
# Source filename for .gitattributes in the remote repository
gitattributes_source:
description: '.gitattributes source file name (e.g., UnrealEngine.gitattributes, DaemonEngine.gitattributes)'
required: false
default: 'DaemonEngine.gitattributes'
type: string
# Option to sync .editorconfig file
sync_editorconfig:
description: 'sync .editorconfig'
required: false
default: false
type: boolean
# Source filename for .editorconfig in the remote repository
editorconfig_source:
description: '.editorconfig source file name'
required: false
default: '.editorconfig'
type: string
# Option to sync .clang-format file (disabled by default)
sync_clangformat:
description: 'sync .clang-format'
required: false
default: false
type: boolean
# Source filename for .clang-format in the remote repository
clangformat_source:
description: '.clang-format source file name'
required: false
default: '.clang-format'
type: string
# Additional files to sync
# Format: local_file:remote_file, comma-separated
# Examples:
# Single file: README.md:template-readme.md
# Multiple files: LICENSE:MIT-license.txt,.pre-commit-config.yaml:.pre-commit-config.yaml
# Mixed examples: my-config.json:default-config.json,docker-compose.yml:development-docker-compose.yml
# Notes:
# - Colon (:) separates local filename from remote filename
# - Comma (,) separates multiple file pairs
# - No spaces around colons or commas
# - If remote file doesn't exist, it will be skipped
additional_files:
description: 'Additional files (format: local_file:remote_file, comma-separated)'
required: false
default: ''
type: string
#-----------------------------------------------------------------------------------------------------
jobs:
sync-config-files:
runs-on: ubuntu-latest
steps:
# Step 1: Check out the current repository
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
# Step 2: Download and sync config files
- name: Download and sync config files
id: sync_files
run: |
# Initialize array to store file pairs (local:remote)
declare -a FILES=()
# Define sync configuration array
declare -A SYNC_CONFIG=(
["gitignore"]="${{ github.event.inputs.sync_gitignore || 'true' }}"
["gitattributes"]="${{ github.event.inputs.sync_gitattributes || 'true' }}"
["editorconfig"]="${{ github.event.inputs.sync_editorconfig || 'false' }}"
["clangformat"]="${{ github.event.inputs.sync_clangformat || 'false' }}"
)
declare -A SOURCE_CONFIG=(
["gitignore"]="${{ github.event.inputs.gitignore_source || 'DaemonEngine.gitignore' }}"
["gitattributes"]="${{ github.event.inputs.gitattributes_source || 'DaemonEngine.gitattributes' }}"
["editorconfig"]="${{ github.event.inputs.editorconfig_source || '.editorconfig' }}"
["clangformat"]="${{ github.event.inputs.clangformat_source || '.clang-format' }}"
)
declare -A LOCAL_CONFIG=(
["gitignore"]=".gitignore"
["gitattributes"]=".gitattributes"
["editorconfig"]=".editorconfig"
["clangformat"]=".clang-format"
)
# Add files to sync based on configuration
for config_type in "${!SYNC_CONFIG[@]}"; do
if [ "${SYNC_CONFIG[$config_type]}" = "true" ]; then
FILES+=("${LOCAL_CONFIG[$config_type]}:${SOURCE_CONFIG[$config_type]}")
echo "✓ Enabled sync for ${LOCAL_CONFIG[$config_type]} (source: ${SOURCE_CONFIG[$config_type]})"
fi
done
# Process additional files if specified
ADDITIONAL_FILES="${{ github.event.inputs.additional_files }}"
if [ -n "$ADDITIONAL_FILES" ]; then
echo "Processing additional files: $ADDITIONAL_FILES"
IFS=',' read -ra EXTRA_FILES <<< "$ADDITIONAL_FILES"
for file_pair in "${EXTRA_FILES[@]}"; do
# Trim whitespace
file_pair=$(echo "$file_pair" | xargs)
if [[ "$file_pair" == *":"* ]]; then
FILES+=("$file_pair")
echo "✓ Added additional file: $file_pair"
else
echo "⚠ Skipping invalid file format: $file_pair (missing colon separator)"
fi
done
fi
# Configuration for file sync operation
readonly BASE_URL="https://raw.githubusercontent.com/dadavidtseng/ConfigCommon/main"
CHANGED_FILES=""
ANY_CHANGED=false
echo "Starting sync process with ${#FILES[@]} file(s) to process..."
# Process each file pair in the FILES array
for FILE_PAIR in "${FILES[@]}"; do
# Validate file pair format
if [[ ! "$FILE_PAIR" == *":"* ]]; then
echo "⚠ Skipping invalid file pair format: $FILE_PAIR"
continue
fi
# Split local and remote file names using colon as delimiter
IFS=':' read -r LOCAL_FILE REMOTE_FILE <<< "$FILE_PAIR"
# Remove leading and trailing whitespace
LOCAL_FILE=$(echo "$LOCAL_FILE" | xargs)
REMOTE_FILE=$(echo "$REMOTE_FILE" | xargs)
# Validate file names are not empty
if [ -z "$LOCAL_FILE" ] || [ -z "$REMOTE_FILE" ]; then
echo "⚠ Skipping empty file names: '$LOCAL_FILE' : '$REMOTE_FILE'"
continue
fi
echo "📥 Processing $LOCAL_FILE (source: $REMOTE_FILE)"
# Download file with better error handling
TEMP_FILE="${LOCAL_FILE}.temp.$$"
if curl -fsSL --max-time 30 --retry 3 "$BASE_URL/$REMOTE_FILE" -o "$TEMP_FILE"; then
# Verify downloaded file is not empty
if [ -s "$TEMP_FILE" ]; then
echo "✓ Successfully downloaded $REMOTE_FILE"
# Check for changes between existing and downloaded file
if [ -f "$LOCAL_FILE" ]; then
# Compare files byte by byte
if ! cmp -s "$LOCAL_FILE" "$TEMP_FILE"; then
echo "🔄 Changes detected in $LOCAL_FILE"
mv "$TEMP_FILE" "$LOCAL_FILE"
CHANGED_FILES="$CHANGED_FILES $LOCAL_FILE"
ANY_CHANGED=true
else
echo "✓ No changes in $LOCAL_FILE"
rm -f "$TEMP_FILE"
fi
else
# Create new file if it doesn't exist
echo "📝 Creating new file: $LOCAL_FILE"
mv "$TEMP_FILE" "$LOCAL_FILE"
CHANGED_FILES="$CHANGED_FILES $LOCAL_FILE"
ANY_CHANGED=true
fi
else
echo "❌ Downloaded file $REMOTE_FILE is empty, skipping..."
rm -f "$TEMP_FILE"
fi
else
echo "❌ Failed to download $REMOTE_FILE, skipping..."
rm -f "$TEMP_FILE"
fi
done
# Clean up any remaining temporary files
find . -name "*.temp.*" -type f -delete 2>/dev/null || true
# Set output variables for subsequent steps
if [ "$ANY_CHANGED" = true ]; then
echo "changed=true" >> "$GITHUB_OUTPUT"
echo "changed_files=$CHANGED_FILES" >> "$GITHUB_OUTPUT"
echo "📋 Summary: Files changed -$CHANGED_FILES"
else
echo "changed=false" >> "$GITHUB_OUTPUT"
echo "✅ All files are up to date"
fi
# Step 3: Commit and push changes (only if files were changed)
- name: Commit and push changes
if: steps.sync_files.outputs.changed == 'true'
run: |
# Configure git with GitHub Actions bot credentials
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
# Stage all changes
git add .
# Create commit with detailed message
git commit -m "chore: sync config files from dadavidtseng/ConfigCommon
📝 Updated files:${{ steps.sync_files.outputs.changed_files }}
🕒 Sync timestamp: $(date -u '+%Y-%m-%d %H:%M:%S UTC')
🔗 Source: https://github.com/dadavidtseng/ConfigCommon"
# Push changes to repository
git push
# Step 4: Display comprehensive summary
- name: Display sync summary
run: |
echo "==========================================="
echo "🔄 CONFIG SYNC OPERATION COMPLETE"
echo "==========================================="
echo "📅 Timestamp: $(date -u '+%Y-%m-%d %H:%M:%S UTC')"
echo "🔗 Source Repository: dadavidtseng/ConfigCommon"
echo "📂 Target Repository: ${{ github.repository }}"
echo "🌿 Branch: ${{ github.ref_name }}"
echo ""
if [ "${{ steps.sync_files.outputs.changed }}" = "true" ]; then
echo "✅ STATUS: Files Updated and Committed"
echo "📝 Updated Files:${{ steps.sync_files.outputs.changed_files }}"
echo "🔍 Changes have been automatically committed and pushed"
else
echo "✅ STATUS: All Files Up to Date"
echo "🔍 No changes detected - repository is current"
fi
echo "==========================================="