1
- name : Sync Service Folder to Main
1
+ name : Sync Service to Main
2
2
3
3
on :
4
4
push :
5
5
branches :
6
- - services/**
6
+ - ' services/**' # Triggers on any branch starting with services/
7
7
8
8
jobs :
9
9
sync-to-main :
10
+ # Only run if we're not already on main branch
11
+ if : github.ref_name != 'main'
10
12
runs-on : ubuntu-latest
11
-
13
+
12
14
steps :
13
- - name : Checkout source branch
14
- uses : actions/checkout@v4
15
- with :
16
- ref : ${{ github.ref }}
15
+ - name : Checkout repository
16
+ uses : actions/checkout@v4
17
+ with :
18
+ fetch-depth : 0
19
+ token : ${{ secrets.GITHUB_TOKEN }}
20
+
21
+ - name : Configure Git
22
+ run : |
23
+ git config --global user.name 'github-actions[bot]'
24
+ git config --global user.email 'github-actions[bot]@users.noreply.github.com'
25
+
26
+ - name : Extract current service info
27
+ id : service-info
28
+ run : |
29
+ CURRENT_BRANCH="${{ github.ref_name }}"
30
+ SERVICE_PATH="$CURRENT_BRANCH"
31
+ SERVICE_NAME=$(basename "$CURRENT_BRANCH")
32
+
33
+ echo "current_branch=$CURRENT_BRANCH" >> $GITHUB_OUTPUT
34
+ echo "service_path=$SERVICE_PATH" >> $GITHUB_OUTPUT
35
+ echo "service_name=$SERVICE_NAME" >> $GITHUB_OUTPUT
36
+
37
+ echo "🔍 Detected service: $SERVICE_NAME"
38
+ echo "🌿 Current branch: $CURRENT_BRANCH"
39
+ echo "📁 Target path in main: $SERVICE_PATH"
40
+
41
+ - name : Fetch main branch
42
+ run : |
43
+ git fetch origin main:main
44
+
45
+ - name : Create backup of current changes
46
+ run : |
47
+ # Stash any uncommitted changes (just in case)
48
+ git stash push -m "backup-before-sync" || true
17
49
18
- - name : Extract service name from branch
19
- id : extract
20
- run : |
21
- BRANCH_NAME="${GITHUB_REF#refs/heads/}"
22
- SERVICE_NAME="${BRANCH_NAME#services/}"
23
- echo "SERVICE_NAME=$SERVICE_NAME" >> $GITHUB_ENV
24
- echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
50
+ - name : Switch to main and sync service
51
+ run : |
52
+ SERVICE_PATH="${{ steps.service-info.outputs.service_path }}"
53
+ CURRENT_BRANCH="${{ steps.service-info.outputs.current_branch }}"
54
+ SERVICE_NAME="${{ steps.service-info.outputs.service_name }}"
55
+
56
+ echo "🔄 Syncing $SERVICE_NAME to main branch..."
57
+
58
+ # Switch to main branch
59
+ git checkout main
60
+ git pull origin main
61
+
62
+ # Ensure the service directory exists in main
63
+ mkdir -p "$SERVICE_PATH"
64
+
65
+ # Copy the entire service from the current branch to main
66
+ git checkout "$CURRENT_BRANCH" -- "$SERVICE_PATH/" 2>/dev/null || {
67
+ echo "⚠️ Service path $SERVICE_PATH not found in current branch, syncing entire branch content to service path"
68
+ # If the service path doesn't exist in current branch, copy all files to the service path
69
+ git checkout "$CURRENT_BRANCH" -- .
70
+ mkdir -p "$SERVICE_PATH"
71
+ # Move all files except .git to the service path
72
+ find . -maxdepth 1 ! -name '.' ! -name '..' ! -name '.git' ! -name "$SERVICE_PATH" -exec mv {} "$SERVICE_PATH/" \; 2>/dev/null || true
73
+ }
25
74
26
- - name : Checkout main branch
27
- uses : actions/checkout@v4
28
- with :
29
- ref : main
30
- path : main_branch
75
+ - name : Check for changes and commit
76
+ id : commit-changes
77
+ run : |
78
+ SERVICE_PATH="${{ steps.service-info.outputs.service_path }}"
79
+ CURRENT_BRANCH="${{ steps.service-info.outputs.current_branch }}"
80
+ SERVICE_NAME="${{ steps.service-info.outputs.service_name }}"
81
+
82
+ # Stage all changes in the service path
83
+ git add "$SERVICE_PATH/"
84
+
85
+ # Check if there are staged changes
86
+ if git diff --staged --quiet; then
87
+ echo "has_changes=false" >> $GITHUB_OUTPUT
88
+ echo "ℹ️ No changes detected for $SERVICE_NAME"
89
+ else
90
+ echo "has_changes=true" >> $GITHUB_OUTPUT
91
+
92
+ # Get the latest commit info from the service branch
93
+ COMMIT_MESSAGE=$(git log -1 --pretty=format:"%s" "$CURRENT_BRANCH")
94
+ COMMIT_HASH=$(git log -1 --pretty=format:"%h" "$CURRENT_BRANCH")
95
+ COMMIT_AUTHOR=$(git log -1 --pretty=format:"%an" "$CURRENT_BRANCH")
96
+
97
+ # Create a descriptive commit message
98
+ SYNC_MESSAGE="🚀 Auto-sync $SERVICE_NAME service ($COMMIT_HASH)
31
99
32
- - name : Copy updated service folder to main branch
33
- run : |
34
- SOURCE_PATH=/home/runner/work/Harmona/Harmona
35
- DEST_PATH=main_branch/services/${{ env.SERVICE_NAME }}/
100
+ Original commit : $COMMIT_MESSAGE
101
+ Author : $COMMIT_AUTHOR
102
+ Service : $SERVICE_NAME
103
+ Branch : $CURRENT_BRANCH → main
104
+ Path : $SERVICE_PATH/"
105
+
106
+ # Commit the changes
107
+ git commit -m "$SYNC_MESSAGE"
108
+
109
+ echo "✅ Committed changes for $SERVICE_NAME"
110
+ echo "📝 Original commit : $COMMIT_MESSAGE"
111
+ fi
36
112
37
- echo "Branch: ${{ env.BRANCH_NAME }}"
38
- echo "Service: ${{ env.SERVICE_NAME }}"
39
- echo "Source: $SOURCE_PATH"
40
- echo "Destination: $DEST_PATH"
113
+ - name : Push to main
114
+ if : steps.commit-changes.outputs.has_changes == 'true'
115
+ run : |
116
+ SERVICE_NAME="${{ steps.service-info.outputs.service_name }}"
117
+
118
+ echo "⬆️ Pushing $SERVICE_NAME changes to main..."
119
+ git push origin main
120
+ echo "🎉 Successfully synced $SERVICE_NAME to main branch!"
41
121
42
- if [ -d "$SOURCE_PATH" ]; then
43
- echo "✅ Found source folder: $SOURCE_PATH"
44
- echo "📁 Files and directories in $SOURCE_PATH:"
45
- ls -la "$SOURCE_PATH"
46
- echo "🔍 Tree structure of $SOURCE_PATH:"
47
- find "$SOURCE_PATH" -type f | head -20
48
- rm -rf "$DEST_PATH"
49
- mkdir -p "$DEST_PATH"
50
- cp -r "$SOURCE_PATH"/. "$DEST_PATH"
51
- echo "✅ Copy completed"
52
- else
53
- echo "❌ Folder $SOURCE_PATH not found in branch."
54
- echo "Available directories in current working directory:"
55
- ls -la /home/runner/work/Harmona/Harmona
56
- echo "Looking for services directory:"
57
- find . -name "services" -type d
58
- echo "All directories containing 'service' or similar:"
59
- find . -name "*service*" -type d
60
- exit 1
61
- fi
122
+ - name : Restore original branch
123
+ run : |
124
+ CURRENT_BRANCH="${{ steps.service-info.outputs.current_branch }}"
125
+ git checkout "$CURRENT_BRANCH"
126
+
127
+ # Restore any stashed changes
128
+ git stash pop || true
62
129
63
- - name : Commit and push to main
64
- run : |
65
- cd main_branch
66
- git config user.name "github-actions"
67
- git config user.email "github-actions@github.com"
68
- git add services/${{ env.SERVICE_NAME }}
69
- git commit -m "Sync ${{ github.ref_name }} → main/services/${{ env.SERVICE_NAME }}" || echo "No changes to commit"
70
- git push origin main
130
+ - name : Summary
131
+ run : |
132
+ SERVICE_NAME="${{ steps.service-info.outputs.service_name }}"
133
+ SERVICE_PATH="${{ steps.service-info.outputs.service_path }}"
134
+ CURRENT_BRANCH="${{ steps.service-info.outputs.current_branch }}"
135
+ HAS_CHANGES="${{ steps.commit-changes.outputs.has_changes }}"
136
+
137
+ echo "📊 SYNC SUMMARY"
138
+ echo "==============="
139
+ echo "🏷️ Service: $SERVICE_NAME"
140
+ echo "🌿 Branch: $CURRENT_BRANCH"
141
+ echo "📂 Path: $SERVICE_PATH"
142
+ echo "🔄 Changes: $([ "$HAS_CHANGES" = "true" ] && echo "✅ Synced" || echo "ℹ️ No changes")"
143
+ echo "==============="
144
+
145
+ if [ "$HAS_CHANGES" = "true" ]; then
146
+ echo "🎯 Main branch has been updated with the latest $SERVICE_NAME changes!"
147
+ else
148
+ echo "👍 Main branch is already up to date with $SERVICE_NAME!"
149
+ fi
0 commit comments