Section 8/171 menit

8. Environment Variables

8. Environment Variables

Built-in Environment Variables

Xcode Cloud menyediakan variabel environment bawaan yang bisa digunakan di ci_scripts:

swift
# ci_scripts/ci_post_clone.sh

# Informasi build
echo "Product: $CI_PRODUCT"              # Nama product (app name)
echo "Bundle ID: $CI_BUNDLE_ID"          # com.company.app
echo "Xcode version: $CI_XCODE_VERSION"  # misal: Xcode_16.2

# Informasi Git
echo "Branch: $CI_BRANCH"               # develop, main, feature/login
echo "Tag: $CI_TAG"                     # v1.2.3 (kosong jika bukan tag build)
echo "Commit hash: $CI_COMMIT"          # SHA commit

# Informasi build
echo "Build number: $CI_BUILD_NUMBER"   # auto-increment dari Apple
echo "Build ID: $CI_BUILD_ID"           # UUID unik tiap build

# Environment
echo "Is CI: $CI_XCODEBUILD_ACTION"     # build, test, analyze, archive
echo "Artifact dir: $CI_ARCHIVE_PATH"   # path ke .xcarchive (saat archive)
echo "Test result: $CI_RESULT_BUNDLE_PATH"  # path ke .xcresult

Custom Environment Variables

Tambahkan secrets dan konfigurasi custom di workflow:

swift
Workflow → Environment → Environment Variables:
├── API_BASE_URL = https://api.staging.example.com  (plain text)
├── SENTRY_DSN = https://xxx@sentry.io/123           (plain text)
└── FIREBASE_APP_ID = 1:123:ios:abc123               (secret — nilai tersembunyi)

Variabel ini tersedia di ci_scripts dan bisa di-inject ke build melalui script:

swift
#!/bin/zsh
# ci_scripts/ci_pre_xcodebuild.sh

# Inject API URL ke xcconfig berdasarkan branch
if [[ "$CI_BRANCH" == "main" ]]; then
    API_URL="https://api.production.example.com"
elif [[ "$CI_BRANCH" == "develop" ]]; then
    API_URL="${API_BASE_URL:-https://api.staging.example.com}"
else
    API_URL="https://api.dev.example.com"
fi

# Tulis ke xcconfig yang di-include oleh project
cat > "$CI_WORKSPACE/Configuration/CI.xcconfig" << EOF
API_BASE_URL = $API_URL
BUILD_NUMBER = $CI_BUILD_NUMBER
EOF

echo "Injected API_URL: $API_URL"

Pola Penggunaan Secrets

Jangan hardcode secrets di source code. Gunakan environment variables Xcode Cloud:

swift
#!/bin/zsh
# ci_scripts/ci_post_clone.sh

# Decode Google Services plist dari secret
# Secret berisi base64-encoded GoogleService-Info.plist
echo "$GOOGLE_SERVICE_INFO_PLIST_BASE64" | base64 --decode \
    > "$CI_PRIMARY_REPOSITORY_PATH/MyApp/GoogleService-Info.plist"

# Setup Firebase App Distribution credentials
echo "$FIREBASE_SERVICE_ACCOUNT_JSON_BASE64" | base64 --decode \
    > /tmp/firebase-service-account.json