Skip to main content

Bazel のリモート ビルド キャッシュ

Bazel のリモート キャッシュを正常に使用するには、 bitrise.bazelrc このファイルには、Bazel プロジェクトのリモート ビルド キャッシュを有効にするために必要な構成が含まれています。リモート キャッシュは、ローカル マシン上で実行されるビルド、または CI 環境で実行されるビルドに使用できます。

ローカル ビルドでの Bazel のリモート キャッシュの構成

Bazel の Bitrise ビルド キャッシュはどのマシンでも使用できます。必要なのは、 bitrise.bazelrc キャッシュ エンドポイントに必要な構成を含む構成ファイル。

  1. Bitrise でパーソナル アクセス トークンを生成します。 パーソナルアクセストークンの作成

    プロセス中に必要になるため、トークンの値をコピーします。

  2. あなたの ワークスペース ID。これを行うには、ワークスペースのページに移動し、URL で ID を見つけます。

    workspace-id.png
  3. というファイルを作成します .bitrise.bazelrc.tpl 次の内容で:

    注記

    交換する <workspace-slug> 前の手順で取得したワークスペース ID に置き換えます。

    build --remote_cache=grpcs://pluggable.services.bitrise.io
    build --remote_header=x-org-id=<workspace-slug>
    build --remote_header=authorization="Bearer $BITRISE_PERSONAL_ACCESS_TOKEN"
  4. 次の行を .bazelrc 設定ファイル:

    try-import %workspace%/.bitrise.bazelrc

    この構成により、 .bitrise.bazelrc 存在する場合はロードされますが、存在しない場合でも問題は発生しません。の bazel コマンドは実行されますが、次の場合はビルド キャッシュなしで実行されます。 .bitrise.bazelrc 存在しません。

  5. を作成します。 bitrise.bazelrc からのファイル bitrise.bazelrc.tpl テンプレートファイル:

    export BITRISE_PERSONAL_ACCESS_TOKEN=your-personal-access-token
    envsubst < .bitrise.bazelrc.tpl > .bitrise.bazelrc
  6. を追加します。 bitrise.bazelrc ファイルに .gitignore

    これはセキュリティ対策です。ファイルには個人用アクセス トークンが含まれているため、一般公開されるべきではありません。

それでおしまい!どれでも実行できるようになりました bazel コマンドを使用して、Bitrise ビルド キャッシュを利用します。

Bitrise CI 環境での Bazel のリモート キャッシュの構成

Bazel のリモート キャッシュの構成には、以下が含まれます。

  • の作成 bitrise.ci.bazelrc ファイル。

  • Bitrise ビルド キャッシュ エンドポイントの構成。

  1. 追加 脚本 ワークフローに進みます。

  2. の中に コンテンツ 入力に以下を追加します。

    #!/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
    
    # === CONFIG ===
    export BAZELRC_PATH='./.bazelrc'
    export BITRISE_BAZELRC_TEMPLATE_PATH='./.bitrise.ci.bazelrc.tpl'
    # ==============
    
    # Install needed tools for Bazel (only needed on Linux)
    # Note: this isn't needed for Bitrise Build Cache specifically, if you install bazelisk or bazel in a different way yourself feel free to remove this section
    if [ $(uname) == "Linux" ]; then
      apt-get update
      apt-get install -y gettext-base
    
      wget https://github.com/bazelbuild/bazelisk/releases/download/v1.17.0/bazelisk-linux-amd64
      chmod +x bazelisk-linux-amd64
      mv bazelisk-linux-amd64 /usr/local/bin/bazel
    fi
    
    # Create .bitrise.ci.bazelrc.tpl file
    cat <<EOF > $BITRISE_BAZELRC_TEMPLATE_PATH
    build --remote_cache=\$BITRISE_CACHE_ENDPOINT
    build --remote_header=authorization="Bearer \$BITRISEIO_BITRISE_SERVICES_ACCESS_TOKEN"
    build --remote_header=x-app-id=\$BITRISE_APP_SLUG
    build --remote_header=x-flare-buildtool=bazel
    build --remote_header=x-flare-builduser=bitrise
    build --remote_header=x-flare-build-id=\$BITRISE_BUILD_SLUG
    build --remote_header=x-flare-step-id=\$BITRISE_STEP_EXECUTION_ID
    EOF
    
    # Check if .bazelrc already has the necessary try-import
    # If not, then add it
    if ! grep -q 'try-import %workspace%/.bitrise.bazelrc' "${BAZELRC_PATH}"; then
      # Add a new-line to .bazelrc file just to be sure we're not appending to the last line
      echo '' >> "${BAZELRC_PATH}"
      # Add the .bitrise.bazelrc import in a non breaking way
      echo 'try-import %workspace%/.bitrise.bazelrc' >> "${BAZELRC_PATH}"
    fi
    
    # Setup cache endpoint
    case "${BITRISE_DEN_VM_DATACENTER}" in
    LAS1)
    export BITRISE_CACHE_ENDPOINT=grpc://las-cache.services.bitrise.io:6666
    ;;
    ATL1)
    export BITRISE_CACHE_ENDPOINT=grpc://atl-cache.services.bitrise.io:6666
    ;;
    *)
    export BITRISE_CACHE_ENDPOINT=grpcs://pluggable.services.bitrise.io
    ;;
    esac
    
    envsubst < "${BITRISE_BAZELRC_TEMPLATE_PATH}" > .bitrise.bazelrc
    echo "selected cache endpoint: ${BITRISE_CACHE_ENDPOINT}"
    

    このスクリプトは .bitrise.bazelrc ファイルを作成し、指定された内容を変更します .bazelrc ファイルを使用してインポートを試行します (try-import がすでに .bazelrc)。の .bitrise.bazelrc このファイルには、プロジェクトで Bazel の Bitrise ビルド キャッシュを有効にするために必要なすべての構成が含まれており、遅延を排除するために、ビルドが実行されている Bitrise データ センターに応じて最も近いキャッシュ サーバーが選択されます。