DEV Community

loading...

Bitbucket pipelines composer cache dir empty 🤷‍♂️

Robert Tajnšek
・2 min read

TL;DR: composer saved cache in different directory then Bitbucket's predefined composer cache location. Adding COMPOSER_HOME=~/.composer before the composer install fixed the issue.

In our company we are using Bitbucket as our Git hosting service. We also use they're Pipelines feature for CI/CD automation. All has been running fine and dandy but after a while I found a small issue.

The last stage of Pipelines is a Build teardown process, which also saves cache between builds providing faster builds, and reducing the number of consumed build minutes. Our bitbucket-pipelines.yml file lookes something like this:

image: funky-org/bitbucket-pipelines-php

pipelines:
  default:
    - step:
        name: Run unit tests
        caches:
          - composer
        script:
          - composer install --no-progress
          - ./vendor/bin/phpunit
          - ls -la ~/.composer/cache
Enter fullscreen mode Exit fullscreen mode

We are using composer as our PHP dependencies manager and the image is based on php:7.4-cli.

When the pipeline executed the composer install the cache should be saved in the ~/.composer/cache directory (based on the documentation). Bitbucket's predefined composer cache location is also ~/.composer/cache so the Build teardown should save the cache in Bitbucket for later use. But that directory was empty 🧐.

I reproduced this issue locally on my computer by running the following command and running the scripts manually:

docker run --rm -it -v ${PWD}:/app funky-org/bitbucket-pipelines-php bash
Enter fullscreen mode Exit fullscreen mode

I found out that the cache got saved into ~/.cache/composer. Thus the reason why the Pipelines didn't find any cache files to save.

The solution I did was to manually specifity the COMPOSER_HOME environment variable so that composer saved cache in the Bitbucket's predefined composer cache location. The final yml file looks like this

image: funky-org/bitbucket-pipelines-php

pipelines:
  default:
    - step:
        name: Run unit tests
        caches:
          - composer
        script:
          - COMPOSER_HOME=~/.composer composer install --no-progress
          - ./vendor/bin/phpunit
          - ls -la ~/.composer/cache
Enter fullscreen mode Exit fullscreen mode

I hope this article helps someone not to lose so much time as I did 😆

Discussion (0)