Workspace cleaning issue before a build / how to cache the source code directory

We have noticed that before every build step, our git clone is a success. Does that mean that before every build, workspace gets cleaned up? If that is the case, is there any way we can avoid it?

Correct, in fact the whole virtual machine / build environment is destroyed at the end of the build, to ensure code and file (generated artifacts) security (see: Code security - Bitrise Docs ). By default we don’t store your source code and we don’t access it either outside of the build environment (and even there only if you have the Git Clone step in your workflow).

In general you should not avoid this, a full clean git clone is the best way to ensure that the state of the code is what you expect, and that it works on any Mac/PC after a new / clean git clone.

Warning: the cache will include every generated (during the build) file too, even those which are in your .gitignore! This can lead to false positive and false negative build results! It’s not advised to cache the source dir, unless you really have to do that!

Depending on your project / repository, the Caching of the source might not even speed up things. For example if there’s any file change in the source directory during the build, that will mean that the cache will be updated at the end of every build, which might take as much or more time (to download and uncompress the cache, and in case there’s a change compress and upload again) than simply cloning the repository.

If you really want to do this, you can just:

  1. Add the Cache:Pull step before the Git Clone step
  2. Add the Cache:Push step after the Git Clone step (can be the very last step in the workflow, depending on what things you want to cache other than the source code)
  3. For the Cache:Push step’s Cache paths input/option specify:
    $BITRISE_SOURCE_DIR
    
  4. Make sure that the Git Clone step’s Reset repository option is set to No (default, unless you changed it)

An example build configuration with source code caching:

---
format_version: 1.3.1
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git
trigger_map:
- push_branch: "*"
  workflow: primary
- pull_request_source_branch: "*"
  workflow: primary
workflows:
  primary:
    steps:
    - activate-ssh-key@3.1.1:
        run_if: '{{getenv "SSH_RSA_PRIVATE_KEY" | ne ""}}'
    - cache-pull@0.9.2: {}
    - git-clone@3.4.1: {}
    - script@1.1.3:
        title: Do anything with Script step
    - install-missing-android-tools@0.9.2: {}
    - gradle-runner@1.5.4:
        inputs:
        - gradle_task: "$GRADLE_TASK"
    - deploy-to-bitrise-io@1.2.7: {}
    - cache-push@0.9.4:
        inputs:
        - cache_paths: "$BITRISE_SOURCE_DIR"
app:
  envs:
  - opts:
      is_expand: false
    GRADLE_BUILD_FILE_PATH: build.gradle
  - opts:
      is_expand: false
    GRADLE_TASK: assemble
  - opts:
      is_expand: false
    GRADLEW_PATH: "./gradlew"

You can find more info about the Build Cache in the docs at Redirecting… - Bitrise Docs

If you have any questions just let us know!