GitLab CI/CD統合
Capgo Live UpdatesをGitLab CI/CDと統合して、コード変更をプッシュするたびにアプリのアップデートを自動的にデプロイします。このガイドでは、自動ビルド、テスト、デプロイメントワークフローの設定について説明します。
GitLab CI/CD統合を設定する前に、以下を確認してください:
- プロジェクトリポジトリを持つGitLabアカウント
- アプリが構成されたCapgoアカウント
- プロジェクトでNode.jsとnpm/yarnが構成されていること
GitLab CI/CDの設定
Section titled “GitLab CI/CDの設定”ステップ1: 環境変数の構成
Section titled “ステップ1: 環境変数の構成”まず、GitLabプロジェクトで必要な変数を設定します:
- GitLabプロジェクトに移動
- Settings → CI/CD → Variablesに移動
- 以下の変数を追加:
| 変数名 | 値 | Protected | Masked |
|---|---|---|---|
CAPGO_TOKEN | Capgo APIトークン | ✅ はい | ✅ はい |
mainブランチへのプッシュごとに本番環境にデプロイする基本構成:
# .gitlab-ci.yml - Simple Configurationimage: node:22
stages: - build - deploy
variables: npm_config_cache: "$CI_PROJECT_DIR/.npm"
build: stage: build script: - npm ci - npm run test - npm run build artifacts: paths: - dist/ expire_in: 1 hour only: - main
deploy_production: stage: deploy script: - npm install -g @capgo/cli - npx @capgo/cli bundle upload --apikey $CAPGO_TOKEN --channel production # For encrypted uploads, add: --key-data-v2 "$CAPGO_PRIVATE_KEY" dependencies: - build only: - mainフィーチャーブランチのデプロイメント
Section titled “フィーチャーブランチのデプロイメント”レビューとテストのためにフィーチャーブランチをテストチャネルにデプロイ:
# Feature branch deploymentdeploy_feature: stage: deploy script: - npm install -g @capgo/cli - CHANNEL_NAME="feature-$(echo $CI_COMMIT_REF_NAME | sed 's/[^a-zA-Z0-9-]/-/g')" - npx @capgo/cli channel create $CHANNEL_NAME --apikey $CAPGO_TOKEN || true - npx @capgo/cli bundle upload --apikey $CAPGO_TOKEN --channel $CHANNEL_NAME dependencies: - build only: - /^feature\/.*$/ environment: name: feature/$CI_COMMIT_REF_NAME url: https://your-app.com/channels/$CHANNEL_NAME暗号化の使用
Section titled “暗号化の使用”Capgoの暗号化機能を使用している場合、CI/CD環境にプライベートキーを安全に保存する必要があります。
ローカルで暗号化キーを設定した後、プライベートキーをGitLab変数に追加します:
# プライベートキーの内容を表示(この出力をコピー)cat .capgo_key_v2この内容をCAPGO_PRIVATE_KEYとしてGitLabプロジェクト変数に追加し(protectedおよびmaskedとしてマーク)、パイプラインで使用します:
# Deploy with encryptiondeploy_production: script: - npm install -g @capgo/cli - npx @capgo/cli bundle upload --apikey $CAPGO_TOKEN --key-data-v2 "$CAPGO_PRIVATE_KEY" --channel productionマルチチャネル構成
Section titled “マルチチャネル構成”複数のデプロイメントチャネルの設定と管理に関する包括的な情報については、チャネルドキュメントを参照してください。
複数の環境とマージリクエストのデプロイメントを含む完全な構成:
# .gitlab-ci.yml - Advanced Multi-Channel Configurationimage: node:22
stages: - build - deploy
variables: npm_config_cache: "$CI_PROJECT_DIR/.npm"
# Build stagebuild: stage: build script: - npm ci - npm run test - npm run build artifacts: paths: - dist/ expire_in: 24 hours
# Deploy to development channeldeploy_development: stage: deploy script: - npm install -g @capgo/cli - npx @capgo/cli bundle upload --apikey $CAPGO_TOKEN --channel development dependencies: - build only: - develop environment: name: development
# Deploy merge requests to test channelsdeploy_mr: stage: deploy script: - npm install -g @capgo/cli - CHANNEL_NAME="mr-$CI_MERGE_REQUEST_IID" - npx @capgo/cli channel create $CHANNEL_NAME --apikey $CAPGO_TOKEN || true - npx @capgo/cli bundle upload --apikey $CAPGO_TOKEN --channel $CHANNEL_NAME dependencies: - build only: - merge_requests environment: name: review/$CI_MERGE_REQUEST_IID url: https://your-app.com/channels/mr-$CI_MERGE_REQUEST_IID on_stop: cleanup_mr
# Cleanup MR channels when MR is closedcleanup_mr: stage: deploy script: - npm install -g @capgo/cli - npx @capgo/cli channel delete mr-$CI_MERGE_REQUEST_IID --apikey $CAPGO_TOKEN || true when: manual environment: name: review/$CI_MERGE_REQUEST_IID action: stop only: - merge_requests
# Deploy to stagingdeploy_staging: stage: deploy script: - npm install -g @capgo/cli - npx @capgo/cli bundle upload --apikey $CAPGO_TOKEN --channel staging dependencies: - build only: - develop environment: name: staging
# Deploy to productiondeploy_production: stage: deploy script: - npm install -g @capgo/cli - npx @capgo/cli bundle upload --apikey $CAPGO_TOKEN --channel production dependencies: - build only: - main environment: name: production手動承認を伴うマルチ環境
Section titled “手動承認を伴うマルチ環境”手動承認が必要な本番デプロイメントの場合:
deploy_production: stage: deploy script: - npm install -g @capgo/cli - npx @capgo/cli bundle upload --apikey $CAPGO_TOKEN --channel production dependencies: - build only: - main when: manual environment: name: productionブランチベースのデプロイメント戦略
Section titled “ブランチベースのデプロイメント戦略”異なるブランチを適切なチャネルに自動的にデプロイ:
# Dynamic channel deployment based on branchdeploy: stage: deploy script: - npm install -g @capgo/cli - | if [ "$CI_COMMIT_REF_NAME" = "main" ]; then CHANNEL="production" elif [ "$CI_COMMIT_REF_NAME" = "develop" ]; then CHANNEL="staging" else CHANNEL="development" fi - npx @capgo/cli bundle upload --apikey $CAPGO_TOKEN --channel $CHANNEL dependencies: - build environment: name: $CHANNELセキュリティのベストプラクティス
Section titled “セキュリティのベストプラクティス”Protected変数
Section titled “Protected変数”- 機密変数をマーク: APIトークンは常にprotectedおよびmaskedとしてマーク
- ブランチ保護: 本番デプロイメント用にprotected変数を使用
- アクセス制御: 変数へのアクセスをメンテナーのみに制限
- 定期的なローテーション: APIトークンを定期的にローテーション
安全なパイプライン構成
Section titled “安全なパイプライン構成”# Use protected variables for productiondeploy_production: stage: deploy script: - npm install -g @capgo/cli - npx @capgo/cli bundle upload --apikey $CAPGO_TOKEN --channel production only: refs: - main variables: - $CI_COMMIT_REF_PROTECTED == "true"Slack統合
Section titled “Slack統合”パイプラインにSlack通知を追加:
notify_success: stage: .post image: alpine:latest before_script: - apk add --no-cache curl script: - | curl -X POST -H 'Content-type: application/json' \ --data '{"text":"✅ Capgo deployment successful for '"$CI_COMMIT_REF_NAME"'"}' \ $SLACK_WEBHOOK_URL when: on_success
notify_failure: stage: .post image: alpine:latest before_script: - apk add --no-cache curl script: - | curl -X POST -H 'Content-type: application/json' \ --data '{"text":"❌ Capgo deployment failed for '"$CI_COMMIT_REF_NAME"'"}' \ $SLACK_WEBHOOK_URL when: on_failureGitLabプロジェクト設定でメール通知を構成、またはAPIを使用:
notify_email: stage: .post script: - | curl --request POST \ --header "PRIVATE-TOKEN: $GITLAB_API_TOKEN" \ --form "to=team@yourcompany.com" \ --form "subject=Capgo Deployment Status" \ --form "body=Deployment of $CI_COMMIT_REF_NAME completed with status: $CI_JOB_STATUS" \ "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/emails" when: alwaysトラブルシューティング
Section titled “トラブルシューティング”一般的な問題
Section titled “一般的な問題”「Capgo CLI not found」でパイプラインが失敗:
# Debug CLI installationdebug_cli: script: - npm install -g @capgo/cli - which capgo || echo "Capgo CLI not found" - npx @capgo/cli --version認証エラー:
# Verify token configurationdebug_auth: script: - | if [ -z "$CAPGO_TOKEN" ]; then echo "CAPGO_TOKEN is not set" exit 1 fi echo "Token length: ${#CAPGO_TOKEN}"ビルドアーティファクトが見つからない:
# List build outputsdebug_build: script: - ls -la dist/ - find dist/ -type f -name "*.js" -o -name "*.html"デバッグパイプライン
Section titled “デバッグパイプライン”問題をトラブルシューティングするためのデバッグ情報を追加:
debug: stage: build script: - echo "Branch: $CI_COMMIT_REF_NAME" - echo "Commit: $CI_COMMIT_SHA" - echo "Build: $CI_PIPELINE_ID" - env | grep CI_ | sort only: - branches次のステップ
Section titled “次のステップ”- チャネルについて学び、異なるデプロイメント環境を管理
- 高度なデプロイメントシナリオのためのカスタムストレージを探索
- 安全なデプロイメントのための暗号化を設定
- アップデートの適用方法をカスタマイズするためのアップデート動作を構成
GitLab CI/CD統合により、Capgoデプロイメントを自動化し、モバイルアプリユーザーへの一貫性のある信頼性の高いアップデートを保証できます。