Python: 配置 GitHub Actions 工作流

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

👋 欢迎来到 Stackhero 文档!

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

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

节省时间简化您的生活:尝试 Stackhero 的 Python 云托管 解决方案只需 5 分钟

在您的 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 an environment corresponding to the branch name in GitHub (in "Settings"/"Environments").
    # Then add the corresponding secret "STACKHERO_SSH_PRIVATE_KEY" and variable "STACKHERO_ENDPOINT" in this 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 branch environment on GitHub 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 实现了自动部署到生产环境。