Node.js: 配置 GitHub Actions 工作流

本文档属于使用 GitHub Actions 部署指南的一部分。您可以在此处查看完整指南:了解如何使用 GitHub Actions 部署您的 Node.js 代码

👋 欢迎使用 Stackhero 文档!

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

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

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

在您的 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:
    # 列出触发部署操作的分支。请确保 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 环境中设置。
        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

此操作会将您的代码推送到 production 分支,并触发 GitHub Actions,将您的代码部署到 Stackhero 服务。要确认部署情况,请在 GitHub 项目中点击 Actions

GitHub Actions 部署到生产环境GitHub Actions 部署到生产环境

就是这样。您的代码现在已通过 GitHub Actions 实现自动部署到生产环境。