Python: 配置 GitHub Actions 工作流

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

👋 欢迎来到 Stackhero 文档!

Stackhero 提供即开即用的 Python cloud 解决方案,旨在简化您的部署流程:

  • 只需简单 git push,即可在几秒钟内将您的应用部署到生产环境。
  • 支持自定义域名,自动配置 HTTPS 证书,安全连接由我们为您管理。
  • 提供自动备份一键更新可预测的定价,让您专注于代码开发,无需担心基础设施管理。
  • 依托专属私有基础设施,为您带来强大的性能安全性。您的环境将被隔离并受到保护。

节省时间简化工作流程:通过 Stackhero 的 Python cloud hosting,您的代码最快只需 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 实现了自动部署到生产环境。