Ruby: 配置 GitHub Actions 工作流

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

👋 欢迎来到 Stackhero 文档!

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

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

节省时间简化您的生活:只需 5 分钟即可试用 Stackhero 的 Ruby cloud hosting 解决方案!

在本地电脑的 Git 仓库中,创建一个名为 .github/workflows 的目录。在该目录下新建一个名为 deploy-to-stackhero.yml 的文件。

# 文件:.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")中为每个分支创建对应的环境。
    # 然后在该环境下添加相应的密钥 "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:
        # 密钥 "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

最后一次 git push 会将您的代码上传到 GitHub 的 production 分支。GitHub Actions 会自动启动并将您的代码部署到 Stackhero 实例。

要查看工作流运行情况,请访问项目的 GitHub 页面并点击 ActionsGitHub Actions 部署到生产环境GitHub Actions 部署到生产环境

恭喜,您已成功通过 GitHub Actions 实现生产环境的持续部署。