cd ..
ssh

SSH into Server with Tailscale

Learn how to securely SSH into your servers during CI/CD workflows using Tailscale

1 min read

Step 1: Generate a Tailscale Auth Key

  1. Go to your Tailscale admin console.
  2. Navigate to Settings -> Keys -> Create Key.
  3. Choose the type:
    • Reusable: Can be used multiple times (good for CI/CD).
    • Ephemeral: Single-use key (more secure for temporary access).
  4. Copy the generated key and store it in GitHub Secrets as TAILSCALE_AUTH_KEY.

Step 2: Prepare SSH Credentials

  1. On your server, ensure your public key is in ~/.ssh/authorized_keys.
  2. Store your private key in GitHub Secrets as SSH_PRIVATE_KEY.
  3. Store your server username in GitHub Secrets as SSH_USERNAME.
  4. Store your Tailscale Server IP address in GitHub Secrets as TAILSCALE_SERVER_IP.

Step 3: GitHub Actions Workflow

name: SSH to Tailscale Server
 
on:
  push:
    branches:
      - main
 
jobs:
  ssh-server:
    runs-on: ubuntu-latest
 
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
 
      - name: Install Tailscale
        run: |
          curl -fsSL https://tailscale.com/install.sh | sh
 
      - name: Connect to Tailscale
        run: |
          sudo tailscale up --authkey ${{ secrets.TAILSCALE_AUTH_KEY }}
 
      - name: Test Tailscale connection
        run: |
          tailscale status
          ping -c 3 ${{ secrets.TAILSCALE_SERVER_IP }}
 
      - name: SSH into Tailscale Server
        uses: appleboy/ssh-action@v0.1.8
        with:
          host: ${{ secrets.TAILSCALE_SERVER_IP }}
          username: ${{ secrets.SSH_USER }}
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          script: |
            whoami
            hostname
            hostname -I

More to Read