DEV Community

Robert Tajnšek
Robert Tajnšek

Posted on

1

Bitbucket pipelines composer cache dir empty 🤷‍♂️

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 😆

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay