Search code examples
google-cloud-platformgoogle-cloud-buildkaniko

Using Kaniko cache with Google Cloud Build for Google Cloud Kubernetes Deployments


We have been using Google Cloud Build via build triggers for our GitHub repository which holds a C++ application that is deployed via Google Cloud Kubernetes Cluster.

enter image description here

As seen above, our build configuration is arriving from Dockerfile which is located in our GitHub repository.

Everything is working as expected, however our builds lasts about 55+ minutes. I would like to add Kaniko Cache support as suggested [here], however Google Cloud document only suggests a way to add it via a yaml file as below :

steps:
- name: 'gcr.io/kaniko-project/executor:latest'
  args:
  - --destination=gcr.io/$PROJECT_ID/image
  - --cache=true
  - --cache-ttl=XXh

How shall I achieve Kaniko builds with a Dockerfile based trigger ?

FROM --platform=amd64 ubuntu:22.10
ENV GCSFUSE_REPO gcsfuse-stretch

RUN apt-get update && apt-get install --yes --no-install-recommends \
    ca-certificates \
    curl \
    gnupg \
  && echo "deb http://packages.cloud.google.com/apt $GCSFUSE_REPO main" \
    | tee /etc/apt/sources.list.d/gcsfuse.list \
  && curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - \
  && apt-get update \
  && apt-get install --yes gcsfuse \
  && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* 

EXPOSE 80

RUN \
  sed -i 's/# \(.*multiverse$\)/\1/g' /etc/apt/sources.list && \
  apt-get update && \
  apt-get -y upgrade && \
  apt-get install -y build-essential && \
  apt-get install -y gcc && \
  apt-get install -y software-properties-common && \
  apt install -y cmake && \
  apt-get install -y make && \
  apt-get install -y clang && \
  apt-get install -y mesa-common-dev && \
  apt-get install -y git && \
  apt-get install -y xorg-dev && \
  apt-get install -y nasm && \
  apt-get install -y byobu curl git htop man unzip vim wget && \
  rm -rf /var/lib/apt/lists/* 

# Update and upgrade repo
RUN apt-get update -y -q && apt-get upgrade -y -q 

COPY . /app
RUN cd /app
RUN ls -la

# Set environment variables.
ENV HOME /root
ENV WDIR /app

# Define working directory.
WORKDIR /app

RUN cd /app/lib/glfw && cmake -G "Unix Makefiles" && make && apt-get install libx11-dev
RUN apt-cache policy libxrandr-dev
RUN apt install libxrandr-dev

RUN cd /app/lib/ffmpeg && ./configure && make && make install
RUN cmake . && make

# Define default command.
CMD ["bash"]

Any suggestions are quite welcome.


Solution

  • As I mentioned in the comment, You can only add your kaniko in your cloudbuild.yaml files as its also the only options shown in this github link but you can add the --dockerfile argument to find your Dockerfile path.