How to cache a tool which you install during the build

The following example shows how Bitrise Key-Value Caching can be used to cache a tool which you install during the build.

Why would you want to cache a tool you install during the build?

  1. To reduce potential network issues, for example when the download source (in this example github.com’s artifact storage) isn’t available.
  2. To speed up the installation, as the Bitrise Key-Value Cache is closer to the Bitrise build machines.

Example configuration where we install bazelisk (a single binary tool) from github.com releases, store it in the Bitrise Key-Value Cache, and in the subsequent builds we download it from the cache:

workflows:
  kv-cache-binary-cache-example:
    steps:
    - set-env-var@0:
        inputs:
        - value: https://github.com/bazelbuild/bazelisk/releases/download/v1.6.1/bazelisk-linux-amd64
        - destination_keys: BAZELISK_DOWNLOAD_URL
        title: Set Tool Download URL as Environment Variable
    - restore-cache@1:
        inputs:
        - key: bazelisk-{{ getenv "BAZELISK_DOWNLOAD_URL" }}
    - script@1:
        title: Install tool (if not cached)
        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

            if [ -e /usr/local/bin/bazelisk ] ; then
              echo 'Bazelisk already installed'
            else
              # Not yet installed - install it
              curl -Lo /usr/local/bin/bazelisk "$BAZELISK_DOWNLOAD_URL"
            fi

            chmod +x /usr/local/bin/bazelisk
            ln -sf /usr/local/bin/bazelisk /usr/local/bin/bazel
            bazel --version
    - save-cache@1:
        inputs:
        - key: bazelisk-{{ getenv "BAZELISK_DOWNLOAD_URL" }}
        - paths: "/usr/local/bin/bazelisk"

Example build: Bitrise - Mobile Continuous Integration and Delivery - iOS & Android Build Automation

Reference docs:

2 Likes