PHP: 配置 GitHub Actions 工作流

本文件是使用 GitHub Actions 部署指南的一部分。您可以在这里查看完整指南:如何使用 GitHub Actions 部署您的 PHP 代码

👋 欢迎来到 Stackhero 文档!

Stackhero 提供现成的 PHP 云 解决方案,具有多种优势,包括:

  • 通过简单的 git push 在几秒钟内 部署您的应用程序。
  • 使用您自己的域名,并享受 HTTPS 证书的自动配置以增强安全性。
  • 享受自动备份一键更新以及简单、透明和可预测的定价带来的安心。
  • 通过私有和专用的 VM获得最佳的性能和强大的安全性

节省时间简化您的生活:只需 5 分钟即可试用 Stackhero 的 PHP 云托管 解决方案!

接下来,您需要创建一个 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 自动部署到生产环境。