Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github actions/cache has no effect on my workflow

I am trying to run the following Github workflow for a php project:

name: CI

on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    env:
      DB_NAME: ${{ secrets.DB_NAME }}
      DB_USER: ${{ secrets.DB_USER }}
      DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
      MYSQL_ROOT_PASSWORD: ${{ secrets.MYSQL_ROOT_PASSWORD }}

    steps:
    - uses: actions/checkout@v2

    - name: Set up the CI environment
      run: |
        envsubst < src/.env.ci > src/.env
        docker pull composer
        alias composer="docker run --rm --interactive --tty --volume $(pwd):/app --volume ${COMPOSER_HOME:-$HOME/.composer}:/tmp --user $(id -u):$(id -g) composer"
        composer --version

    - name: Get Composer Cache Directory
      id: composer-cache
      run: |
        echo "::set-output name=dir::$(composer config cache-files-dir)"

    - uses: actions/cache@v1
      with:
        path: ${{ steps.composer-cache.outputs.dir }}
        key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
        restore-keys: |
          ${{ runner.os }}-composer-

    - name: Install composer dependencies
      if: steps.composer-cache.outputs.cache-hit != 'true'
      run: composer install

The scenario is the following. I have a docker based development environment orchestrated with docker-compose. Php composer and other tools are run from docker images.

It seems that i am missing something here because the cache-hit is taking place as I see in logs:

2020-03-28T10:37:07.2837003Z Cache restored from key: Linux-composer-0dffe7e110c8249b30d4e46844fede7ea6b8e1433061bed12cbb9a2ae964e2bb

But is not taking effect because the step Install composer dependencies is still running and downloading artifacts. My expectation is that either does not run or run but does not download anything since it takes all from restored cached.

Does anyone have an idea of what I am missing?

UPDATE

I did accepted @edric answer because on this context where i asked my question his answer provided the solution for my question. Although I muss say it was not completely the solution.

I needed to delete the condition: if: steps.composer-cache.outputs.cache-hit != 'true' from last step to get the rest of my workflow running. I did noticed that the composer cache was restored and that composer install was not run, causing problems later on due to missing dependencies. Without the if condition composer install run always but using the restored cache.

like image 694
davidmpaz Avatar asked Nov 15 '25 22:11

davidmpaz


1 Answers

That's because you didn't set an ID on the step that caches your lock file and you're also using the wrong ID in the following step to check if a cache was hit. I suggest that you should rename the ID of the step that retrieves the cache directory to a different ID so as to not confuse yourself:

- name: Get Composer Cache Directory
  id: get-composer-cache-dir # Instead of composer-cache
  run: |
    echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache Composer
  uses: actions/cache@v1
  id: composer-cache
  with:
        path: ${{ steps.get-composer-cache-dir.outputs.dir }}
        key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
        restore-keys: |
          ${{ runner.os }}-composer-
- name: Install composer dependencies
  if: steps.composer-cache.outputs.cache-hit != 'true'
  run: composer install
like image 134
Edric Avatar answered Nov 18 '25 21:11

Edric



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!