PHP: 設定 GitHub Actions 工作流程

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

👋 歡迎瀏覽 Stackhero 文件!

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

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

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

接下來,您需要建立一個 GitHub Actions workflow 檔案來自動化部署。在您的 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:
    # 會觸發部署動作的分支清單
    # 請務必為每個分支在 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。