Flutter Bitbucket Pipeline cache
How to speed up the flutter Bitbucket build pipelines by caching libraries and build files.
It was pretty tricky to speed up the pipeline since there are not so many resources on how to cache your flutter app on Bitbucket Pipeline.
Read how to setup bitbucket pipelines for flutter if you just want to get started.
How Dart/Flutter saves dependencies
Whenever you run
flutter pub get, the system downloads all dependencies to the folder
.pub-cache in your home directory.
The Folder contains all cached dependencies and will look similar to:
Caching those dependencies makes sense since a plugin with a specific version will never change. It's basically not possible to upload the same version with a different code to pub.dev.
Dependency version changes
Whenever you change your dependencies, the new versions will appear in the same Folder.
Alright. Let's cache the dependencies with bitbucket pipelines
bitbucket-pipelines.yml config definitions
We want all dependencies to be cached in the .pub-cache folder on bitbucket.
For this we define certain definitions that can be referenced in future steps.
- step: &Flutter-pub-get
name: Flutter Pub Get
- flutter pub get
- step: &Lint-and-test
name: Flutter lint and test
- flutter analyze
- flutter test ./test
We define the steps Flutter-pub-get and Lint-and-test plus the cache directory $HOME/.pub-cache
Now we just need to use the steps in any pipeline runner.
- step: *Flutter-pub-get
- step: *Lint-and-test
name: Build Flutter Android appbundle
- flutter build appbundle
and Bitbucket also notes that the cache was downloaded
Cache "dartpubcache": Downloading
Cache "dartpubcache": Downloaded 40.6 MiB in 2 seconds
Cache "dartpubcache": Extracting
Cache "dartpubcache": Extracted in 1 seconds
The bitbucket cache will refreshed after a week. You can also delete the cache manually inside the bitbucket pipeline section. Click on the top right Cache button.
Flutter dependency update
It also does not cause any problems if you change your dependencies before the cache has expired. The only thing that happens is that the command flutter pub get takes a little longer because additional dependencies are downloaded.