# CodePush Deployment

CodePush enables over-the-air JavaScript updates without requiring a full app store release.

## Overview

CodePush allows you to:
- Deploy bug fixes instantly
- Update JavaScript and assets
- Roll out features gradually
- Rollback problematic releases

**Important:** CodePush only updates JavaScript and assets. Native code changes require a full app store release.

## Setup

### Configuration

```bash
# Copy CodePush configuration template
cp .code-push.config.qa.shadow .code-push.config

# Edit with your access key
```

### Authentication

```bash
# Check who is logged in
LOCALAPPDATA=. yarn code-push-standalone whoami

# List available apps
LOCALAPPDATA=. yarn code-push-standalone app ls
```

## Monitoring

### DataDog Dashboards

Monitor CodePush deployments:
- [Production Dashboard](https://app.datadoghq.com/dashboard/bk2-s6f-vtz/prod-code-push-server-release-radar)
- [QA Dashboard](https://app.datadoghq.com/dashboard/74n-raq-nip)

## Deployment Structure

OrchardGo has separate CodePush apps for each platform:

- **iOS**: `OrchardGoIOS`
  - Production: `OrchardGoIOS/Production`
  - QA: `OrchardGoIOS/QA`

- **Android**: `OrchardGoAndroid`
  - Production: `OrchardGoAndroid/Production`
  - QA: `OrchardGoAndroid/QA`

## Deployment Workflow

### 1. Deploy to QA

```bash
# Deploy JavaScript bundle to QA
yarn deploy:js --env qa --platform ios
yarn deploy:js --env qa --platform android
```

### 2. Test in QA

- Install QA build on device
- Verify CodePush update downloads
- Test functionality thoroughly

### 3. Promote to Production

```bash
# Promote from QA to Production
yarn promote

# Or manually per platform
LOCALAPPDATA=. yarn code-push-standalone promote OrchardGoIOS QA Production --noDuplicateReleaseError
LOCALAPPDATA=. yarn code-push-standalone promote OrchardGoAndroid QA Production --noDuplicateReleaseError
```

## Platform-Specific Commands

### iOS Commands

#### View Collaborators
```bash
LOCALAPPDATA=. DEBUG=* yarn code-push-standalone collaborator ls OrchardGoIOS
```

#### View Deployments
```bash
LOCALAPPDATA=. DEBUG=* yarn code-push-standalone deployment ls OrchardGoIOS --displayKeys
```

#### View Production History
```bash
LOCALAPPDATA=. DEBUG=* yarn code-push-standalone deployment history OrchardGoIOS Production
```

#### View QA History
```bash
LOCALAPPDATA=. DEBUG=* yarn code-push-standalone deployment history OrchardGoIOS QA
```

#### Disable Release
```bash
LOCALAPPDATA=. DEBUG=* yarn code-push-standalone patch OrchardGoIOS Production --targetRelease v123 --disabled true
```

#### Enable Release
```bash
LOCALAPPDATA=. DEBUG=* yarn code-push-standalone patch OrchardGoIOS Production --targetRelease v123 --disabled false
```

### Android Commands

#### View Collaborators
```bash
LOCALAPPDATA=. DEBUG=* yarn code-push-standalone collaborator ls OrchardGoAndroid
```

#### View Deployments
```bash
LOCALAPPDATA=. DEBUG=* yarn code-push-standalone deployment ls OrchardGoAndroid --displayKeys
```

#### View Production History
```bash
LOCALAPPDATA=. DEBUG=* yarn code-push-standalone deployment history OrchardGoAndroid Production
```

#### View QA History
```bash
LOCALAPPDATA=. DEBUG=* yarn code-push-standalone deployment history OrchardGoAndroid QA
```

#### Promote to Production
```bash
LOCALAPPDATA=. DEBUG=* yarn code-push-standalone promote OrchardGoAndroid QA Production --noDuplicateReleaseError
```

## Configuration Scripts

### Enable/Disable CodePush

```bash
# Enable CodePush updates
yarn codepush:enable

# Disable CodePush updates
yarn codepush:disable
```

### View History

```bash
# View deployment history (all platforms)
yarn codepush:history
```

## Deployment Keys

Each deployment has unique keys for:
- Production environment
- QA environment

View deployment keys:
```bash
LOCALAPPDATA=. yarn code-push-standalone deployment ls [APP_NAME] --displayKeys
```

## Rollout Strategies

### Instant Rollout (Default)

```bash
# Deploy to all users immediately
yarn deploy:js --env prod
```

### Gradual Rollout

```bash
# Deploy to 25% of users
LOCALAPPDATA=. yarn code-push-standalone release OrchardGoIOS Production \
  --deploymentName Production \
  --rollout 25
```

After monitoring, increase rollout:
```bash
LOCALAPPDATA=. yarn code-push-standalone patch OrchardGoIOS Production \
  --rollout 50
```

### Targeted Rollout

```bash
# Deploy only to specific app version
LOCALAPPDATA=. yarn code-push-standalone release OrchardGoIOS Production \
  --targetBinaryVersion "2.18.0"
```

## Rollback

### Disable Problematic Release

```bash
# Disable specific release
LOCALAPPDATA=. yarn code-push-standalone patch OrchardGoIOS Production \
  --targetRelease v123 \
  --disabled true
```

### Rollback to Previous Version

```bash
# Rollback to previous release
LOCALAPPDATA=. yarn code-push-standalone rollback OrchardGoIOS Production
```

## Debugging CodePush

### Temporary Debugging Setup

**⚠️ WARNING:** These changes break CodePush and Sentry. Do not commit!

#### Enable Hermes for Debugging

**iOS** (`ios/Podfile:61`):
```ruby
:hermes_enabled => true  # Change false to true
```

**Android** (`android/gradle.properties:39`):
```properties
hermesEnabled=true  # Change false to true
```

Then reinstall:
```bash
yarn install
```

#### Debug with Chrome DevTools

Now you can use the debugger. Access the dev menu:
- iOS Simulator: `Cmd + D`
- Android Emulator: `Cmd + M`

**Note:** New debugger doesn't have Network Inspector. Use proxy tools like Proxyman or Charles.

#### Revert Changes

```bash
# Revert Hermes changes
git checkout ios/Podfile android/gradle.properties

# Reinstall
yarn install
```

## Best Practices

### Before Deployment

1. **Test thoroughly in QA**
   - Deploy to QA first
   - Test on multiple devices/OS versions
   - Verify critical flows work

2. **Version compatibility**
   - Ensure CodePush update is compatible with current native version
   - Test with oldest supported app version

3. **Bundle size**
   - Keep updates small for faster downloads
   - Remove unused code and assets

### During Deployment

1. **Monitor metrics**
   - Watch DataDog dashboards
   - Check download success rates
   - Monitor error rates in Sentry

2. **Gradual rollout**
   - Start with small percentage (10-25%)
   - Increase gradually after monitoring
   - Full rollout after confidence

### After Deployment

1. **Monitor for issues**
   - Check Sentry for new errors
   - Watch crash rates
   - Monitor user feedback

2. **Be ready to rollback**
   - Have rollback command ready
   - Monitor during rollout window
   - Disable quickly if issues arise

## Troubleshooting

### CodePush Update Not Installing

1. **Check deployment key**
   ```bash
   LOCALAPPDATA=. yarn code-push-standalone deployment ls OrchardGoIOS --displayKeys
   ```
   Verify key matches app configuration.

2. **Check target binary version**
   Ensure CodePush update targets correct app version.

3. **Check device connection**
   Verify device has internet connectivity.

### "Duplicate Release" Error

Use `--noDuplicateReleaseError` flag:
```bash
yarn promote -- --noDuplicateReleaseError
```

### Bundle Size Too Large

1. Reduce bundle size:
   - Remove unused dependencies
   - Optimize images
   - Use smaller libraries

2. Split updates:
   - Deploy in multiple smaller updates
   - Use native binary for large changes

## Limitations

CodePush **cannot** update:
- Native code (Swift, Objective-C, Java, Kotlin)
- Native dependencies
- App permissions
- Native modules
- iOS/Android configuration

These require full app store release.

## Next Steps

- [Building Binaries](./building.md)
- [Release Process](./release-process.md)
- [Debugging](../development/debugging.md)
