question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Builder not using cache

See original GitHub issue

Behaviour

Hi, I can’t seem to get the build-push-action to use the restored cache when building. I’ve tried following the steps in the market place instructions and recommendation here: https://github.com/docker/build-push-action/issues/132#issuecomment-699502544, but to no avail.

The logs indicate that the cache is restored, but when the build runs it seems to build everything from scratch.

Steps to reproduce

I have this in a test repo here: https://github.com/ltamrazov/testingactions

Expected behaviour

Cache is used to rebuild images on rerunning a workflow and on any new workflow run.

Actual behaviour

Images are rebuild from scratch.

Configuration

name: ci

on:
    push:
        branches: [ master ]
    pull_request:
        branches: [ master ]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - name: Builder
      uses: docker/setup-buildx-action@v1
      id: buildx

    - name: Cache main image layers
      uses: actions/cache@v2
      with:
        path: /tmp/.buildx-cache
        key: ${{ runner.os }}-buildx-${{ github.sha }}
        restore-keys: |
          ${{ runner.os }}-buildx-

    - name: Checkout Code
      uses: actions/checkout@v2
    
    - name: Determine Image Tag
      id: generate_tag
      run: |
          _tag=latest
          if [ "${{ github.event_name }}" == "pull_request" ]
          then
              _tag=pr-${{ github.event.number }}
          fi
          _full=test/dev:${_tag}
          echo ::set-output name=tag::${_full}

    - name: Build code
      uses: docker/build-push-action@v2
      with:
        builder: ${{ steps.buildx.outputs.name }}
        push: false
        target: test
        load: true
        cache-from: type=local,src=/tmp/.buildx-cache
        cache-to: type=local,mode=max,dest=/tmp/.buildx-cache
        tags: ${{ steps.generate_tag.outputs.tag }}
    
    - name: Build DB
      uses: docker/build-push-action@v2
      with:
        builder: ${{ steps.buildx.outputs.name }}
        file: Dockerfile.postgres
        load: true
        cache-from: type=local,src=/tmp/.buildx-cache
        cache-to: type=local,mode=max,dest=/tmp/.buildx-cache
        tags: db-${{ steps.generate_tag.outputs.tag }}
    
    - name: Run Tests
      run: |
        API_IMAGE_TAG=${{ steps.generate_tag.outputs.tag }} DB_IMAGE_TAG=db-${{ steps.generate_tag.outputs.tag }} docker-compose -f docker-compose.yml --env-file .env.ci run test-api

Logs

logs_20.zip

Thank you!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
crazy-maxcommented, Oct 4, 2020

Hi @ltamrazov

That’s because your cache is mixed with 2 images. You have to create another cache:

name: ci

on:
    push:
        branches: [ master ]
    pull_request:
        branches: [ master ]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - name: Builder
      id: buildx
      uses: docker/setup-buildx-action@v1
      with:
        buildkitd-flags: --debug

    - name: Cache main image layers
      uses: actions/cache@v2
      with:
        path: /tmp/.buildx-main-cache
        key: ${{ runner.os }}-buildx-main-${{ github.sha }}
        restore-keys: |
          ${{ runner.os }}-buildx-main-

    - name: Cache postgres image layers
      uses: actions/cache@v2
      with:
        path: /tmp/.buildx-postgres-cache
        key: ${{ runner.os }}-buildx-postgres-${{ github.sha }}
        restore-keys: |
          ${{ runner.os }}-buildx-postgres-

    - name: Checkout Code
      uses: actions/checkout@v2
    
    - name: Determine Image Tag
      id: generate_tag
      run: |
          _tag=latest
          if [ "${{ github.event_name }}" == "pull_request" ]
          then
              _tag=pr-${{ github.event.number }}
          fi
          _full=test/dev:${_tag}
          echo ::set-output name=tag::${_full}
    - name: Build code
      uses: docker/build-push-action@v2
      with:
        builder: ${{ steps.buildx.outputs.name }}
        push: false
        target: test
        load: true
        cache-from: type=local,src=/tmp/.buildx-main-cache
        cache-to: type=local,mode=max,dest=/tmp/.buildx-main-cache
        tags: ${{ steps.generate_tag.outputs.tag }}
    
    - name: Build DB
      uses: docker/build-push-action@v2
      with:
        builder: ${{ steps.buildx.outputs.name }}
        file: Dockerfile.postgres
        load: true
        cache-from: type=local,src=/tmp/.buildx-postgres-cache
        cache-to: type=local,mode=max,dest=/tmp/.buildx-postgres-cache
        tags: db-${{ steps.generate_tag.outputs.tag }}
    
    - name: Run Tests
      run: |
        API_IMAGE_TAG=${{ steps.generate_tag.outputs.tag }} DB_IMAGE_TAG=db-${{ steps.generate_tag.outputs.tag }} docker-compose -f docker-compose.yml --env-file .env.ci run test-api

Tested: https://github.com/crazy-max/testingactions/runs/1204121486?check_suite_focus=true

1reaction
ltamrazovcommented, Oct 5, 2020

aaah, ya that did the trick. After pruning it started to rebuild from scratch, so it was never using the specified cache. Thank you for the quick response and your time!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Docker build is not using cache - Stack Overflow
I am modifying the Dockerfile, adding commands at the end of the file. So the previous layers should be cached and valid. I've...
Read more >
Builder not using cache for unmodified build stages?
It seems to me that the reason is the builder doesn't reuse cache for certain stages in the multistage build. An example of...
Read more >
Docker build not using cache - GitLab Forum
I am trying to speed up my Docker build by using the --cache-from option. ... Notice that for example step [builder 4/6] is...
Read more >
Caching challenges and strategies - Amazon AWS
Cached data necessarily grows inconsistent with the source over time, so caching can only be successful if both the service and its clients...
Read more >
UX Builder not loading WP-total cache | WordPress.org
Hi, i use flatsome for my website but for some reason i'm stuck in a loading loop for the UX Builder. I tried...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found