メインコンテンツまでスキップ

Bitrise CI での CodePush アップデート

CodePush の更新は、Bitrise CIのWorkflowを使用してリリースできます。この処理には次のことが必要です。

  • CodePush の認証情報を設定する: デプロイ ID とBitrise CodePush のデプロイキーを保持するSecretsとEnvironment Variablesを作成します。
  • iOS とAndroidの両方の更新パッケージを作成し、両方をBitrise CodePush Server にアップロードするWorkflowを作成する。

Bitriseでの CodePush 認証情報の設定Click to copy link​

  1. 認証情報を取得する: Release Managementアプリの ID と、CodePush のデプロイ ID およびデプロイキーが必要です。

  2. デプロイ ID と接続済みアプリの ID 用にEnvironment Variablesを作成します:

    • IOS_PROD_DEPLOYMENT_ID: iOS アプリのデプロイ ID。
    • ANDROID_PROD_DEPLOYMENT_ID: Androidアプリのデプロイ ID。
    • IOS_CONNECTED_APP_ID: iOS アプリのアプリ ID。
    • ANDROID_CONNECTED_APP_ID: Androidアプリのアプリ ID。
  3. デプロイキーとAPIトークン用にSecretsを作成します:

    • IOS_PROD_DEPLOYMENT_KEY: Bitrise CodePush の iOS デプロイのデプロイキー。
    • ANDROID_PROD_DEPLOYMENT_KEY: Bitrise CodePush のAndroidデプロイのデプロイキー。
    • BITRISE_API_TOKEN: 個人アクセストークンまたはworkspaceAPIトークン。

CodePush の更新用にWorkflowを作成するClick to copy link​

