Skip to content

Commit 674b34c

Browse files
authored
Refactor deployment workflow for Cloudflare Tunnel
Updated deployment workflow to streamline SSH and rsync commands.
1 parent f944dcd commit 674b34c

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

.github/workflows/deploy.yml

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,46 @@
1-
- name: Deploy via Cloudflare Tunnel
1+
name: Deploy App
2+
on:
3+
push:
4+
branches: [ main ]
5+
6+
jobs:
7+
deploy:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Checkout Code
11+
uses: actions/checkout@v4
12+
13+
- name: Install Cloudflared on Runner
14+
run: |
15+
curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64 -o cloudflared
16+
chmod +x cloudflared
17+
sudo mv cloudflared /usr/local/bin/
18+
19+
- name: Deploy via Cloudflare Tunnel
220
run: |
3-
# 1. Prepare the SSH Key
421
mkdir -p ~/.ssh
5-
echo "${{ secrets.SSH_KEY }}" > ~/.ssh/deploy_key
6-
chmod 600 ~/.ssh/deploy_key
22+
echo "${{ secrets.SSH_KEY }}" > ~/.ssh/id_ed25519
23+
chmod 600 ~/.ssh/id_ed25519
724
8-
# 2. Define the Cloudflare Proxy Command
9-
# This tells SSH to "tunnel" through Cloudflare using your Service Token
10-
PROXY="cloudflared access ssh --hostname ${{ secrets.SSH_HOST }} --id ${{ secrets.CF_CLIENT_ID }} --secret ${{ secrets.CF_CLIENT_SECRET }}"
25+
# Use Service Tokens to bypass the Cloudflare Access login screen
26+
cat <<EOF > ~/.ssh/config
27+
Host ${{ secrets.SSH_HOST }}
28+
ProxyCommand /usr/local/bin/cloudflared access ssh --hostname %h --service-token-id ${{ secrets.CF_CLIENT_ID }} --service-token-secret ${{ secrets.CF_CLIENT_SECRET }}
29+
EOF
1130
12-
# 3. Run RSYNC (Syncing the code)
13-
# We use -e to pass the specific SSH command with the Proxy
14-
rsync -e "ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no -o ProxyCommand='$PROXY'" \
15-
-avz --delete --exclude '.git' . \
16-
${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}:/var/www/${{ github.event.repository.name }}
31+
# Sync the project files to the VPS
32+
rsync -e "ssh -o StrictHostKeyChecking=no" -avz --delete --exclude '.git' . ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}:/var/www/${{ github.event.repository.name }}
1733
18-
# 4. Run Build and Restart on VPS
19-
ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no -o ProxyCommand="$PROXY" \
20-
${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} << 'EOF'
21-
22-
# Ensure we are in the right directory
34+
# Run the build and restart commands on the VPS
35+
ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} << 'EOF'
2336
cd /var/www/${{ github.event.repository.name }}
2437
25-
# Load the paths for nixpacks and pm2
26-
export PATH=$PATH:/usr/local/bin:/usr/bin
38+
# Use the absolute path for Nixpacks (since WARP is now helping Docker)
39+
/usr/bin/nixpacks build . --name ${{ github.event.repository.name }}
2740
28-
# Build the app (Docker will use the WARP proxy we set up on the VPS)
29-
nixpacks build . --name ${{ github.event.repository.name }}
41+
# Use the absolute path for the PM2 we just moved to /usr/bin
42+
/usr/bin/pm2 restart ${{ github.event.repository.name }} || /usr/bin/pm2 start "nixpacks run ." --name ${{ github.event.repository.name }}
3043
31-
# Restart the app
32-
pm2 restart ${{ github.event.repository.name }} || pm2 start "nixpacks run ." --name ${{ github.event.repository.name }}
44+
# Save the PM2 list so it persists after a reboot
45+
/usr/bin/pm2 save
3346
EOF

0 commit comments

Comments
 (0)