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:
    # 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 部署到生产环境GitHub Actions 部署到生产环境

恭喜!您的项目现已配置为通过 GitHub Actions 自动部署到生产环境。