このセクションでは、次のことを行うCI Workflowの作成方法を説明します。

  • iOS 用の更新パッケージを作成する。
  • iOS の更新パッケージをBitrise CodePush Server にアップロードする。
  • Android用の更新パッケージを作成する。
  • Androidの更新パッケージをBitrise CodePush Server にアップロードする。
  1. Workflowを作成し、git-clone Stepの後に、installコマンドを使ってnpm Stepを追加します:

    ---
    format_version: '13'
    default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git
    project_type: ios
    workflows:
    codepush_update_to_server:
    description: The workflow will generate and push your update to CodePush Server.
    steps:
    - git-clone@8: {}
    - npm@1:
    inputs:
    - command: install
  2. JSONパーサーを使ってアプリのバージョンを抽出し、envmanを使って環境変数にバージョンを設定するscript Stepを追加します:

    - script@1:
    title: Extract App Version
    inputs:
    - content: |-
    #!/bin/bash
    set -e

    # Extract version from app.json using jq (JSON parser)
    VERSION=$(jq -r '.version' package.json)

    # Verify version extraction
    if [ -z "$VERSION" ]; then
    echo "Error: Could not extract version from package.json"
    exit 1
    fi

    echo "Extracted version from package.json: $VERSION"

    # Set the environment variable using envman
    envman add --key APP_VERSION --value "$VERSION"

    echo "Successfully set APP_VERSION=$VERSION"
  3. GitHubからrelease-management-recipesリポジトリをクローンするscript Stepをもう1つ追加します。このリポジトリには、後で使用するアップロードスクリプトが含まれています。

    - script@1:
    title: Get Release Management Recipes
    inputs:
    - content: >-
    #!/usr/bin/env bash
    # fail if any commands fails
    set -e
    # make pipelines' return status equal the last command to exit with a
    non-zero status, or zero if all commands exit successfully
    set -o pipefail
    # debug log
    set -x

    # write your script here
    git clone https://github.com/bitrise-io/release-management-recipes
  4. iOS の更新バンドルを生成し、そこから zip アーカイブを作成します:

    - script@1:
    title: Generate iOS Update Bundle
    inputs:
    - content: >-
    #!/usr/bin/env bash
    # fail if any commands fails
    set -e
    # make pipelines' return status equal the last command to exit with a
    non-zero status, or zero if all commands exit successfully
    set -o pipefail
    # debug log
    set -x

    npx react-native bundle \
    --platform ios \
    --dev false \
    --entry-file index.js \
    --bundle-output ./build/main.jsbundle \
    --assets-dest ./build

    # Create zip archive
    zip -r update.zip ./build
  5. upload_code_push_package.shを使って、iOS の更新バンドルを CodePush Server にアップロードします:

    - script@1:
    title: Upload iOS Update Bundle to Codepush Server
    inputs:
    - content: >
    #!/usr/bin/env bash
    # fail if any commands fails
    set -e
    # make pipelines' return status equal the last command to exit with a non-zero status, or zero if all
    commands exit successfully
    set -o pipefail
    # debug log
    set -x

    cd release-management-recipes

    UPLOAD_RESPONSE=$(PACKAGE_PATH=../update.zip \
    AUTHORIZATION_TOKEN=$BITRISE_API_TOKEN \
    CONNECTED_APP_ID=$IOS_CONNECTED_APP_ID \
    DEPLOYMENT_ID=$IOS_PROD_DEPLOYMENT_ID \
    APP_VERSION=$APP_VERSION /bin/bash ./api/upload_code_push_package.sh 2>&1)
    EXIT_CODE=$?

    if [ $EXIT_CODE -ne 0 ]; then
    \ echo \"❌ upload_code_push_package.sh failed with exit code
    $EXIT_CODE\"
    \ echo \"$UPLOAD_RESPONSE\"
    \ exit $EXIT_CODE
    fi

    # Take only the last line of the response (final/latest status JSON
    object) FINAL_RESPONSE=$(echo "$UPLOAD_RESPONSE" | tail -n1)

    # Check explicitly for ERR_INTERNAL or other internal error indicators
    if echo \"$UPLOAD_RESPONSE\" | grep -q \"ERR_INTERNAL\"; then
    \ ERROR_MESSAGE=$(echo \"$FINAL_RESPONSE\" | jq -r '.message' ||
    echo \"Unknown error\")
    \ echo \"❌ Server returned internal error: $ERROR_MESSAGE\"
    \ exit 1
    fi


    # Now safely parse 'status' and 'status_reason' from the final
    response line PACKAGE_STATUS=$(echo "$FINAL_RESPONSE" | jq -r
    '.status' || echo "null") STATUS_REASON=$(echo "$FINAL_RESPONSE" | jq
    -r '.status_reason' || echo "")

    if [ "$PACKAGE_STATUS" = "processed_valid" ]; then echo "✅ Package
    uploaded and processed successfully." rm -rf ../build.zip rm -rf
    ../build else echo "⚠️ Package upload unexpected status:
    $PACKAGE_STATUS - Reason: $STATUS_REASON" exit 1 fi cd ..
  6. Androidの更新バンドルを生成します:

    - script@1:
    title: Generate Android Update Bundle
    inputs:
    - content: |
    #!/usr/bin/env bash
    # fail if any commands fails
    set -e
    # make pipelines' return status equal the last command to exit with a non-zero status, or zero if all commands exit successfully
    set -o pipefail
    # debug log
    set -x

    npx react-native bundle \
    --platform android \
    --dev false \
    --entry-file index.js \
    --bundle-output ./build/index.android.bundle \
    --assets-dest ./build

    # Create zip archive
    zip -r update.zip ./build
  7. Androidの更新バンドルを CodePush Server にアップロードします:

    - script@1:
    title: Upload Android Update Bundle to Codepush Server
    inputs:
    - content: |
    #!/usr/bin/env bash
    # fail if any commands fails
    set -e
    # make pipelines' return status equal the last command to exit with a non-zero status, or zero if all commands exit successfully
    set -o pipefail
    # debug log
    set -x

    cd release-management-recipes

    UPLOAD_RESPONSE=$(PACKAGE_PATH=../update.zip \
    AUTHORIZATION_TOKEN=$BITRISE_API_TOKEN \
    CONNECTED_APP_ID=$ANDROID_CONNECTED_APP_ID \
    DEPLOYMENT_ID=$ANDROID_PROD_DEPLOYMENT_ID \
    APP_VERSION=$APP_VERSION /bin/bash ./api/upload_code_push_package.sh 2>&1)
    EXIT_CODE=$?

    if [ $EXIT_CODE -ne 0 ]; then
    echo "❌ upload_code_push_package.sh failed with exit code $EXIT_CODE"
    echo "$UPLOAD_RESPONSE"
    exit $EXIT_CODE
    fi

    # Take only the last line of the response (final/latest status JSON object)
    FINAL_RESPONSE=$(echo "$UPLOAD_RESPONSE" | tail -n1)

    # Check explicitly for ERR_INTERNAL or other internal error indicators
    if echo "$UPLOAD_RESPONSE" | grep -q "ERR_INTERNAL"; then
    ERROR_MESSAGE=$(echo "$FINAL_RESPONSE" | jq -r '.message' || echo "Unknown error")
    echo "❌ Server returned internal error: $ERROR_MESSAGE"
    exit 1
    fi

    # Now safely parse 'status' and 'status_reason' from the final response line
    PACKAGE_STATUS=$(echo "$FINAL_RESPONSE" | jq -r '.status' || echo "null")
    STATUS_REASON=$(echo "$FINAL_RESPONSE" | jq -r '.status_reason' || echo "")

    if [ "$PACKAGE_STATUS" = "processed_valid" ]; then
    echo "✅ Package uploaded and processed successfully."
    else
    echo "⚠️ Package upload unexpected status: $PACKAGE_STATUS - Reason: $STATUS_REASON"
    exit 1
    fi

    cd ..

CodePush の更新リリースを自動化するClick to copy link​

CodePush の更新を生成してアップロードするWorkflowを作成した後は、次の2つの方法で実行できます。

この処理を自動化するために、さまざまな種類のトリガーを設定できます。この例の設定では、ラベル付きのプルリクエストトリガーを使用しています。

特定のブランチへのプルリクエストが作成されたときに CodePush の更新をリリースする

この例では、次のようなトリガーを設定します:

  • Bitriseは、updatesをターゲットブランチとして作成されたプルリクエストを検出します。
  • ブランチへのプルリクエストがPRに対してrelease-updateラベルを受け取ると、codepush_update_deploy Workflowがトリガーされます。
  1. トリガーを作成する: target_branchとlabelという2つの条件でプルリクエストトリガーを設定します:

    • target_branchをupdatesに設定します。
    • labelをrelease-updateに設定します。
    GUI 設定

    これらはWorkflow Editorの GUI からも設定できます。ここでは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 CodePush Server
    steps:
    [...]
    triggers:
    pull_request:
    - target_branch: updates
    label: release-update
  2. CodePush の更新をリリースする場合は、updatesブランチへのプルリクエストを作成します。

  3. PRのレビューと承認が完了したら、release-updateラベルを追加します。