Skip to content

Commit 9dd253e

Browse files
committed
Optimize SSH connections and add caching for GitHub Actions deployment
1 parent 4a21104 commit 9dd253e

File tree

4 files changed

+60
-44
lines changed

4 files changed

+60
-44
lines changed

.deployignore

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1+
# Dependencies
12
node_modules/
3+
.pnpm-store/
4+
5+
# Version control
6+
.git/
7+
.github/
8+
.gitignore
9+
10+
# Logs and temporary files
211
*.log
3-
.DS_Store
12+
.DS_Store
13+
npm-debug.log*
14+
yarn-debug.log*
15+
yarn-error.log*
16+
17+
# Development files
18+
.vscode/
19+
.idea/
20+
*.md
21+
!README.md
22+
*.test.js
23+
tests/
24+
test/
25+
26+
# Environment files (handled separately in workflow)
27+
.env*
28+
!.env.example
29+
30+
# Other unnecessary files
31+
*.tmp
32+
*.bak

.github/workflows/deploy.yml

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,65 +12,68 @@ jobs:
1212
steps:
1313
- name: Checkout code
1414
uses: actions/checkout@v3
15+
16+
# Cache SSH configuration
17+
- name: Cache SSH configuration
18+
uses: actions/cache@v3
19+
id: ssh-cache
20+
with:
21+
path: ~/.ssh
22+
key: ssh-config-${{ runner.os }}-v1
1523

1624
- name: Set up SSH
1725
uses: webfactory/[email protected]
1826
with:
1927
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
2028

2129
- name: Add server to known hosts
30+
if: steps.ssh-cache.outputs.cache-hit != 'true'
2231
run: |
2332
mkdir -p ~/.ssh
2433
2534
# Create SSH config file with custom port and user
26-
echo "Creating SSH config file..."
2735
cat > ~/.ssh/config << EOF
2836
Host profullstack.com 104.36.23.197
2937
HostName 104.36.23.197
3038
User ubuntu
3139
Port 2048
40+
ControlMaster auto
41+
ControlPath ~/.ssh/control-%C
42+
ControlPersist 30s
3243
EOF
3344
chmod 600 ~/.ssh/config
3445
3546
# Run ssh-keyscan with the correct port
36-
echo "Running ssh-keyscan with custom port..."
37-
# The -p flag is for the port, -T disables pseudo-terminal allocation
3847
ssh-keyscan -p 2048 -T 60 104.36.23.197 > /tmp/known_hosts_entry
3948
4049
# Check if ssh-keyscan succeeded
4150
if [ -s /tmp/known_hosts_entry ]; then
42-
echo "Successfully retrieved host key:"
43-
cat /tmp/known_hosts_entry
44-
45-
# Add the host key to known_hosts
4651
cat /tmp/known_hosts_entry >> ~/.ssh/known_hosts
4752
chmod 644 ~/.ssh/known_hosts
4853
else
49-
echo "Failed to retrieve host key via ssh-keyscan, using provided key..."
5054
echo "${{ secrets.SERVER_KNOWN_HOSTS }}" > ~/.ssh/known_hosts
5155
chmod 644 ~/.ssh/known_hosts
5256
fi
53-
54-
# Verify the files exist and have content
55-
ls -la ~/.ssh/
56-
echo "Content of SSH config:"
57-
cat ~/.ssh/config
58-
echo "Lines in known_hosts:"
59-
wc -l ~/.ssh/known_hosts
57+
58+
# Cache apt packages
59+
- name: Cache apt packages
60+
uses: actions/cache@v3
61+
id: apt-cache
62+
with:
63+
path: /var/cache/apt/archives
64+
key: apt-packages-${{ runner.os }}-${{ hashFiles('.github/workflows/deploy.yml') }}
6065

