Ruby: 設定 GitHub Actions workflow

此文件屬於使用 GitHub Actions 進行部署指南的一部分。請在此處查看完整指南:如何利用 GitHub Actions 部署您的 Ruby 程式碼

👋 歡迎來到 Stackhero 文檔!

Stackhero 提供一個即用型的 Ruby cloud 解決方案,帶來多種好處,包括:

  • 只需一個簡單的 git push,即可在幾秒鐘內 部署您的應用程式。
  • 使用您自己的域名,並享受 HTTPS 證書的自動配置以增強安全性。
  • 享受自動備份一鍵更新,以及簡單、透明和可預測的定價,讓您安心無憂。
  • 得益於專用的私人 VM,獲得最佳的性能和強大的安全性

節省時間簡化您的生活:只需 5 分鐘即可嘗試 Stackhero 的 Ruby cloud hosting 解決方案!

在您的本機 Git repository 中,建立一個名為 .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 }}

建立 workflow 檔案後,您可以提交變更。

git add -A .
git commit -m "Add GitHub Actions to deploy to Stackhero"

接著,建立 production 分支。

git checkout -b production

最後,將變更 push 到 GitHub。

git push --set-upstream origin production

這個 git push 會將您的程式碼上傳到 GitHub 的 production 分支。GitHub Actions 會自動啟動並將您的程式碼部署到 Stackhero 實例。

如要查看 workflow 執行情況,請到您的 GitHub 專案頁面並點擊 ActionsGitHub Actions 已部署到 productionGitHub Actions 已部署到 production

恭喜您,已成功利用 GitHub Actions 建立自動部署到 production 的流程。