# Releasing CodePush updates with Bitrise CI

You can release your CodePush updates via a [Bitrise CI](/bitrise-ci) Workflow. The process requires:

- Setting up CodePush credentials: create [Environment Variables](/bitrise-ci/configure-builds/environment-variables) and a Secret that hold the app IDs, the deployment name, the target versions, and a Bitrise API token.
- Creating a Workflow that creates the update packages for both iOS and Android, and uploads both to Bitrise.

## Configuring CodePush credentials on Bitrise

1. Get your Release Management app IDs ([how to find them](/release-management/codepush/codepush-cli/using-the-codepush-cli#setting-up-a-project)) and the names of your [CodePush deployments](/release-management/codepush/creating-a-codepush-deployment).
1. [Create Environment Variables](/bitrise-ci/configure-builds/environment-variables#setting-an-env-var-in-the-workflow-editor) for the connected app IDs, the deployment name, and the target versions:

   - `IOS_CONNECTED_APP_ID`: The app ID for the iOS app.
   - `ANDROID_CONNECTED_APP_ID`: The app ID for the Android app.
   - `CODEPUSH_DEPLOYMENT`: The name of the deployment to release to, for example `Staging`.
   - `APP_TARGET_VERSIONS`: The app versions that receive the update. Use an exact version or a [range expression](/release-management/codepush/creating-and-releasing-codepush-updates#target-versions), for example `1.2.0` or `>=1.5.0`.

   Deployments belong to an app, so the iOS and the Android app each have their own deployment. The two deployments can share a name, for example `Staging`. In that case, set `CODEPUSH_DEPLOYMENT` to `Staging` and Bitrise finds the right deployment for each app.

   Bitrise cannot check whether your update works with the native code of an app version. You decide which versions are compatible, so set `APP_TARGET_VERSIONS` yourself before each release.
1. [Create a Secret](/bitrise-ci/configure-builds/secrets#setting-a-secret) called `BITRISE_API_TOKEN` that holds your personal access token or workspace API token.

## Creating a Workflow for CodePush updates

This section shows you how to create a CI Workflow that will:

- Create an update package for iOS.
- Upload the iOS update package to Bitrise.
- Create an update package for Android.
- Upload the Android update package to Bitrise.

You can create these update packages and upload them with either the Bitrise CodePush Publish Step or the [CodePush CLI](/release-management/codepush/codepush-cli/about-the-codepush-cli). Both detect whether your project uses bare React Native or Expo, and each run handles one platform, so add it once for iOS and once for Android.

### Using the Bitrise CodePush Publish Step

We recommend the Step for most projects. It installs your JavaScript dependencies, bundles your app, and uploads the update. Add it after the `git-clone` Step, once for each platform:

```yaml
workflows:
  codepush_update_deploy:
    steps:
    - git-clone@8: {}
    - codepush-publish@1:
        title: Publish iOS update
        inputs:
        - platform: ios
        - app_id: $IOS_CONNECTED_APP_ID
        - deployment: $CODEPUSH_DEPLOYMENT
        - app_version: $APP_TARGET_VERSIONS
        - api_token: $BITRISE_API_TOKEN
    - codepush-publish@1:
        title: Publish Android update
        inputs:
        - platform: android
        - app_id: $ANDROID_CONNECTED_APP_ID
        - deployment: $CODEPUSH_DEPLOYMENT
        - app_version: $APP_TARGET_VERSIONS
        - api_token: $BITRISE_API_TOKEN
```

The Step also has optional inputs such as `rollout_percentage`, `mandatory`, and `description`. It detects the entry file and the Hermes setting, and you can override them with `entry_file` and `hermes`. If an earlier Step already installs your dependencies, set `skip_dependency_install` to `"true"`.

To [sign your update](/release-management/codepush/code-signing-with-codepush), upload your private key as a Secret File and set `private_key_path` to its Environment Variable, for example `$CODEPUSH_PRIVATE_KEY`.

The Step exports the update package to `$BITRISE_DEPLOY_DIR`, so a later **Deploy to Bitrise.io** Step adds it to the build's artifacts.

### Using the CodePush CLI

Use the CLI if you need more than publishing an update, for example to promote a release to another deployment or to roll one back. Install the CodePush CLI plugin, install your dependencies, and run `push --bundle` once for each platform. The command bundles your JavaScript and uploads the update. The CLI reads `BITRISE_API_TOKEN` from the environment automatically.

```yaml
workflows:
  codepush_update_deploy:
    steps:
    - git-clone@8: {}
    - script@1:
        title: Install the CodePush CLI
        inputs:
        - content: bitrise plugin install https://github.com/bitrise-io/bitrise-plugins-codepush-cli.git
    - npm@1:
        inputs:
        - command: install
    - script@1:
        title: Push iOS update
        inputs:
        - content: |-
            bitrise :codepush push --bundle --platform ios \
              --app-id "$IOS_CONNECTED_APP_ID" \
              --deployment "$CODEPUSH_DEPLOYMENT" \
              --app-version "$APP_TARGET_VERSIONS"
    - script@1:
        title: Push Android update
        inputs:
        - content: |-
            bitrise :codepush push --bundle --platform android \
              --app-id "$ANDROID_CONNECTED_APP_ID" \
              --deployment "$CODEPUSH_DEPLOYMENT" \
              --app-version "$APP_TARGET_VERSIONS"
```

In a Bitrise build, the CLI attaches the build number and commit hash to the update. It also exports the update ID to the next Steps. For all commands and flags, see the [CodePush CLI reference](/release-management/codepush/codepush-cli/codepush-cli-reference).

## Automating the CodePush update release

After creating a Workflow to generate and upload your CodePush updates, you have two ways of running it:

- [Run the Workflow manually](/bitrise-ci/run-and-analyze-builds/starting-builds/starting-builds-manually).
- [Triggering a build automatically](/bitrise-ci/run-and-analyze-builds/build-triggers/configuring-build-triggers) when a defined condition is met. For the available conditions, see [Supported trigger conditions](/bitrise-ci/run-and-analyze-builds/build-triggers/configuring-build-triggers#supported-trigger-conditions).

You can set up several different types of triggers to automate the process. Our example configuration uses a pull request trigger with a label:

**Release a CodePush update when a pull request gets a label**

In this example, we're setting up a trigger where the `codepush_update_deploy` Workflow runs when any pull request receives the `release-update` label. Use it to release a pull request to a deployment, such as `Staging`, for testing before you merge.

1. [Create the trigger](/bitrise-ci/run-and-analyze-builds/build-triggers/configuring-build-triggers#creating-build-triggers): set up a pull request trigger with the `label` condition set to `release-update`. The `target_branch` condition defaults to `*`, so pull requests to any branch match. To limit releases to one branch, also set `target_branch`.

   :::tip[GUI config]

   You can set these up on the GUI of the Workflow Editor, too. Here we're showing you the YAML configuration.

   :::

   ```yaml
   format_version: "13"
   default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git
   project_type: react-native
   workflows:
     codepush_update_deploy:
       status_report_name: 'Executing <target_id> for <project_title>'
       description: |
         Uploads Update Bundle to Bitrise
       steps:
       [...]
       triggers:
         pull_request:
         - label: release-update
   ```
1. When you want to release a CodePush update, open a pull request.
1. After the pull request has been reviewed and approved, add the `release-update` label to it.

:::note

Bitrise runs the build when you add the label, before the pull request is merged. It releases the update from the code of the pull request.

:::
