PHP: 設定 GitHub Actions workflow

此文件屬於使用 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:
    # 只要 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

恭喜您!您的專案現已完成自動部署至 production 的設定,並可透過 GitHub Actions 自動化部署流程。