Skip to content

Configuration Tips and Tricks

Custom Clone Command

By default Cirrus CI uses a Git client implemented purely in Go to perform a clone of a single branch with full Git history. It is possible to control clone depth via CIRRUS_CLONE_DEPTH environment variable.

Customizing clone behavior is a simple as overriding clone_script. For example, here an override to use a pre-installed Git client (if your build environment has it) to do a shallow clone of a single branch:

  clone_script: |
    if [ -z "$CIRRUS_PR" ]; then
      git clone --recursive --branch=$CIRRUS_BRANCH https://x-access-token:${CIRRUS_REPO_CLONE_TOKEN}${CIRRUS_REPO_FULL_NAME}.git $CIRRUS_WORKING_DIR
      git reset --hard $CIRRUS_CHANGE_IN_REPO
      git clone --recursive https://x-access-token:${CIRRUS_REPO_CLONE_TOKEN}${CIRRUS_REPO_FULL_NAME}.git $CIRRUS_WORKING_DIR
      git fetch origin pull/$CIRRUS_PR/head:pull/$CIRRUS_PR
      git reset --hard $CIRRUS_CHANGE_IN_REPO
  # ...

go-git benefits

Using go-git made it possible to not require a pre-installed Git from an execution environment. For example, most of alpine-based containers doesn't have Git pre-installed. Because of go-git you can even use distroless containers with Cirrus CI which don't even have Operation System.

Sharing configuration between tasks

You can use YAML aliases to share configuration options between multiple tasks. For example, here is a 2-task build which only runs for "master", PRs and tags, and installs some framework:

# Define a node anywhere in YAML file to create an alias. Make sure the name doesn't clash with an existing keyword.
regular_task_template: &REGULAR_TASK_TEMPLATE
  only_if: $CIRRUS_BRANCH == 'master' || $CIRRUS_TAG != '' || $CIRRUS_PR != ''
    FRAMEWORK_PATH: "${HOME}/framework"
  install_framework_script: curl | tar -C "${FRAMEWORK_PATH}" -x

  # This operator will insert REGULAR_TASK_TEMPLATE at this point in the task node.
  name: linux
    image: alpine:latest
  test_script: ls "${FRAMEWORK_PATH}"

  name: osx
    image: mojave-xcode-10.2
  test_script: ls -w "${FRAMEWORK_PATH}"

Long lines in configuration file

If you like your YAML file to fit on your screen, and some commands are just too long, you can split them across multiple lines. YAML supports a variety of options to do that, for example here's how you can split ENCRYPTED values: