Skip to main content

永続的なビルド環境のクリーンアップ

概要

Bitrise CLI をエージェント モードで実行するように設定できます。これにより、セルフホスト エージェントでビルドを実行するときに永続的なビルド環境をクリーンアップできます。

Bitrise では、ビルドが開始されるたびに新しい仮想マシンが作成され、ビルドが完了すると仮想マシンは破棄されます。ただし、Bitrise ビルドを永続的なビルド環境で実行することはできます。このような環境では、あるビルドの製品が後続のビルドに影響を与える可能性があります。

セルフホスト型エージェントでは、1 つのエージェントが複数のビルドを (同時にまたは次々に) 実行します。これにより、ローカル ファイル システム上のビルド間でデータを共有できますが、あるビルドが別のビルドに影響を与えないように注意する必要もあります。

この問題を回避するには、Bitrise CLI を次の環境で実行するように設定できます。 エージェントモード。エージェントモードでは、 agent-config.yml ホストマシンの ~/.bitrise/ ディレクトリ。このファイルでは、新しいビルドの開始時またはビルドの終了時にクリーンアップするディレクトリを指定できます。単純なクリーンアップよりも高度な使用例がある場合は、独自のカスタム スクリプトを実行することもできます。

エージェントモードの設定

  1. を追加します。 agent-config.yml ファイルをあなたの ~/.bitrise/ ディレクトリ。

  2. クリーンアップ操作を実行するフォルダーを正確に構成したい場合は、 bitrise_dirs 財産。このプロパティはデフォルト設定をオーバーライドします。たとえば、 BITRISE_DEPLOY_DIR 環境変数をデフォルトよりも大きくします。

  3. bitrise_dirs、ビルドの開始時または終了時に何らかのアクションを実行するディレクトリを定義します。 KEY: path フォーマット。たとえば、すべてのソース コード チェックアウト、デプロイ可能な成果物、およびデプロイ可能なテスト結果成果物に対して個別のディレクトリを定義できます。

    # Customize the common Bitrise directories
    bitrise_dirs:
      # Root directory for all Bitrise data produced at runtime
      BITRISE_DATA_HOME_DIR: /opt/bitrise
      
      # Directory for source code checkout.
      BITRISE_SOURCE_DIR: /opt/bitrise/workspace/$BITRISE_APP_SLUG
      
      # Directory for deployable artifacts.
      BITRISE_DEPLOY_DIR: /opt/bitrise/$BITRISE_APP_SLUG/$BITRISE_BUILD_SLUG/artifacts
      
      # Directory for deployable test result artifacts.
      BITRISE_TEST_DEPLOY_DIR: /opt/bitrise/$BITRISE_APP_SLUG/$BITRISE_BUILD_SLUG/test_results
    
    

    Multiple projects of the same Workspace

    Don’t forget that multiple projects of the same Workspace could run on the same agent. Make sure to always include $BITRISE_APP_SLUG in the directory hierarchy to separate projects and their source code checkouts.

    # wrong:
    BITRISE_SOURCE_DIR: /opt/bitrise/workspace
    # correct:
    BITRISE_SOURCE_DIR: /opt/bitrise/$BITRISE_APP_SLUG/workspace
  4. 追加 hooks の財産 agent-config.yml ファイル。このプロパティは、ビルドの開始時または終了時に実行するアクションを定義します。これには 4 つの異なるパラメータがあります。

    • cleanup_on_build_start: 新しいビルドの開始時にクリーンアップされるディレクトリを定義します。

    • cleanup_on_build_end: ビルドが完了するたびにクリーンアップされるディレクトリを定義します。ビルドに失敗した場合の実行は保証されません。

    • do_on_build_start: 新しいビルドの開始時に実行されるカスタム スクリプトを定義します。

    • do_on_build_end: ビルドの完了時に実行されるカスタム スクリプトを定義します。ビルドが失敗した場合の実行は保証されません。

    Nested Workflows

    A Script Step in a Workflow can execute bitrise run nested_workflow and trigger a nested workflow. This nested Workflow inherits all the envs and parameters of the parent Workflow, and the parent Workflow waits for the completion of the nested Workflow. When the nested Workflow is launched this way, hooks and directory cleanups are not executed in this process to avoid unexpected behavior.

  5. To test the agent mode, start a build. The log should show the following message at the top:

    Running in agent mode
    Config file: .bitrise/agent-config.yml

一般的な構成例

例1 完全に分離されたビルド

状態を最小限に抑え、信頼性を最大限に高めるために、各ビルドには一意のディレクトリが割り当てられます。ソース コードはビルドごとに最初からチェックアウトされます。

bitrise_dirs:
  BITRISE_DATA_HOME_DIR: /opt/bitrise
  BITRISE_SOURCE_DIR: /opt/bitrise/$BITRISE_APP_SLUG/$BITRISE_BUILD_SLUG/workspace
  BITRISE_DEPLOY_DIR: /opt/bitrise/$BITRISE_APP_SLUG/$BITRISE_BUILD_SLUG/artifacts
  BITRISE_TEST_DEPLOY_DIR: /opt/bitrise/$BITRISE_APP_SLUG/$BITRISE_BUILD_SLUG/test_results

hooks:
  # Since dirs are unique to each build, there is nothing to clean up here:
  cleanup_on_build_start: []
  
  # Clean up everything after the build ends:
  cleanup_on_build_end:
    - $BITRISE_SOURCE_DIR
    - $BITRISE_DEPLOY_DIR
    - $BITRISE_TEST_DEPLOY_DIR

例2 ウォーム チェックアウト用の共有ソース コード ディレクトリ

ソース コードのチェックアウトを高速化するために、以前のビルドのソース コード ディレクトリを再利用できます。

bitrise_dirs:
  BITRISE_DATA_HOME_DIR: /opt/bitrise
  # Use a warm clone of the repo for all builds:
  BITRISE_SOURCE_DIR: /opt/bitrise/$BITRISE_APP_SLUG/workspace
  # For artifacts and test results, it's still a good idea to place them into unique dirs
  BITRISE_DEPLOY_DIR: /opt/bitrise/$BITRISE_APP_SLUG/$BITRISE_BUILD_SLUG/artifacts
  BITRISE_TEST_DEPLOY_DIR: /opt/bitrise/$BITRISE_APP_SLUG/$BITRISE_BUILD_SLUG/test_results

hooks:
  # Since dirs are unique to each build, there is nothing to clean up here:
  cleanup_on_build_start: []
  
  # Optional: clean up artifacts and test results.
  # Note that $BITRISE_SOURCE_DIR is NOT clean up here!
  cleanup_on_build_end:
    - $BITRISE_DEPLOY_DIR
    - $BITRISE_TEST_DEPLOY_DIR