|
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 |
2 | 20 | run: | |
3 | | - # 1. Prepare the SSH Key |
4 | 21 | 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 |
7 | 24 |
|
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 |
11 | 30 |
|
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 }} |
17 | 33 |
|
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' |
23 | 36 | cd /var/www/${{ github.event.repository.name }} |
24 | 37 | |
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 }} |
27 | 40 | |
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 }} |
30 | 43 | |
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 |
33 | 46 | EOF |
0 commit comments