PHP: 設定 GitHub Actions 工作流程

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

👋 歡迎來到 Stackhero 文件中心!

Stackhero 提供託管型 PHP 主機服務,專為快速且可靠的部署而設計:

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

讓您的開發流程更順暢,節省寶貴時間。只需幾分鐘,即可開始使用 Stackhero 的 Laravel 與 Symfony 專用 PHP 主機服務。

接下來,您將建立 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:
    # List of branches that will trigger the deploy action following a git push
    # Do not forget to create a corresponding environment in GitHub (under "Settings" -> "Environments") for each branch
    # Then add the secret "STACKHERO_SSH_PRIVATE_KEY" and variable "STACKHERO_ENDPOINT" in that 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 GitHub environment (under "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。