Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache miss with cargo/rust #743

Open
kpcyrd opened this issue Dec 8, 2022 · 5 comments
Open

Cache miss with cargo/rust #743

kpcyrd opened this issue Dec 8, 2022 · 5 comments

Comments

@kpcyrd
Copy link

kpcyrd commented Dec 8, 2022

Behaviour

I'm trying to do docker build -t something . and use the github actions cache for caching.

Steps to reproduce this issue

FROM rust:1-alpine3.16
ENV RUSTFLAGS="-C target-feature=-crt-static"
RUN apk add --no-cache musl-dev
WORKDIR /app
COPY ./ /app
RUN --mount=type=cache,target=/var/cache/buildkit \
    CARGO_HOME=/var/cache/buildkit/cargo \
    CARGO_TARGET_DIR=/var/cache/buildkit/target \
    cargo build --release --locked && \
    cp -v /var/cache/buildkit/target/release/something .
RUN strip something
jobs:
  build:
    runs-on: ubuntu-22.04
    steps:
    - uses: actions/checkout@v3
    - uses: docker/setup-buildx-action@v2

    - name: Build Docker image
      uses: docker/build-push-action@v3
      with:
        tags: something
        load: true
        cache-from: type=gha
        cache-to: type=gha,mode=max

Expected behaviour

The cache is written to and read from github actions cache, the build finishes quickly.

Actual behaviour

It seems the cache is partially used, the RUN apk ... layer seems to be read from cache but after COPY ./ /app it's a cache miss and cargo build hangs on Updating crates.io index for a long time even though it should be read from cache because of RUN --mount=type=cache,target=/var/cache/buildkit CARGO_HOME=/var/cache/buildkit/cargo ....

Possibly related to #741, #735.

Configuration

name: Docker Image CI

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:
    runs-on: ubuntu-22.04
    steps:
    - uses: actions/checkout@v3
    - uses: docker/setup-buildx-action@v2

    - name: Build Docker image
      uses: docker/build-push-action@v3
      with:
        tags: sh4d0wup
        load: true
        cache-from: type=gha
        cache-to: type=gha,mode=max

    - name: Test the Docker image
      run: docker run --rm sh4d0wup --help

    - name: Login to github container registry
      if: github.event_name != 'pull_request'
      uses: docker/login-action@v2
      with:
        registry: ghcr.io
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}

    - name: Push the image to `edge`
      if: github.event_name != 'pull_request'
      run: |
        docker tag sh4d0wup ghcr.io/kpcyrd/sh4d0wup:edge
        docker push ghcr.io/kpcyrd/sh4d0wup:edge

Logs

logs_44.zip

@kpcyrd
Copy link
Author

kpcyrd commented Dec 9, 2022

I started another build with debugging enabled:

jobs:
  build:
    runs-on: ubuntu-22.04
    steps:
    - uses: actions/checkout@v3
    - uses: docker/setup-buildx-action@v2
      with:
        buildkitd-flags: --debug

[...]

Build URL: https://github.com/kpcyrd/sh4d0wup/actions/runs/3653117586/jobs/6172218379

Logs

logs_46.zip

@crazy-max
Copy link
Member

crazy-max commented Dec 15, 2022

@kpcyrd Cache mounts through --mount=type=cache are not exported atm. They are just useful for incremental builds. So if you want this to be exported you have to remove this cache mount. This is tracked in moby/buildkit#1512

@jedevc @dvdksn I'm not sure if we have this properly documented atm?

@dvdksn
Copy link
Contributor

dvdksn commented Dec 15, 2022

Yeah I don't think we have covered this case. We've got some work to do on run mounts in general, but we could probably mention this in the docs for the gha backend.

@cemo
Copy link

cemo commented Apr 3, 2023

What should I have in my docker file and github action configuration to have a proper cache?

@kpcyrd
Copy link
Author

kpcyrd commented Apr 3, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants