Python: 設定 GitHub Actions 工作流程

本文件是使用 GitHub Actions 部署指南的一部分。請在此處查看完整指南:如何使用 GitHub Actions 部署您的 Python 程式碼

👋 歡迎來到 Stackhero 文件中心!

Stackhero 提供即時可用的 Python 雲端 解決方案,專為簡化您的部署流程而設計:

  • 只需一個簡單的 git push,即可在數秒內將您的應用程式部署到正式環境。
  • 支援自訂網域名稱,並為您自動設定 HTTPS 憑證,確保連線安全。
  • 享有自動備份一鍵更新可預測的計價,讓您專注於程式開發,而無需煩惱基礎架構管理。
  • 專屬私有基礎架構上,獲得強大的效能安全性。您的執行環境完全隔離並受到保護。

節省時間簡化您的工作流程:透過 Stackhero 的 Python 雲端主機,您的程式碼只需 5 分鐘即可上線運作。

在您的 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:
    # List of branches that will trigger the deploy action following a git push.
    # Do not forget to create an environment corresponding to the branch name in GitHub (in "Settings"/"Environments").
    # Then add the corresponding secret "STACKHERO_SSH_PRIVATE_KEY" and variable "STACKHERO_ENDPOINT" in this environment.
    branches: [ "production", "staging" ]

jobs:
  Deploy:
    environment: ${{ github.ref_name }}
    runs-on: ubuntu-latest
    steps:
    - uses: stackhero-io/github-actions-deploy-to-stackhero@v1
      with:
        # The secret "STACKHERO_SSH_PRIVATE_KEY" and the variable "STACKHERO_ENDPOINT" should be defined in the corresponding branch environment on GitHub under "Settings"/"Environments".
        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

這會將您的程式碼推送到 GitHub 上的 production 分支,並觸發 GitHub Actions,將您的程式碼部署到 Stackhero 實例。

要檢查部署狀態,只需前往您的 GitHub 專案頁面並點擊 Actions

GitHub Actions 已部署至 productionGitHub Actions 已部署至 production

就是這麼簡單。您現在已經透過 GitHub Actions 實現自動部署到 production。