GitHub Actions 集成
复制包含安装步骤和该插件完整 Markdown 指南的配置 prompt。
将 Capgo Live Updates 与 GitHub Actions 集成,实现每次推送 code 变更时自动部署应用更新。本指南将介绍如何利用 GitHub 强大的 CI/CD 平台来配置自动化构建、测试及部署工作流。
前提条件
“前提条件”章节在配置 GitHub Actions 集成之前,请确保您已具备:
- 包含应用源码 code 的 GitHub 仓库
- 一个已配置好 App 的 Capgo 账号
- 项目中已配置 Node.js 和 npm/yarn
- 已为您的仓库启用了 GitHub Actions
配置 GitHub Secrets
标题为“配置 GitHub Secrets”的章节步骤 1:配置仓库 Secrets
第 1 步:配置 Repository Secrets在你的 GitHub 仓库中设置必要的 secrets:
- 前往你的 GitHub 仓库
- 前往 设置 → Secrets 与变量 → 操作
- 点击 新建仓库 Secret 并添加以下内容:
| Secret 名称 | 值 |
|---|---|
CAPGO_TOKEN | 你的 Capgo API token |
简单的生产环境部署
“简单生产环境部署”章节从以下基础配置开始,每次向 main 分支推送代码时都会自动部署到生产环境:
# Simple GitHub Actions Workflow for Capgo Live Updatesname: Deploy to Capgo
on: push: branches: - main
jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v6
- name: Setup Node.js uses: actions/setup-node@v6 with: node-version: '24' cache: 'npm'
- name: Install, test and build run: | npm ci npm run test npm run build
- name: Deploy to Capgo run: | npm install -g @capgo/cli npx @capgo/cli bundle upload --channel production env: CAPGO_TOKEN: ${{ secrets.CAPGO_TOKEN }} # For encrypted uploads, add: --key-data-v2 "${{ secrets.CAPGO_PRIVATE_KEY }}"高级多频道配置
“高级多渠道配置”章节特性分支部署
“功能分支部署”章节将功能分支部署到临时渠道进行测试:
# Feature branch deploymentname: Deploy Feature Branch to Capgo
on: push: branches: - 'feature/**'
jobs: deploy-feature: runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 - uses: actions/setup-node@v6 with: node-version: '24' cache: 'npm'
- run: | npm ci npm run test npm run build
- name: Deploy to feature channel run: | CHANNEL_NAME=$(echo "${{ github.ref_name }}" | sed 's/[^a-zA-Z0-9]/-/g' | tr '[:upper:]' '[:lower:]') npm install -g @capgo/cli npx @capgo/cli channel add $CHANNEL_NAME --apikey ${{ secrets.CAPGO_TOKEN }} || true npx @capgo/cli bundle upload --apikey ${{ secrets.CAPGO_TOKEN }} --channel $CHANNEL_NAME使用加密
“使用加密”章节如果你在使用 Capgo 的加密功能,你需要将私钥安全地存储在 CI/CD 环境中。
在 设置加密密钥 在本地,将您的私钥添加到 GitHub secrets 中:
# Display your private key content (copy this output)cat .capgo_key_v2将此内容作为 CAPGO_PRIVATE_KEY 添加到您的 GitHub 仓库 secrets 中,然后在 workflows 中使用:
# Deploy with encryption- name: Deploy to Capgo with Encryption run: | npm install -g @capgo/cli npx @capgo/cli bundle upload --apikey ${{ secrets.CAPGO_TOKEN }} --key-data-v2 "${{ secrets.CAPGO_PRIVATE_KEY }}" --channel production多通道配置
标题为“多通道配置”的章节如需全面了解如何设置和管理多个部署渠道,请参阅 Channels 文档.
涵盖开发、Pull Request 及生产环境部署的完整工作流:
# Complete multi-environment workflowname: Deploy to Capgo
on: push: branches: [main, develop] pull_request: branches: [main, develop]
jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 - uses: actions/setup-node@v6 with: node-version: '24' cache: 'npm'
- run: | npm ci npm run test npm run build
- uses: actions/upload-artifact@v6 with: name: dist path: dist/
deploy-development: if: github.ref == 'refs/heads/develop' needs: build runs-on: ubuntu-latest environment: development steps: - uses: actions/setup-node@v6 with: node-version: '24'
- uses: actions/download-artifact@v4 with: name: dist path: dist/
- run: | npm install -g @capgo/cli npx @capgo/cli bundle upload --apikey ${{ secrets.CAPGO_TOKEN }} --channel development
deploy-pr: if: github.event_name == 'pull_request' needs: build runs-on: ubuntu-latest steps: - uses: actions/setup-node@v6 with: node-version: '24'
- uses: actions/download-artifact@v4 with: name: dist path: dist/
- name: Deploy to PR channel run: | CHANNEL_NAME="pr-${{ github.event.number }}" npm install -g @capgo/cli npx @capgo/cli channel add $CHANNEL_NAME --apikey ${{ secrets.CAPGO_TOKEN }} || true npx @capgo/cli bundle upload --apikey ${{ secrets.CAPGO_TOKEN }} --channel $CHANNEL_NAME
- name: Comment PR uses: actions/github-script@v7 with: script: | github.rest.issues.createComment({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, body: `🚀 This PR has been deployed to Capgo channel: \`pr-${{ github.event.number }}\`\n\nTo test this update in your app, configure it to use this channel. [Learn how to configure channels →](/docs/live-updates/channels/#configuring-the-channel-in-your-app)` })
deploy-production: if: github.ref == 'refs/heads/main' needs: build runs-on: ubuntu-latest environment: production steps: - uses: actions/setup-node@v6 with: node-version: '24'
- uses: actions/download-artifact@v4 with: name: dist path: dist/
- run: | npm install -g @capgo/cli npx @capgo/cli bundle upload --apikey ${{ secrets.CAPGO_TOKEN }} --channel production清理功能频道
“清理 Feature Channels”章节当分支被删除时,自动清理对应的 feature channels:
name: Cleanup Feature Channels
on: delete:
jobs: cleanup: runs-on: ubuntu-latest if: github.event.ref_type == 'branch' && startsWith(github.event.ref, 'feature/') steps: - uses: actions/setup-node@v6 with: node-version: '24'
- name: Delete Capgo channel run: | CHANNEL_NAME=$(echo "${{ github.event.ref }}" | sed 's/[^a-zA-Z0-9]/-/g' | tr '[:upper:]' '[:lower:]') npm install -g @capgo/cli npx @capgo/cli channel delete $CHANNEL_NAME --apikey ${{ secrets.CAPGO_TOKEN }} || true安全与最佳实践
“安全与最佳实践”章节环境防护规则
“环境保护规则”章节在 GitHub 中设置环境保护规则:
- 前往 设置 → Environments 在你的仓库中
- 创建环境:
development,staging,production - 生产环境请添加:
- 必需审核者:添加必须审批部署的团队成员
- 等待计时器:在部署前添加延迟(可选)
- 部署分支:限制为
main仅限该分支
Secrets 安全管理
标题为“安全密钥管理”的章节使用按环境区分的 secrets:
# Use different secrets per environmentdeploy-production: environment: production steps: - name: Deploy to Production run: | npx @capgo/cli bundle upload \ --apikey ${{ secrets.CAPGO_PROD_TOKEN }} \ --app ${{ secrets.CAPGO_PROD_APP_ID }} \ --channel production监控与通知
标题为“监控与通知”的章节Slack 集成
标题为“Slack 集成”的章节在工作流中添加 Slack 通知:
name: Deploy with Notifications
jobs: deploy: runs-on: ubuntu-latest steps: # ... deployment steps
- name: Notify Slack on Success if: success() uses: 8398a7/action-slack@v3 with: status: success text: '✅ Capgo deployment successful!' fields: repo,message,commit,author,action,eventName,ref,workflow env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
- name: Notify Slack on Failure if: failure() uses: 8398a7/action-slack@v3 with: status: failure text: '❌ Capgo deployment failed!' fields: repo,message,commit,author,action,eventName,ref,workflow env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}Discord 集成
标题为“Discord 集成”的章节向 Discord 发送通知:
- name: Discord notification if: always() uses: Ilshidur/action-discord@master with: args: | Capgo deployment ${{ job.status }}! App: ${{ secrets.CAPGO_APP_ID }} Channel: ${{ github.ref_name }} Commit: ${{ github.sha }} env: DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}邮件通知
章节标题为“邮件通知”配置邮件通知:
- name: Send email notification if: failure() uses: dawidd6/action-send-mail@v3 with: server_address: smtp.gmail.com server_port: 465 username: ${{ secrets.EMAIL_USERNAME }} password: ${{ secrets.EMAIL_PASSWORD }} subject: 'Capgo Deployment Failed - ${{ github.repository }}' to: team@yourcompany.com from: ci-cd@yourcompany.com body: | Deployment failed for ${{ github.repository }} Branch: ${{ github.ref_name }} Commit: ${{ github.sha }} Workflow: ${{ github.workflow }}故障排除
“故障排除”章节调试 Workflow
标题为“调试 Workflow”的章节添加调试步骤以排查问题:
- name: Debug environment run: | echo "Node version: $(node --version)" echo "NPM version: $(npm --version)" echo "Working directory: $(pwd)" echo "Files in dist/: $(ls -la dist/ || echo 'No dist directory')" echo "Environment variables:" env | grep -E "(GITHUB_|CAPGO_)" | sort
- name: Test Capgo CLI run: | npx @capgo/cli --version npx @capgo/cli app debug --apikey ${{ secrets.CAPGO_TOKEN }} --app ${{ secrets.CAPGO_APP_ID }}常见问题与解决方案
标题为“常见问题与解决方案”的章节Workflow 报错 “CAPGO_TOKEN not found”:
- name: Verify secrets run: | if [ -z "${{ secrets.CAPGO_TOKEN }}" ]; then echo "ERROR: CAPGO_TOKEN secret is not set" exit 1 fi echo "CAPGO_TOKEN is set (length: ${#CAPGO_TOKEN})" env: CAPGO_TOKEN: ${{ secrets.CAPGO_TOKEN }}找不到构建产物:
- name: Debug artifacts run: | echo "Checking for build artifacts..." ls -la dist/ || echo "No dist directory found" find . -name "*.js" -o -name "*.html" | head -10网络连接问题:
- name: Test connectivity run: | ping -c 3 api.capgo.io || echo "Ping failed" curl -I https://api.capgo.io/health || echo "Health check failed"可复用 Workflow
标题为 “可复用 Workflow” 的章节创建可复用的 workflow 以保持各项目的一致性:
name: Reusable Capgo Deploy
on: workflow_call: inputs: environment: required: true type: string channel: required: true type: string secrets: CAPGO_TOKEN: required: true CAPGO_APP_ID: required: true
jobs: deploy: runs-on: ubuntu-latest environment: ${{ inputs.environment }} steps: - uses: actions/checkout@v6
- name: Setup Node.js uses: actions/setup-node@v6 with: node-version: '24' cache: 'npm'
- name: Install and build run: | npm ci npm run build
- name: Deploy to Capgo run: | npm install -g @capgo/cli npx @capgo/cli bundle upload \ --apikey ${{ secrets.CAPGO_TOKEN }} \ --app ${{ secrets.CAPGO_APP_ID }} \ --channel ${{ inputs.channel }}使用该可复用 workflow:
name: Deploy App
on: push: branches: [main, develop]
jobs: deploy-dev: if: github.ref == 'refs/heads/develop' uses: ./.github/workflows/reusable-capgo-deploy.yml with: environment: development channel: development secrets: CAPGO_TOKEN: ${{ secrets.CAPGO_TOKEN }} CAPGO_APP_ID: ${{ secrets.CAPGO_APP_ID }}
deploy-prod: if: github.ref == 'refs/heads/main' uses: ./.github/workflows/reusable-capgo-deploy.yml with: environment: production channel: production secrets: CAPGO_TOKEN: ${{ secrets.CAPGO_TOKEN }} CAPGO_APP_ID: ${{ secrets.CAPGO_APP_ID }}下一步
“后续步骤”章节通过 GitHub Actions 集成,你可以利用 GitHub 强大的 CI/CD 平台,为你的 Capgo Live Updates 构建包含内置安全、监控和协作功能的复杂部署工作流。
继续探索 GitHub Actions 集成
标题为“继续探索 GitHub Actions 集成”如果你正在使用 GitHub Actions 集成 规划 CI/CD 自动化,并将其与 Capgo CI/CD 用于 Capgo CI/CD 中的产品工作流, Capgo Native Build 用于 Capgo Native Builds 中的产品工作流, Capgo 集成 用于 Capgo 集成中的产品工作流, CI/CD 集成 了解 CI/CD 集成的实现细节,以及 GitLab CI/CD 集成 有关 GitLab CI/CD 集成中的实现细节。