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

Is it possible to read the image from cache without building? #670

Closed
audunsolemdal opened this issue Aug 12, 2022 · 4 comments
Closed

Is it possible to read the image from cache without building? #670

audunsolemdal opened this issue Aug 12, 2022 · 4 comments

Comments

@audunsolemdal
Copy link

My use case is the following:

  1. Build on self hosted github runner (succeeds) + cache to gha
  2. From self hosted runner, read from cache and push to a private container registry

It seems to me as if there is no way to use docker/build-push-action@v3 without trying to build the image? From the error logs it seems at it is trying to clone my git repo and look for a dockerfile, but this is not what I want. I simply want a docker load the contents of the cache. Is this possible?

Job 1 (Github hoster runner): docker build and tag image, upload image artifact - works as expected and writes to cache

  steps:
    - name: Docker meta
      id: meta
      uses: docker/metadata-action@v4
      with:
        images: |
          ${{ inputs.image-name }}
        tags: |
          type=sha
          type=raw,value=${{ inputs.tag }}

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v2

    - name: Build docker image using cache
      uses: docker/build-push-action@v3
      with:
        context: ${{ inputs.build-path }}
        push: false
        tags: ${{ steps.meta.outputs.tags }}
        labels: ${{ steps.meta.outputs.labels }}
        cache-from: type=gha
        cache-to: type=gha,mode=max

Job 2 (Self hosted runner as RunnerDeployment) - load image from cache, docker push the loaded container image to a private container registry

  steps:
    # Login and push containe image to Azure Container Registry (ACR)
    - name: Set ACR context
      uses: azure/docker-login@v1
      with:
        login-server: ${{ inputs.acr-name }}
        username: ${{ inputs.acr-clientid }}
        password: ${{ inputs.acr-clientsecret }}

    - run: docker context create mycontext
      shell: bash
    - run: docker context use mycontext
      shell: bash
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v2
      with:
        buildkitd-flags: --debug
        driver: docker
        endpoint: mycontext

    - name: Build docker image using cache
      uses: docker/build-push-action@v3
      with:
        context: ${{ inputs.build-path }}
        push: true
        tags: ${{ github.sha }}
        cache-from: type=gha

Error:

/usr/local/bin/docker buildx build --cache-from type=gha --iidfile /tmp/docker-build-push-VVHSrL/iidfile --secret id=GIT_AUTH_TOKEN,src=/tmp/docker-build-push-VVHSrL/tmp-431-qCiWelxp5KCI --tag a28e9181b150e2c6edeef6f7c462d59343f967ac --metadata-file /tmp/docker-build-push-VVHSrL/metadata-file --push https://github.com/myorg/wl-testteam1.git#a28e9181b150e2c6edeef6f7c462d59343f967ac
#1 [internal] load git source https://github.com/myorg/wl-testteam1.git#a28e9181b150e2c6edeef6f7c462d59343f967ac
#1 ERROR: failed to init repo at /home/rootless/.local/share/docker/overlay2/o3cj18v6ht55zwbufxgxntxdo/diff: exec: "git": executable file not found in $PATH
------
 > [internal] load git source https://github.com/myorg/wl-testteam1.git#a28e9181b150e2c6edeef6f7c462d59343f967ac:
------
error: failed to solve: rpc error: code = Unknown desc = failed to solve with frontend dockerfile.v0: failed to read dockerfile: failed to init repo at /home/rootless/.local/share/docker/overlay2/o3cj18v6ht55zwbufxgxntxdo/diff: exec: "git": executable file not found in $PATH
Error: buildx failed with: error: failed to solve: rpc error: code = Unknown desc = failed to solve with frontend dockerfile.v0: failed to read dockerfile: failed to init repo at /home/rootless/.local/share/docker/overlay2/o3cj18v6ht55zwbufxgxntxdo/diff: exec: "git": executable file not found in $PATH
@crazy-max
Copy link
Member

Don't think that's an issue with the cache but your Dockerfile. Can you post your Dockerfile or link to your repo?

@audunsolemdal
Copy link
Author

The Dockerfile is simply
FROM busybox:latest `

git is not installed on the runner I am running the job on which causes some issues.

@audunsolemdal
Copy link
Author

Ok, so I managed to get it working by installing git and fixingthe context context: ${{ inputs.build-path }}`

However, from what I can see the cache size is only 183 bytes (output from gh actions-cache list). This makes me believe that only some metadata is cached, and builds still have to be done every time.

@crazy-max
Copy link
Member

crazy-max commented Sep 2, 2022

However, from what I can see the cache size is only 183 bytes (output from gh actions-cache list). This makes me believe that only some metadata is cached, and builds still have to be done every time.

Yes this is expected as you don't have any runnable instructions that could be cached when using only FROM. You can add some RUN commands to start caching something:

# from.Dockerfile
FROM busybox:latest
# from-run.Dockerfile
FROM busybox:latest
RUN echo "hello"
name: build

on:
  push:
    branches:
      - 'main'

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        file:
          - from
          - from-run
    steps:
      -
        name: Checkout
        uses: actions/checkout@v3
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      -
        name: Build
        uses: docker/build-push-action@master
        with:
          context: .
          file: ./${{ matrix.file }}.Dockerfile
          cache-from: type=gha,scope=${{ matrix.file }}
          cache-to: type=gha,scope=${{ matrix.file }},mode=max

from job

#5 [1/1] FROM docker.io/library/busybox:latest@sha256:20142e89dab967c01765b0aea3be4cec3a5957cc330f061e5503ef6168ae6613
#5 resolve docker.io/library/busybox:latest@sha256:20142e89dab967c01765b0aea3be4cec3a5957cc330f061e5503ef6168ae6613 done
#5 DONE 0.0s

#6 importing cache manifest from gha:13374401039022741620
#6 DONE 0.4s

#5 [1/1] FROM docker.io/library/busybox:latest@sha256:20142e89dab967c01765b0aea3be4cec3a5957cc330f061e5503ef6168ae6613
#5 DONE 0.0s

#7 exporting cache
#7 preparing build cache for export done
#7 DONE 0.3s

from-run job

#5 [1/2] FROM docker.io/library/busybox:latest@sha256:20142e89dab967c01765b0aea3be4cec3a5957cc330f061e5503ef6168ae6613
#5 resolve docker.io/library/busybox:latest@sha256:20142e89dab967c01765b0aea3be4cec3a5957cc330f061e5503ef6168ae6613 done
#5 DONE 0.0s

#6 importing cache manifest from gha:13550490130606782471
#6 DONE 0.4s

#7 [2/2] RUN echo "hello"
#7 CACHED

#8 exporting cache
#8 preparing build cache for export done
#8 DONE 0.3s

Closing this issue since that should answer your question, but feel free to re-open if it doesn't. Thanks.

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

No branches or pull requests

2 participants