Skip to content

Instantly share code, notes, and snippets.

@osipxd
Last active March 25, 2024 15:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osipxd/2ed1ee0878acddc399da2152b69ea7a2 to your computer and use it in GitHub Desktop.
Save osipxd/2ed1ee0878acddc399da2152b69ea7a2 to your computer and use it in GitHub Desktop.
Script preparing Android project cache for GitLab CI
#!/usr/bin/env bash
set -eu
BUILD_CACHE_THRESHOLD=5000
function get_gradle_slug() {
local dist
dist=$(source gradle/wrapper/gradle-wrapper.properties && echo "${distributionUrl?:}")
dist=${dist##*gradle-}
echo "${dist%.zip}"
}
# Delete outdated Gradle wrappers
shopt -s extglob
gradle_slug=$(get_gradle_slug)
rm -rf .gradle/wrapper/dists/gradle-!("$gradle_slug")
# Delete dependencies lock file and plugin resolution
rm -f .gradle/caches/modules-2/modules-2.lock
rm -rf .gradle/caches/*/plugin-resolution/
# Drop build cache if it contains too many files
build_cache_count=$(find .gradle/caches/build-cache-1/ -type f | wc -l)
if [[ $build_cache_count -gt $BUILD_CACHE_THRESHOLD ]]; then
echo "Build cache contains $build_cache_count files. Cleaning..."
rm -rf .gradle/caches/build-cache-1/
fi
@osipxd
Copy link
Author

osipxd commented Sep 11, 2020

Run it after your script in .gitlab-ci.yml

# ...

after_script:
  - ./scripts/prepare-cache.sh
cache:
  key: ${CI_PROJECT_ID}
  paths:
    - .gradle/wrapper
    - .gradle/caches/modules-2
    - .gradle/caches/build-cache-1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment