+1

[MacBook-Server] Sử dụng Github Action để SSH đến Macbook 🚀 (2/4)

Tiếp tục ^^

Xem nhiều hơn tại: https://devvui.one

Hi anh em dev 👋, ở phần 1, chúng ta đã setup được Cloudflare Tunnel để kết nối với MacBook. Trong phần này, mình sẽ hướng dẫn cách setup GitHub Actions để SSH MacBook server để xem như nào nhé 😎

Sau bài viết này, chúng ta sẽ:

  • ✅ Setup SSH key cho GitHub Actions để SSH đến MacBook
  • ✅ Cấu hình GitHub Actions workflow
  • ✅ Tự động nhận thông báo qua Telegram khi deploy

Setup SSH Key cho GitHub Actions

1. Tạo SSH Key

Đầu tiên, tạo một cặp SSH key mới:

ssh-keygen -t ed25519 -C "github-actions" -f ./github-actions-key

2. Thêm Public Key vào MacBook

Copy nội dung public key vào file ~/.ssh/authorized_keys trên MacBook:

cat github-actions-key.pub >> ~/.ssh/authorized_keys

3. Thêm Private Key vào GitHub Secrets

  1. Vào repository settings > Secrets and variables > Actions
  2. Tạo secret mới với tên SSH_PRIVATE_KEY
  3. Copy nội dung file github-actions-key vào secret

Tạo repository trên Github

Bạn tạo 1 repository trên Github để thử nghiệm nhé, hoặc bạn có thể tham khảo Repository của mình tại đây nha Repository

Thiết lập GitHub Actions

Tạo file .github/workflows/deploy.yml để setup workflow:

name: SSH to Mac
on: [push]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install Cloudflared
        run: |
          curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64 -o cloudflared
          chmod +x cloudflared
          sudo mv cloudflared /usr/local/bin

      - name: Setup SSH key
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519

      - name: Connect via SSH
        run: |
          ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no \
            -o ProxyCommand='cloudflared access ssh --hostname %h' \
            dongtran@domain.xyz

      - name: Create test file
        run: |
          ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no \
            -o ProxyCommand='cloudflared access ssh --hostname %h' \
            dongtran@domain.xyz "echo 'SSH connection successful at $(date)' > ~/ssh_test.txt"

  notify:
    needs: deploy
    runs-on: ubuntu-latest
    steps:
      - name: Send Telegram Notification
        uses: appleboy/telegram-action@master
        with:
          to: ${{ secrets.TELEGRAM_CHAT_ID }}
          token: ${{ secrets.TELEGRAM_BOT_TOKEN }}
          format: html
          message: |
            ${{ job.status == 'success' && '✅' || '❌' }} <b>Deploy ${{ job.status == 'success' && 'thành công' || 'thất bại' }}!</b>
            
            Repo: ${{ github.repository }}
            Branch: ${{ github.ref_name }}
            Commit: ${{ github.sha }}
            Author: ${{ github.actor }}

Setup Telegram Notifications 🤗

Tạo Telegram Bot

  1. Chat với @BotFather trên Telegram
  2. Sử dụng lệnh /newbot để tạo bot mới
  3. Lưu lại API token và thêm vào GitHub Secrets với tên TELEGRAM_BOT_TOKEN
  4. Lấy Chat ID (có thể là chat id của chính bạn hoặc là chat id của 1 group nào đó nhé, tớ lấy id này bằng cách sử dụng Telegram Web) và thêm vào GitHub Secrets với tên TELEGRAM_CHAT_ID

Mình sử dụng action appleboy/telegram-action để gửi thông báo, nó sẽ giúp code ngắn gọn và dễ maintain hơn 😎

Test thử nào! 🚀

Workflow của chúng ta sẽ tạo một file ssh_test.txt trong thư mục home với timestamp. Để test:

  1. Push code lên GitHub repository
  2. Check GitHub Actions tab để theo dõi workflow

Hình ảnh khi wofklow trên Github Action chạy như sau Architecture Overview

  1. SSH vào MacBook và kiểm tra file:
cat ~/ssh_test.txt

Nếu thấy nội dung kiểu "SSH connection successful at..." là đã thành công rồi nhé


Bài viết của mình dừng ở đây 😛

Ở phần tiếp theo, chúng ta sẽ tìm hiểu cách deploy MongoDB, các bạn thấy ý tưởng này như nào, cùng để lại comment nhé 🥰


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí