PHP: 設定 GitHub Actions 工作流程

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

👋 歡迎來到 Stackhero 文件中心!

Stackhero 提供專為快速且可靠部署設計的 PHP cloud 平台:

  • 只需一個簡單的 git push,即可在數秒內部署您的 PHP 應用程式到生產環境。
  • 可使用您自己的網域名稱,並為您自動設定 HTTPS 憑證,全程無需手動操作。
  • 享有自動備份一鍵更新,以及清楚、可預測的收費模式。Stackhero 為您處理這些細節,讓您能專注於開發程式碼。
  • 在專為您的應用程式打造的專屬私有基礎架構上,獲得強大的效能安全性

讓您的工作流程更順暢,節省寶貴時間。只需幾分鐘,即可開始體驗 Stackhero 的 PHP cloud hosting

接下來,您將建立一個 GitHub Actions 工作流程檔案來自動化部署。在您的 Git 儲存庫中,建立 .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:
    # 這裡列出哪些分支在 git 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:
        # Secret "STACKHERO_SSH_PRIVATE_KEY" 與變數 "STACKHERO_ENDPOINT" 必須在對應的 GitHub 環境(「Settings」->「Environments」)中設定
        ssh_private_key: ${{ secrets.STACKHERO_SSH_PRIVATE_KEY }}
        endpoint: ${{ vars.STACKHERO_ENDPOINT }}

新增此檔案後,您可以在終端機執行下列指令提交變更:

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。