跳过内容

Azure DevOps 集成

将 Capgo Live Updates 与 Azure DevOps Pipelines 集成,以便在推送 code 变更时自动部署您的应用程序更新。该指南涵盖了设置自动构建、测试和部署工作流程。

在设置 Azure DevOps 集成之前,请确保您有:

  • Azure DevOps 组织和项目
  • 一个具有应用程序配置的 Capgo 账户
  • 您的应用程序的源 code 位于 Azure Repos Git 仓库
  • Node.js 和 npm/yarn 在您的项目中配置

设置 Azure DevOps Pipeline

标题:设置 Azure DevOps Pipeline

步骤 1:创建管道变量

标题:步骤 1:创建管道变量

首先,在 Azure DevOps 项目中设置必要的变量:

  1. 前往您的 Azure DevOps 项目
  2. 前往 管道变量组
  3. 创建一个名为 Capgo-Variables
  4. 添加以下变量:
变量名称安全
CAPGO_TOKENYour Capgo API token✅ 是

将应用程序部署到生产环境中的基本配置:每次推送到主分支时都会发生

# Simple Azure DevOps Pipeline for Capgo Live Updates
trigger:
branches:
include:
- main
variables:
- group: Capgo-Variables
jobs:
- job: BuildAndDeploy
displayName: 'Build and Deploy to Capgo'
pool:
vmImage: 'ubuntu-latest'
steps:
- task: NodeTool@0
displayName: 'Setup Node.js'
inputs:
versionSpec: '22.x'
- script: |
npm ci
npm run test
npm run build
displayName: 'Install, test and build'
- script: |
npm install -g @capgo/cli
npx @capgo/cli bundle upload --apikey $(CAPGO_TOKEN) --channel production
displayName: 'Deploy to Capgo'

功能分支部署

功能分支部署

将功能分支部署到测试频道进行审查和测试:

