Node.js: 設定 GitHub Actions workflow

本文件是使用 GitHub Actions 部署指南的一部分。請在此處查看完整指南:學習如何透過 GitHub Actions 部署您的 Node.js 程式碼

👋 歡迎來到 Stackhero 文件中心!

Stackhero 提供即時可用的 Node.js 雲端 環境,協助您加速開發流程:

  • 只需一個簡單的 git push,即可在幾秒內部署到生產環境。無需額外工具或手動設定。
  • 支援自有網域,並自動配置 HTTPS 憑證,讓您的應用程式安全無虞,無需額外設定。
  • 自動備份一鍵更新,以及預測性收費,讓您專注於功能開發,Stackhero 為您管理基礎架構。
  • 建構於專屬私有基礎架構之上,確保穩定效能與高安全性。

節省時間,降低營運負擔:只需幾分鐘,您的應用程式就能在 Stackhero 的 Node.js 雲端主機 上順利運行。

在您的 Git repository 中,如果尚未有 .github/workflows 目錄,請先建立一個。然後新增一個名為 deploy-to-stackhero.yml 的檔案:

# File: .github/workflows/deploy-to-stackhero.yml

name: Deploy to Stackhero
run-name: Deploy branch "${{ github.ref_name }}" to Stackhero

on:
  push:
    # 列出會觸發部署動作的分支。請確保 GitHub(於 "Settings" > "Environments")中每個分支都有對應的環境。
    # 並在該環境中新增對應的 secret STACKHERO_SSH_PRIVATE_KEY 與變數 STACKHERO_ENDPOINT。
    branches: [ "production", "staging" ]

jobs:
  Deploy:
    environment: ${{ github.ref_name }}
    runs-on: ubuntu-latest
    steps:
    - uses: stackhero-io/github-actions-deploy-to-stackhero@v1
      with:
        # STACKHERO_SSH_PRIVATE_KEY 與 STACKHERO_ENDPOINT 必須在對應的 GitHub 環境中設定。
        ssh_private_key: ${{ secrets.STACKHERO_SSH_PRIVATE_KEY }}
        endpoint: ${{ vars.STACKHERO_ENDPOINT }}

建立 workflow 檔案後,您可以這樣提交變更:

git add -A .
git commit -m "Add GitHub Actions to deploy to Stackhero"

若要建立 production 分支,請執行:

git checkout -b production

然後將您的變更推送到 GitHub:

git push --set-upstream origin production

這次推送會將您的程式碼送到 production 分支,並觸發 GitHub Actions 將您的程式碼部署到 Stackhero 服務。若要確認部署狀態,請在 GitHub 專案頁面點擊 Actions

GitHub Actions 已部署到 productionGitHub Actions 已部署到 production

就是這樣。您的程式碼現在已經設定好,能夠透過 GitHub Actions 自動部署到 production。