Lompat ke konten

Integrasi GitLab CI/CD

Integrasikan Capgo Live Updates dengan GitLab CI/CD untuk memperbarui otomatis aplikasi Anda setiap kali Anda mendorong code perubahan. Petunjuk ini mencakup pengaturan build otomatis, pengujian, dan alur kerja pengiriman.

Sebelum mengatur integrasi GitLab CI/CD, pastikan Anda memiliki:

  • Akun GitLab dengan repositori proyek
  • Salinan Capgo dengan aplikasi yang dikonfigurasi
  • Node.js dan npm/yarn dikonfigurasi di proyek Anda

Langkah 1: Konfigurasi Variabel Lingkungan

Judul Bagian: “Langkah 1: Konfigurasi Variabel Lingkungan”

Pertama-tama, atur variabel yang diperlukan di proyek GitLab Anda:

  1. Pergi ke proyek GitLab Anda
  2. Pergi ke PengaturanCI/CDVariabel
  3. Tambahkan variabel berikut:
Nama VariabelNilaiTerlindungiMembungkus
CAPGO_TOKENKunci Capgo API Anda✅ Ya✅ Ya

Konfigurasi dasar yang mengirimkan ke produksi setiap kali push ke cabang utama:

# .gitlab-ci.yml - Simple Configuration
image: 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

Mengirimkan cabang fitur ke saluran uji untuk tinjauan dan pengujian:

# Feature branch deployment
deploy_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

Jika Anda menggunakan fitur enkripsi Capgo, Anda perlu menyimpan kunci pribadi Anda dengan aman di lingkungan CI/CD Anda.

Setelah mengatur kunci enkripsi lokal, tambahkan kunci pribadi Anda ke variabel GitLab:

Jendela terminal
# Display your private key content (copy this output)
cat .capgo_key_v2

Tambahkan konten ini sebagai CAPGO_PRIVATE_KEY dalam variabel proyek GitLab Anda (tandai sebagai terlindungi dan tersembunyi), kemudian gunakan di pipeline:

# Deploy with encryption
deploy_production:
script:
- npm install -g @capgo/cli
- npx @capgo/cli bundle upload --apikey $CAPGO_TOKEN --key-data-v2 "$CAPGO_PRIVATE_KEY" --channel production

Konfigurasi Multi-Kanal

Konfigurasi Multi-Kanal

Untuk informasi yang lebih komprehensif tentang pengaturan dan pengelolaan saluran-deployan yang banyak, lihat Dokumentasi Saluran .

Konfigurasi lengkap dengan beberapa lingkungan dan pengiriman permintaan gabungan:

# .gitlab-ci.yml - Advanced Multi-Channel Configuration
image: node:22
stages:
- build
- deploy
variables:
npm_config_cache: "$CI_PROJECT_DIR/.npm"
# Build stage
build:
stage: build
script:
- npm ci
- npm run test
- npm run build
artifacts:
paths:
- dist/
expire_in: 24 hours
# Deploy to development channel
deploy_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 channels
deploy_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 closed
cleanup_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 staging
deploy_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 production
deploy_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

Multi-Environment dengan Persetujuan Manual

Bagian berjudul “Multi-Environment dengan Persetujuan Manual”

Untuk pengiriman produksi yang memerlukan persetujuan manual:

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

Pengiriman cabang yang berbeda ke saluran yang sesuai secara otomatis:

# Dynamic channel deployment based on branch
deploy:
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
  1. Mark Variabel yang Sensitive: Selalu tandai token API sebagai terlindungi dan tersembunyi
  2. Pelindungan Cabang: Gunakan variabel terlindungi untuk pengiriman produksi
  3. Pengendalian Akses: Batasi akses variabel hanya untuk pengelola
  4. Penggantian Rutin: Ganti token API secara berkala

Konfigurasi Pipa Sumber Daya yang Aman

Judul Bagian “Konfigurasi Pipa Sumber Daya yang Aman”
# Use protected variables for production
deploy_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"

Tambahkan Pemberitahuan Slack ke Pipa Anda:

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_failure

Konfigurasi pemberitahuan email di pengaturan proyek GitLab Anda atau gunakan 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

Pipeline gagal dengan “Capgo CLI tidak ditemukan”:

# Debug CLI installation
debug_cli:
script:
- npm install -g @capgo/cli
- which capgo || echo "Capgo CLI not found"
- npx @capgo/cli --version

Kesalahan Autentikasi:

# Verify token configuration
debug_auth:
script:
- |
if [ -z "$CAPGO_TOKEN" ]; then
echo "CAPGO_TOKEN is not set"
exit 1
fi
echo "Token length: ${#CAPGO_TOKEN}"

Jenis Bahan Bangunan tidak ditemukan:

# List build outputs
debug_build:
script:
- ls -la dist/
- find dist/ -type f -name "*.js" -o -name "*.html"

Tambahkan informasi debugging untuk menyelesaikan masalah:

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

Dengan integrasi GitLab CI/CD, Anda dapat mengotomatisasi Capgo Anda dan memastikan pembaruan yang konsisten dan dapat diandalkan untuk pengguna aplikasi seluler Anda.

Jika Anda menggunakan Integrasi GitLab CI/CD untuk merencanakan otomatisasi CI/CD, hubungkannya dengan Capgo CI/CD Untuk alur kerja produk di Capgo CI/CD, Capgo Pembangunan Asli Untuk alur kerja produk di Capgo Pembangunan Asli, Capgo Integrasi Untuk alur kerja produk di Capgo Integrasi, Integrasi CI/CD Untuk detail implementasi di Integrasi CI/CD, dan GitHub Integrasi Aksi Untuk detail implementasi di GitHub Integrasi Aksi.