Search code examples
laravelgithub-actions

Laravel Github Workflow Action - Cache composer dependencies, not working as intended


This is my .github/workflow/staging.yml setup

name: Deploy Staging

on:
  push:
    branches:
      - staging

jobs:
  deploy-staging:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Cache composer dependencies
        uses: actions/cache@v2
        with:
          path: vendor
          key: composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            composer-

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.2
          tools: composer:v2
          coverage: none

      - name: Install Project Dependencies
        working-directory: ./application
        run: composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader --no-dev

      ... rest of the steps

My laravel code exists inside a folder called application (hence the use of working-directory directive)

I've deployed the app once with the above changes and the vendor folder cache supposedly "warmed up". Then on subsequent deployment it looked like this:

enter image description here

as you can see, it said restored ~0 MB of cached vendor and in the rest of the flow it downloads and installs the composer dependencies again (even though the composer.lock file hasn't changed)

I tried changing hash checking location like this and it did not help either:

composer-${{ hashFiles('./application/composer.lock') }}

Any ideas what might be going wrong here?


Solution

  • it seems that the path you have specified for the composer cache is incorrect.

    In your workflow, you are setting the working directory to ./application before running composer install, but the cache action is looking for the vendor folder in the root directory.

    You need to change the path in the cache action to ./application/vendor, this should then correctly cache your composer dependencies and make them available for subsequent runs.

    Here's the updated caching step in your workflow:

    - name: Cache composer dependencies
      uses: actions/cache@v2
      with:
        path: ./application/vendor
        key: composer-${{ hashFiles('**/composer.lock') }}
        restore-keys: |
          composer-
    

    Remember that hashFiles('**/composer.lock') will hash all composer.lock files in your repository including in all subdirectories. If you have multiple composer.lock files, consider specifying the exact path to the one you want.

    As well as, make sure the composer.lock file is committed and pushed into your repository, as the GitHub actions runner may not find it if it's not included in your repository.