<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Robert Tajnšek</title>
    <description>The latest articles on DEV Community by Robert Tajnšek (@ronzyfonzy).</description>
    <link>https://dev.to/ronzyfonzy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F119093%2F7b99a2f2-b8bf-4ab1-9f19-b8adf084978f.jpeg</url>
      <title>DEV Community: Robert Tajnšek</title>
      <link>https://dev.to/ronzyfonzy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ronzyfonzy"/>
    <language>en</language>
    <item>
      <title>Bitbucket pipelines composer cache dir empty 🤷‍♂️</title>
      <dc:creator>Robert Tajnšek</dc:creator>
      <pubDate>Fri, 12 Feb 2021 09:18:44 +0000</pubDate>
      <link>https://dev.to/ronzyfonzy/bitbucket-pipelines-composer-cache-dir-empty-413h</link>
      <guid>https://dev.to/ronzyfonzy/bitbucket-pipelines-composer-cache-dir-empty-413h</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;TL;DR: composer saved cache in different directory then Bitbucket's predefined &lt;code&gt;composer&lt;/code&gt; cache location. Adding &lt;code&gt;COMPOSER_HOME=~/.composer&lt;/code&gt; before the &lt;code&gt;composer install&lt;/code&gt; fixed the issue.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;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. &lt;/p&gt;

&lt;p&gt;The last stage of Pipelines is a &lt;em&gt;Build teardown&lt;/em&gt; process, which also &lt;a href="https://support.atlassian.com/bitbucket-cloud/docs/cache-dependencies"&gt;saves cache&lt;/a&gt; between builds providing faster builds, and reducing the number of consumed build minutes. Our &lt;code&gt;bitbucket-pipelines.yml&lt;/code&gt; file lookes something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;funky-org/bitbucket-pipelines-php&lt;/span&gt;

&lt;span class="na"&gt;pipelines&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;default&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;step&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Run unit tests&lt;/span&gt;
        &lt;span class="na"&gt;caches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;composer&lt;/span&gt;
        &lt;span class="na"&gt;script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;composer install --no-progress&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./vendor/bin/phpunit&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;ls -la ~/.composer/cache&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are using composer as our PHP dependencies manager and the image is based on &lt;a href="https://hub.docker.com/layers/php/library/php/7.4-cli/images/sha256-e6c912ad45f84aaf5598c2e3cb426ab011a5568c20b249e3f1c02ea2a148a8a2?context=explore"&gt;php:7.4-cli&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;When the pipeline executed the &lt;code&gt;composer install&lt;/code&gt; the cache should be saved in the &lt;code&gt;~/.composer/cache&lt;/code&gt; directory (based on the &lt;a href="https://getcomposer.org/doc/03-cli.md#composer-cache-dir"&gt;documentation&lt;/a&gt;). Bitbucket's predefined &lt;code&gt;composer&lt;/code&gt; cache location is also &lt;code&gt;~/.composer/cache&lt;/code&gt; so the &lt;em&gt;Build teardown&lt;/em&gt; should save the cache in Bitbucket for later use. But that directory was empty 🧐.&lt;/p&gt;

&lt;p&gt;I reproduced this issue locally on my computer by running the following command and running the scripts manually:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;--rm&lt;/span&gt; &lt;span class="nt"&gt;-it&lt;/span&gt; &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;PWD&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;:/app funky-org/bitbucket-pipelines-php bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;The solution I did was to manually specifity the &lt;code&gt;COMPOSER_HOME&lt;/code&gt; environment variable so that composer saved cache in the Bitbucket's predefined &lt;code&gt;composer&lt;/code&gt; cache location. The final yml file looks like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;funky-org/bitbucket-pipelines-php&lt;/span&gt;

&lt;span class="na"&gt;pipelines&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;default&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;step&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Run unit tests&lt;/span&gt;
        &lt;span class="na"&gt;caches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;composer&lt;/span&gt;
        &lt;span class="na"&gt;script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;COMPOSER_HOME=~/.composer composer install --no-progress&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./vendor/bin/phpunit&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;ls -la ~/.composer/cache&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope this article helps someone not to lose so much time as I did 😆&lt;/p&gt;

</description>
      <category>composer</category>
      <category>bitbucker</category>
      <category>pipelines</category>
      <category>cache</category>
    </item>
  </channel>
</rss>
