Git Multi-Branch Deployment
Overview
This skill provides guidance for setting up Git-based deployment systems where multiple branches (e.g., main, dev) automatically deploy to different web server locations via post-receive hooks. It covers SSH configuration, Git repository setup, web server configuration, and automated deployment workflows.
Prerequisites Verification
Before starting, verify all required tools and configurations:
- Check for required packages:
git, openssh-server, sshpass (for testing), web server (nginx/apache)
- Verify port availability for SSH (22) and HTTP/HTTPS (80/443)
- Confirm user creation permissions
- Set Git default branch name upfront:
git config --global init.defaultBranch main
Approach: Step-by-Step Implementation
1. User and SSH Setup
Actions:
- Create a dedicated Git user with home directory
- Set password for SSH authentication
- Configure SSH to allow password authentication
SSH Configuration Considerations:
- Modify
/etc/ssh/sshd_config settings: PasswordAuthentication yes, ChallengeResponseAuthentication yes
- Consider additional settings that may interfere:
UsePAM, KbdInteractiveAuthentication
- Restart SSH service after configuration changes
- Plan for SSH host key verification in testing (either pre-accept keys or handle
StrictHostKeyChecking)
2. Git Repository Creation
Actions:
- Create a bare Git repository in the Git user's home directory
- Initialize with proper permissions
- Create initial branches with content
Branch Setup Pattern:
1. Clone the bare repo to a temp location
2. Create initial commit on main branch
3. Create and populate additional branches (e.g., dev)
4. Push all branches to the bare repo
3. Web Server Configuration
Actions:
- Create deployment directories for each branch (e.g.,
/var/www/main, /var/www/dev)
- Set appropriate ownership and permissions for the Git user
- Configure virtual hosts or server blocks for each deployment target
- Handle SSL certificates if HTTPS is required
SSL Certificate Generation (self-signed):
- Use
openssl req with appropriate subject and SAN extensions
- Configure web server to use the certificates with modern TLS protocols
4. Post-Receive Hook Creation
Actions:
- Create executable post-receive hook in
hooks/ directory of bare repo
- Parse ref updates to determine which branch was pushed
- Checkout appropriate branch to corresponding deployment directory
Hook Structure:
1. Read stdin for ref updates (oldrev newrev refname)
2. Parse branch name from refname
3. For each target branch, checkout to deployment directory using GIT_WORK_TREE
5. Service Configuration
Actions:
- Start and enable required services (SSH, web server)
- Configure services to start on boot if persistence is needed
- Verify services are running and accessible
Verification Strategies
SSH Access Verification
- Test SSH connection with password:
sshpass -p <password> ssh -o StrictHostKeyChecking=no <user>@<host>
- Verify Git operations over SSH work correctly
Git Operations Verification
- Clone the repository via SSH
- Create test commits and push to each branch
- Verify pushes complete without errors
Deployment Verification
- After each push, verify content appears in correct deployment directory
- Test web server serves correct content for each branch/endpoint
- Verify deployment completes within required time constraints
End-to-End Testing Pattern
1. Clone repository to fresh test directory
2. Make changes to branch content
3. Push changes
4. Immediately verify web endpoint reflects changes
5. Time the deployment if latency requirements exist
Common Pitfalls and Mistakes
Branch Naming Confusion
- Problem: Git may default to
master instead of main
- Solution: Set
git config --global init.defaultBranch main before creating repositories
- Recovery: Rename branch with
git branch -m master main if already created
SSH Configuration Incomplete
- Problem: SSH connections fail despite configuration changes
- Causes: Missing settings like
UsePAM, KbdInteractiveAuthentication, or service not restarted
- Solution: Verify all related SSH settings and restart sshd after changes
Host Key Verification Failures
- Problem: SSH commands fail with host key verification errors
- Solutions:
- Pre-accept host keys:
ssh-keyscan -H <host> >> ~/.ssh/known_hosts
- Use
StrictHostKeyChecking=no for testing (document security implications)
Push Conflicts During Testing
- Problem: Non-fast-forward errors when re-testing
- Causes: Previous test data conflicts with new pushes
- Solutions:
- Use fresh test directories for each test run
- Force-push if appropriate:
git push --force
- Reset repository state between tests
Post-Receive Hook Issues
- Problem: Hook doesn't execute or deployment fails silently
- Causes: Hook not executable, wrong shebang, missing GIT_WORK_TREE
- Verification: Add logging to hook, check permissions with
ls -la hooks/
Service State Issues
- Problem: Services not running or not starting on boot
- Solution: Use
systemctl enable for boot persistence, verify status before testing
Missing Prerequisites
- Problem: Commands fail because required tools not installed
- Solution: Check all prerequisites at the start, install missing packages before beginning setup
Edge Cases to Consider
Unhandled Branch Pushes
- Decide behavior when branches other than configured ones are pushed
- Document whether silent ignore, warning, or error is appropriate
Failed Deployments
- Consider adding error handling in post-receive hooks
- Decide on notification mechanism for deployment failures
Concurrent Pushes
- Be aware of potential race conditions with simultaneous pushes
- Consider adding locking mechanism for production environments
Permission Issues
- Ensure Git user has write access to all deployment directories
- Verify ownership after creating directories
Efficiency Recommendations
Reduce Repetition
- Set environment variables for repeated SSH commands:
export GIT_SSH_COMMAND='sshpass -p password ssh -o StrictHostKeyChecking=no'
- Batch related operations into single shell invocations
Clean Testing
- Use isolated test directories that don't accumulate artifacts
- Clean up test repositories between runs
Command Availability
- Verify command existence before using (e.g.,
command -v time before timing operations)
- Have fallbacks ready (e.g.,
date +%s%3N instead of time)
1---2name: git-multibranch-23description: Guidance for setting up Git-based multi-branch deployment systems with SSH access, web servers, and automated deployment hooks. This skill should be used when configuring Git repositories that deploy to multiple environments (e.g., main/dev branches), setting up SSH authentication for Git, configuring web servers to serve content from different branches, or creating post-receive hooks for automated deployments.4---5
6# Git Multi-Branch Deployment
7
8## Overview
9
10This skill provides guidance for setting up Git-based deployment systems where multiple branches (e.g., main, dev) automatically deploy to different web server locations via post-receive hooks. It covers SSH configuration, Git repository setup, web server configuration, and automated deployment workflows.
11
12## Prerequisites Verification
13
14Before starting, verify all required tools and configurations:
15
16- Check for required packages: `git`, `openssh-server`, `sshpass` (for testing), web server (nginx/apache)
17- Verify port availability for SSH (22) and HTTP/HTTPS (80/443)
18- Confirm user creation permissions
19- Set Git default branch name upfront: `git config --global init.defaultBranch main`
20
21## Approach: Step-by-Step Implementation
22
23### 1. User and SSH Setup
24
25**Actions:**
26- Create a dedicated Git user with home directory
27- Set password for SSH authentication
28- Configure SSH to allow password authentication
29
30**SSH Configuration Considerations:**
31- Modify `/etc/ssh/sshd_config` settings: `PasswordAuthentication yes`, `ChallengeResponseAuthentication yes`
32- Consider additional settings that may interfere: `UsePAM`, `KbdInteractiveAuthentication`
33- Restart SSH service after configuration changes
34- Plan for SSH host key verification in testing (either pre-accept keys or handle `StrictHostKeyChecking`)
35
36### 2. Git Repository Creation
37
38**Actions:**
39- Create a bare Git repository in the Git user's home directory
40- Initialize with proper permissions
41- Create initial branches with content
42
43**Branch Setup Pattern:**
44```
451. Clone the bare repo to a temp location
462. Create initial commit on main branch
473. Create and populate additional branches (e.g., dev)
484. Push all branches to the bare repo
49```
50
51### 3. Web Server Configuration
52
53**Actions:**
54- Create deployment directories for each branch (e.g., `/var/www/main`, `/var/www/dev`)
55- Set appropriate ownership and permissions for the Git user
56- Configure virtual hosts or server blocks for each deployment target
57- Handle SSL certificates if HTTPS is required
58
59**SSL Certificate Generation (self-signed):**
60- Use `openssl req` with appropriate subject and SAN extensions
61- Configure web server to use the certificates with modern TLS protocols
62
63### 4. Post-Receive Hook Creation
64
65**Actions:**
66- Create executable post-receive hook in `hooks/` directory of bare repo
67- Parse ref updates to determine which branch was pushed
68- Checkout appropriate branch to corresponding deployment directory
69
70**Hook Structure:**
71```
721. Read stdin for ref updates (oldrev newrev refname)
732. Parse branch name from refname
743. For each target branch, checkout to deployment directory using GIT_WORK_TREE
75```
76
77### 5. Service Configuration
78
79**Actions:**
80- Start and enable required services (SSH, web server)
81- Configure services to start on boot if persistence is needed
82- Verify services are running and accessible
83
84## Verification Strategies
85
86### SSH Access Verification
87- Test SSH connection with password: `sshpass -p <password> ssh -o StrictHostKeyChecking=no <user>@<host>`
88- Verify Git operations over SSH work correctly
89
90### Git Operations Verification
91- Clone the repository via SSH
92- Create test commits and push to each branch
93- Verify pushes complete without errors
94
95### Deployment Verification
96- After each push, verify content appears in correct deployment directory
97- Test web server serves correct content for each branch/endpoint
98- Verify deployment completes within required time constraints
99
100### End-to-End Testing Pattern
101```
1021. Clone repository to fresh test directory
1032. Make changes to branch content
1043. Push changes
1054. Immediately verify web endpoint reflects changes
1065. Time the deployment if latency requirements exist
107```
108
109## Common Pitfalls and Mistakes
110
111### Branch Naming Confusion
112- **Problem:** Git may default to `master` instead of `main`
113- **Solution:** Set `git config --global init.defaultBranch main` before creating repositories
114- **Recovery:** Rename branch with `git branch -m master main` if already created
115
116### SSH Configuration Incomplete
117- **Problem:** SSH connections fail despite configuration changes
118- **Causes:** Missing settings like `UsePAM`, `KbdInteractiveAuthentication`, or service not restarted
119- **Solution:** Verify all related SSH settings and restart sshd after changes
120
121### Host Key Verification Failures
122- **Problem:** SSH commands fail with host key verification errors
123- **Solutions:**
124 - Pre-accept host keys: `ssh-keyscan -H <host> >> ~/.ssh/known_hosts`
125 - Use `StrictHostKeyChecking=no` for testing (document security implications)
126
127### Push Conflicts During Testing
128- **Problem:** Non-fast-forward errors when re-testing
129- **Causes:** Previous test data conflicts with new pushes
130- **Solutions:**
131 - Use fresh test directories for each test run
132 - Force-push if appropriate: `git push --force`
133 - Reset repository state between tests
134
135### Post-Receive Hook Issues
136- **Problem:** Hook doesn't execute or deployment fails silently
137- **Causes:** Hook not executable, wrong shebang, missing GIT_WORK_TREE
138- **Verification:** Add logging to hook, check permissions with `ls -la hooks/`
139
140### Service State Issues
141- **Problem:** Services not running or not starting on boot
142- **Solution:** Use `systemctl enable` for boot persistence, verify status before testing
143
144### Missing Prerequisites
145- **Problem:** Commands fail because required tools not installed
146- **Solution:** Check all prerequisites at the start, install missing packages before beginning setup
147
148## Edge Cases to Consider
149
150### Unhandled Branch Pushes
151- Decide behavior when branches other than configured ones are pushed
152- Document whether silent ignore, warning, or error is appropriate
153
154### Failed Deployments
155- Consider adding error handling in post-receive hooks
156- Decide on notification mechanism for deployment failures
157
158### Concurrent Pushes
159- Be aware of potential race conditions with simultaneous pushes
160- Consider adding locking mechanism for production environments
161
162### Permission Issues
163- Ensure Git user has write access to all deployment directories
164- Verify ownership after creating directories
165
166## Efficiency Recommendations
167
168### Reduce Repetition
169- Set environment variables for repeated SSH commands: `export GIT_SSH_COMMAND='sshpass -p password ssh -o StrictHostKeyChecking=no'`
170- Batch related operations into single shell invocations
171
172### Clean Testing
173- Use isolated test directories that don't accumulate artifacts
174- Clean up test repositories between runs
175
176### Command Availability
177- Verify command existence before using (e.g., `command -v time` before timing operations)
178- Have fallbacks ready (e.g., `date +%s%3N` instead of `time`)