PHP: 設定 GitHub Actions 工作流程

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

👋 歡迎來到 Stackhero 文檔!

Stackhero 提供一個即用型的 PHP cloud 解決方案,帶來多項優勢,包括:

  • 只需一個簡單的 git push,即可在幾秒鐘內部署您的應用程式。
  • 使用您自己的域名,並享受 HTTPS 證書的自動配置,以增強安全性。
  • 享受自動備份一鍵更新,以及簡單、透明且可預測的定價,讓您高枕無憂。
  • 通過專用的私人 VM獲得最佳的性能和強大的安全性

節省時間簡化您的生活:只需 5 分鐘即可嘗試 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。