6166
- name: Install dependencies
6267
run: |
63-
sudo apt-get update
64-
sudo apt-get install -y rsync zsh curl file golang postgresql-client gnome-keyring dbus-x11
68+
sudo apt-get update -qq
69+
sudo apt-get install -y --no-install-recommends rsync zsh curl file golang postgresql-client gnome-keyring dbus-x11
6570
6671
# Set up dbus for gnome-keyring
67-
echo "Setting up dbus and gnome-keyring..."
6872
mkdir -p ~/.cache
6973
dbus-launch --sh-syntax > ~/.cache/dbus-env
7074
source ~/.cache/dbus-env
7175
7276
# Initialize the keyring
73-
echo "Initializing gnome-keyring..."
7477
echo -n "password" | gnome-keyring-daemon --unlock
7578
7679
# Export environment variables for subsequent steps
@@ -80,8 +83,6 @@ jobs:
8083
run: |
8184
# Create .env file from GitHub secret
8285
echo "${{ secrets.ENV_FILE_CONTENT }}" > .env
83-
84-
# Print confirmation (without showing the content for security)
8586
echo "Created .env file with $(grep -c '' .env) lines"
8687
8788
- name: Prepare for deployment
@@ -90,28 +91,14 @@ jobs:
9091
chmod +x ./bin/supabase-db.sh
9192
chmod +x ./bin/deploy.sh
9293
chmod +x ./bin/deploy-with-migrations.sh
93-
94-
# Debug: Check .env file existence
95-
echo "Checking .env file..."
96-
if [ -f .env ]; then
97-
echo ".env file exists with $(grep -c '' .env) lines"
98-
else
99-
echo ".env file does not exist"
100-
fi
10194
10295
- name: Deploy to server with migrations
10396
run: |
104-
# Print debug info
105-
echo "Current directory: $(pwd)"
106-
echo "Files in bin directory:"
107-
ls -la ./bin
108-
10997
# Run deploy script with migrations
11098
./bin/deploy-with-migrations.sh
11199
112100
# Run test script to verify deployment
113-
echo "Running test script on remote server..."
114-
ssh -p 2048 [email protected] "cd $DEPLOY_REMOTE_DIR && chmod +x bin/test-github-actions.sh && ./bin/test-github-actions.sh"
101+
ssh -p 2048 -o ControlMaster=auto -o ControlPath=~/.ssh/control-%C -o ControlPersist=30s [email protected] "cd $DEPLOY_REMOTE_DIR && chmod +x bin/test-github-actions.sh && ./bin/test-github-actions.sh"
115102
env:
116103
DEPLOY_REMOTE_HOST: 104.36.23.197
117104
DEPLOY_REMOTE_PORT: 2048

bin/deploy-with-migrations.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ REMOTE_PORT="${DEPLOY_REMOTE_PORT:-2048}"
2323
REMOTE_USER="${DEPLOY_REMOTE_USER:-ubuntu}"
2424
REMOTE_DIR="${DEPLOY_REMOTE_DIR:-www/profullstack.com/pdf}"
2525

26-
# Create SSH options
27-
SSH_OPTS="-p $REMOTE_PORT"
26+
# Create SSH options with connection reuse
27+
SSH_OPTS="-p $REMOTE_PORT -o ControlMaster=auto -o ControlPath=~/.ssh/control-%C -o ControlPersist=30s"
2828

2929
# Deploy the code first using deploy.sh
3030
echo -e "${YELLOW}Running deployment script...${NC}"

bin/deploy.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ REMOTE_DIR="${DEPLOY_REMOTE_DIR:-www/profullstack.com/pdf}"
1616
LOCAL_DIR="."
1717
INSTALL_SERVICE="${INSTALL_SERVICE:-false}"
1818

19-
# Create SSH options
20-
SSH_OPTS="-p $REMOTE_PORT"
19+
# Create SSH options with connection reuse
20+
SSH_OPTS="-p $REMOTE_PORT -o ControlMaster=auto -o ControlPath=~/.ssh/control-%C -o ControlPersist=30s"
2121
SCP_OPTS="-P $REMOTE_PORT"
22-
RSYNC_OPTS="-e \"ssh -p $REMOTE_PORT\""
22+
RSYNC_OPTS="-e \"ssh -p $REMOTE_PORT -o ControlMaster=auto -o ControlPath=~/.ssh/control-%C -o ControlPersist=30s\""
2323

2424
# Set colors for output
2525
GREEN='\033[0;32m'
@@ -42,10 +42,10 @@ if ssh $SSH_OPTS $REMOTE_USER@$REMOTE_HOST "[ -d $REMOTE_DIR ]"; then
4242
# Deploy using rsync with .deployignore
4343
if [ -f .deployignore ]; then
4444
echo -e "${YELLOW}Using .deployignore file for exclusions...${NC}"
45-
rsync -avz --partial --progress -e "ssh -p $REMOTE_PORT" --exclude-from=.deployignore $LOCAL_DIR $REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR
45+
rsync -avz --partial --compress-level=9 --delete -e "ssh -p $REMOTE_PORT -o ControlMaster=auto -o ControlPath=~/.ssh/control-%C -o ControlPersist=30s" --exclude-from=.deployignore $LOCAL_DIR $REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR
4646
else
4747
echo -e "${YELLOW}No .deployignore file found. Excluding node_modules/ by default...${NC}"
48-
rsync -avz --partial --progress -e "ssh -p $REMOTE_PORT" --exclude="node_modules/" $LOCAL_DIR $REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR
48+
rsync -avz --partial --compress-level=9 --delete -e "ssh -p $REMOTE_PORT -o ControlMaster=auto -o ControlPath=~/.ssh/control-%C -o ControlPersist=30s" --exclude="node_modules/" $LOCAL_DIR $REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR
4949
fi
5050

5151
# Check if rsync was successful

0 commit comments

Comments
 (0)