1

I have a google cloud trigger that is connected to my github repository that builds docker containers. But when I update my code it takes a really long time to build, so I want it to cache it by changing the google trigger configuration to Cloud Build configuration file from Dockerfile which was set previously (By setting it to dockerfile it takes really a long time like mentioned).

My cloudbuild.yaml looks like this:

steps:
- name: 'gcr.io/kaniko-project/executor:latest'
  args:
  - --destination=gcr.io/project/github.com/user/repo_name:$COMMIT_SHA
  - --cache=true
  - --cache-ttl=6h
  - --dockerfile=Dockerfile
timeout: 7200s

But when I run it like this it always starts from scratch and even though it builds it it's not showing up under the images section of the container registry where my builds are usually registered to and where I want them to be.

How can I get my kaniko to cache my builds so it won't take much each time I commit to my github?

Using kubernetes and docker for the build.

Turgut
  • 711
  • 3
  • 25

1 Answers1

2

If you using the Docker image build you can use the --cache-from

The easiest way to increase the speed of your Docker image build is by specifying a cached image that can be used for subsequent builds. You can specify the cached image by adding the --cache-from argument in your build config file, which will instruct Docker to build using that image as a cache source.

YAML example

steps:
- name: 'gcr.io/cloud-builders/docker'
  entrypoint: 'bash'
  args: ['-c', 'docker pull gcr.io/$PROJECT_ID/[IMAGE_NAME]:latest || exit 0']
- name: 'gcr.io/cloud-builders/docker'
  args: [
            'build',
            '-t', 'gcr.io/$PROJECT_ID/[IMAGE_NAME]:latest',
            '--cache-from', 'gcr.io/$PROJECT_ID/[IMAGE_NAME]:latest',
            '.'
        ]
images: ['gcr.io/$PROJECT_ID/[IMAGE_NAME]:latest']

Google's suggested best practice: https://cloud.google.com/build/docs/optimize-builds/speeding-up-builds

Update

Destination argument to add where you want to push your image:

    "--destination=gcr.io/$PROJECT_ID/hello:$COMMIT_SHA"

ref : https://cloud.google.com/build/docs/optimize-builds/kaniko-cache#enabling_kaniko_cache_in_your_builds

Harsh Manvar
  • 27,020
  • 6
  • 48
  • 102
  • 1
    What should I use as [IMAGE_NAME] ? – Turgut Aug 13 '22 at 14:24
  • 2
    you have to use the older build which will get a used cache, one older image. – Harsh Manvar Aug 13 '22 at 14:32
  • 1
    It looks like this is the right step forward however when I copy the exact code you've just provided I get this error: ```ERROR: build step 0 "gcr.io/kaniko-project/executor:latest" failed: starting step container failed: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "bash": executable file not found in $PATH: unknown ```, I don't have an executable called bash and I'm not directly calling an executable with my jobs. – Turgut Aug 13 '22 at 14:41
  • 1
    When i remove entrypoint it says ``` Error: You must provide --destination, or use --no-push Step #0: Usage: Step #0: executor [flags] Step #0: executor [command]``` what should I set --destination to? – Turgut Aug 13 '22 at 14:48
  • 2
    destination mean where you wan tto push your image, so it will be GCR in your case : ref :https://cloud.google.com/build/docs/optimize-builds/kaniko-cache#enabling_kaniko_cache_in_your_builds – Harsh Manvar Aug 13 '22 at 19:06
  • How to push the image to artifactory, it gives me user password issue. Can anyone suggest how to provide username and password of artifactory in detination? – Sneha Sep 15 '22 at 05:01
  • not sure you are on which artifactory or registry but you can use the login option in bash command and destination argument will push as you are already logged in so. else you can add new step with docker push command – Harsh Manvar Sep 15 '22 at 05:16
  • Its our private artifactory. Thanks for suggestion but can you just show me how to do this in cloubuild.yaml. In args we only have the option to specify destination and dockerfile, so not really sure how to mention the login option. – Sneha Sep 15 '22 at 06:10
  • here we go : https://cloud.google.com/build/docs/interacting-with-dockerhub-images#pulling_private_images_from_docker_hub feel free to do upvote the answer if found it helpful. – Harsh Manvar Sep 15 '22 at 06:22
  • I have done with docker, I am facing issue with Kaniko. steps: - name: 'gcr.io/kaniko-project/executor:latest' args: - --destination=artifactory url/image:tag - --cache=true - --cache-ttl=6h - --dockerfile=Dockerfile . I am facing issue in providing credential to access artifactory url in destination. How to provide the credential? – Sneha Sep 15 '22 at 09:18
  • here is list for all different registry, you can one as per requirement by mounting `.docker` to stage and you will be able to push image. or any other option you prefer. – Harsh Manvar Sep 15 '22 at 09:28