# Feature branch deployment
trigger:
branches:
include:
- feature/*
variables:
- group: Capgo-Variables
jobs:
- job: DeployFeature
displayName: 'Deploy Feature Branch'
pool:
vmImage: 'ubuntu-latest'
condition: startsWith(variables['Build.SourceBranch'], 'refs/heads/feature/')
steps:
- task: NodeTool@0
inputs:
versionSpec: '22.x'
- script: |
npm ci
npm run test
npm run build
displayName: 'Install, test and build'
- script: |
BRANCH_NAME=$(echo "$(Build.SourceBranchName)" | sed 's/[^a-zA-Z0-9-]/-/g')
CHANNEL_NAME="feature-$BRANCH_NAME"
npm install -g @capgo/cli
npx @capgo/cli channel create $CHANNEL_NAME --apikey $(CAPGO_TOKEN) || true
npx @capgo/cli bundle upload --apikey $(CAPGO_TOKEN) --channel $CHANNEL_NAME
displayName: 'Deploy to Feature Channel'

如果您正在使用 Capgo的加密功能您需要将您的私钥安全地存储在您的CI/CD环境中。

之后 在本地设置加密密钥后 添加您的私钥到Azure DevOps变量:

终端窗口
# Display your private key content (copy this output)
cat .capgo_key_v2

将此内容添加为 CAPGO_PRIVATE_KEY 在您的Azure DevOps变量组(标记为机密)中,然后在管道中使用:

# Deploy with encryption
- script: |
npm install -g @capgo/cli
npx @capgo/cli bundle upload --apikey $(CAPGO_TOKEN) --key-data-v2 "$(CAPGO_PRIVATE_KEY)" --channel production
displayName: 'Deploy to Capgo with Encryption'

多渠道配置

多渠道配置

有关设置和管理多个部署通道的详细信息,请参见 多渠道文档.

具有多个环境和拉取请求部署的完整配置:

# Advanced Azure DevOps Pipeline with Multiple Channels
trigger:
branches:
include:
- main
- develop
pr:
branches:
include:
- main
- develop
variables:
- group: Capgo-Variables
stages:
# Build stage
- stage: Build
jobs:
- job: BuildApp
pool:
vmImage: 'ubuntu-latest'
steps:
- task: NodeTool@0
inputs:
versionSpec: '22.x'
- script: |
npm ci
npm run test
npm run build
displayName: 'Install, test and build'
- task: PublishBuildArtifacts@1
inputs:
pathToPublish: 'dist'
artifactName: 'app-build'
# Deploy to development
- stage: DeployDev
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/develop'))
jobs:
- deployment: DeployDevelopment
environment: development
pool:
vmImage: 'ubuntu-latest'
strategy:
runOnce:
deploy:
steps:
- task: NodeTool@0
inputs:
versionSpec: '22.x'
- task: DownloadBuildArtifacts@0
inputs:
artifactName: 'app-build'
downloadPath: '$(Pipeline.Workspace)'
- script: |
npm install -g @capgo/cli
npx @capgo/cli bundle upload --apikey $(CAPGO_TOKEN) --channel development --path $(Pipeline.Workspace)/app-build
displayName: 'Deploy to Development'
# Deploy PR to test channel
- stage: DeployPR
condition: and(succeeded(), eq(variables['Build.Reason'], 'PullRequest'))
jobs:
- job: DeployPRChannel
pool:
vmImage: 'ubuntu-latest'
steps:
- task: NodeTool@0
inputs:
versionSpec: '22.x'
- task: DownloadBuildArtifacts@0
inputs:
artifactName: 'app-build'
downloadPath: '$(Pipeline.Workspace)'
- script: |
CHANNEL_NAME="pr-$(System.PullRequest.PullRequestNumber)"
npm install -g @capgo/cli
npx @capgo/cli channel create $CHANNEL_NAME --apikey $(CAPGO_TOKEN) || true
npx @capgo/cli bundle upload --apikey $(CAPGO_TOKEN) --channel $CHANNEL_NAME --path $(Pipeline.Workspace)/app-build
displayName: 'Deploy to PR Channel'
# Deploy to production
- stage: DeployProd
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
jobs:
- deployment: DeployProduction
environment: production
pool:
vmImage: 'ubuntu-latest'
strategy:
runOnce:
deploy:
steps:
- task: NodeTool@0
inputs:
versionSpec: '22.x'
- task: DownloadBuildArtifacts@0
inputs:
artifactName: 'app-build'
downloadPath: '$(Pipeline.Workspace)'
- script: |
npm install -g @capgo/cli
npx @capgo/cli bundle upload --apikey $(CAPGO_TOKEN) --channel production --path $(Pipeline.Workspace)/app-build
displayName: 'Deploy to Production'

多环境部署

多环境部署

对于复杂的场景,涉及多个环境:

# Extended pipeline with multiple environments
parameters:
- name: deployEnvironment
displayName: 'Deploy Environment'
type: string
default: 'staging'
values:
- staging
- production
variables:
- group: Capgo-Variables
- name: channelName
${{ if eq(parameters.deployEnvironment, 'production') }}:
value: 'production'
${{ else }}:
value: 'staging'
stages:
# Build stage
- stage: Build
jobs:
- job: BuildApp
pool:
vmImage: 'ubuntu-latest'
steps:
- task: NodeTool@0
inputs:
versionSpec: '22.x'
- script: |
npm ci
npm run test
npm run build
displayName: 'Install, test and build'
- task: PublishBuildArtifacts@1
inputs:
pathToPublish: 'dist'
artifactName: 'app-build'
- stage: DeployStaging
displayName: 'Deploy to Staging'
dependsOn: Build
condition: and(succeeded(), eq('${{ parameters.deployEnvironment }}', 'staging'))
jobs:
- deployment: DeployStaging
displayName: 'Deploy to Staging Channel'
pool:
vmImage: 'ubuntu-latest'
environment: 'staging'
strategy:
runOnce:
deploy:
steps:
- template: deploy-steps.yml
parameters:
channel: 'staging'
- stage: DeployProduction
displayName: 'Deploy to Production'
dependsOn: Build
condition: and(succeeded(), eq('${{ parameters.deployEnvironment }}', 'production'))
jobs:
- deployment: DeployProduction
displayName: 'Deploy to Production Channel'
pool:
vmImage: 'ubuntu-latest'
environment: 'production'
strategy:
runOnce:
deploy:
steps:
- template: deploy-steps.yml
parameters:
channel: 'production'

部署模板 (deploy-steps.yml)

部署模板(deploy-steps.yml)

创建可重用的模板文件 deploy-steps.yml:

deploy-steps.yml
parameters:
- name: channel
type: string
steps:
- task: NodeTool@0
displayName: 'Install Node.js'
inputs:
versionSpec: '22.x'
- task: DownloadBuildArtifacts@0
displayName: 'Download build artifacts'
inputs:
artifactName: 'app-build'
downloadPath: '$(System.ArtifactsDirectory)'
- script: |
npm install -g @capgo/cli
displayName: 'Install Capgo CLI'
- script: |
npx @capgo/cli bundle upload \
--apikey $(CAPGO_TOKEN) \
--channel ${{ parameters.channel }} \
--path $(System.ArtifactsDirectory)/app-build
displayName: 'Upload to Capgo (${{ parameters.channel }})'

分支部署策略

分支部署策略

根据 Git Branch 配置不同的部署策略:

trigger:
branches:
include:
- main
- develop
- feature/*
variables:
- group: Capgo-Variables
- name: targetChannel
${{ if eq(variables['Build.SourceBranch'], 'refs/heads/main') }}:
value: 'production'
${{ elseif eq(variables['Build.SourceBranch'], 'refs/heads/develop') }}:
value: 'staging'
${{ else }}:
value: 'development'
stages:
- stage: Build
jobs:
- job: BuildApp
pool:
vmImage: 'ubuntu-latest'
steps:
- task: NodeTool@0
inputs:
versionSpec: '22.x'
- script: |
npm ci
npm run test
npm run build
displayName: 'Install, test and build'
- task: PublishBuildArtifacts@1
inputs:
pathToPublish: 'dist'
artifactName: 'app-build'
- stage: Deploy
displayName: 'Deploy to $(targetChannel)'
dependsOn: Build
condition: succeeded()
jobs:
- deployment: DeployJob
displayName: 'Deploy to $(targetChannel) Channel'
pool:
vmImage: 'ubuntu-latest'
environment: '$(targetChannel)'
strategy:
runOnce:
deploy:
steps:
- template: deploy-steps.yml
parameters:
channel: '$(targetChannel)'

安全最佳实践

安全最佳实践

安全变量管理

安全变量管理
  1. 使用变量组在 Azure DevOps 变量组中存储敏感数据
  2. 将其标记为机密在所有的配置文件和环境变量中,始终将 API 类型的令牌和键标记为机密变量。
  3. 访问范围限制变量组的访问权限,仅允许特定管道和用户
  4. 旋转密钥: 定期轮换您的 Capgo API 令牌

监控和通知

“监控和通知”

团队集成

团队集成

在您的管道中添加 Microsoft Teams 通知:

- task: ms-teams-deploy-card@1.4.1
displayName: 'Notify Teams on Success'
condition: succeeded()
inputs:
webhookUri: '$(TEAMS_WEBHOOK_URL)'
title: 'Capgo Deployment Successful'
text: 'App deployed to $(targetChannel) channel'
themeColor: '00FF00'
- task: ms-teams-deploy-card@1.4.1
displayName: 'Notify Teams on Failure'
condition: failed()
inputs:
webhookUri: '$(TEAMS_WEBHOOK_URL)'
title: 'Capgo Deployment Failed'
text: 'Deployment to $(targetChannel) failed'
themeColor: 'FF0000'

电子邮件通知

电子邮件通知

配置部署状态的电子邮件通知:

- task: EmailReport@1
displayName: 'Send Email Report'
condition: always()
inputs:
sendMailConditionConfig: 'Always'
subject: 'Capgo Deployment Report - $(Build.BuildNumber)'
to: 'team@yourcompany.com'
body: |
Deployment Status: $(Agent.JobStatus)
Channel: $(targetChannel)
Build: $(Build.BuildNumber)
Commit: $(Build.SourceVersion)

故障排除

“故障排除”

常见问题

常见问题

管道出错:"Capgo CLI"未找到

# Ensure global installation
- script: |
npm install -g @capgo/cli
which capgo || echo "Capgo CLI not found in PATH"
displayName: 'Install and verify Capgo CLI'

认证错误:

# Verify token is correctly set
- script: |
echo "Token length: ${#CAPGO_TOKEN}"
if [ -z "$CAPGO_TOKEN" ]; then
echo "CAPGO_TOKEN is not set"
exit 1
fi
displayName: 'Verify Capgo token'
env:
CAPGO_TOKEN: $(CAPGO_TOKEN)

无法找到构建产物:

# List available artifacts for debugging
- script: |
ls -la $(System.ArtifactsDirectory)
find $(System.ArtifactsDirectory) -name "*.js" -o -name "*.html"
displayName: 'Debug artifacts'

调试管道

调试管道

添加调试步骤来解决问题:

- script: |
echo "Build.SourceBranch: $(Build.SourceBranch)"
echo "Build.BuildNumber: $(Build.BuildNumber)"
echo "Target Channel: $(targetChannel)"
displayName: 'Debug Pipeline Variables'
- script: |
npx @capgo/cli app debug --apikey $(CAPGO_TOKEN)
displayName: 'Debug Capgo App Status'

With Azure DevOps integration, you can automate your Capgo deployments and ensure consistent, reliable updates to your mobile app users.