I'm using google cloud build and a new build is triggered each time a new commit is pushed to my branch.
Everything is working fine (artifacts and cloud run machine) but I'm trying to speed up the build time.
I've seen a lot of tutorial and SO answers but I can't figure out how to optimize in my case: I'm using cloudbuild.yaml file and Docker, both visibile here:
CLOUDBUILD.YAML
steps:
- name: gcr.io/cloud-builders/docker
args:
- build
- '-f'
- 'Dockerfile'
- '-t'
- '${_IMG_REGION}-docker.pkg.dev/$PROJECT_ID/${_IMG_REPO}/${_IMG_NAME}:$SHORT_SHA'
- .
- name: gcr.io/cloud-builders/docker
args:
- push
- '${_IMG_REGION}-docker.pkg.dev/$PROJECT_ID/${_IMG_REPO}/${_IMG_NAME}:$SHORT_SHA'
- name: gcr.io/google.com/cloudsdktool/cloud-sdk
args:
- run
- deploy
- '${_RUN_SERVICE}'
- '--image=${_IMG_REGION}-docker.pkg.dev/$PROJECT_ID/${_IMG_REPO}/${_IMG_NAME}:$SHORT_SHA'
- '--region=${_RUN_REGION}'
- '--service-account=${_ACCOUNT_EMAIL}'
- '--platform=managed'
- '--ingress=internal-and-cloud-load-balancing'
- '--concurrency=200'
- '--timeout=600'
- '--min-instances=1'
- '--max-instances=2'
- '--cpu=1'
- '--memory=256Mi'
- '--cpu-boost'
- '--cpu-throttling'
entrypoint: gcloud
images:
- '${_IMG_REGION}-docker.pkg.dev/$PROJECT_ID/${_IMG_REPO}/${_IMG_NAME}:$SHORT_SHA'
DOCKERFILE
FROM node:20.5.1-alpine AS step1
LABEL version="23.10.01"
WORKDIR /temp
COPY package.json .
RUN npm install
COPY ./tsconfig.json .
COPY src/ ./src
RUN npm run build
FROM node:20.5.1-alpine AS step2
WORKDIR /app/
COPY package.json .
RUN npm install --omit=dev && npm cache clean --force
COPY --from=step1 /temp/build ./build
ENV NODE_ENV production
ENV APP_ENV dev
EXPOSE $PORT
CMD [ "npm", "run", "start:prod" ]
Every time the new build start, the entire process is repeated and last more than 10 minutes, when in my local machine, using cache, it requires no more than 10 seconds.
First of all, it would be good to see which step is taking a long time and then fix the problem. Since I have no idea which step in your build is taking a long time, I can only share some of my observations about optimizing builds in cloudbuild.
Your build pipeline can be improved right here and now. Briefly about the improvement in this case:
cloudbuild.yamlperforms its main task - successfully builds the project);cloudbuild.yaml;Some thoughts on point 7.
In the following
cloudbuild.yamlI will try to demonstrate my suggestions:P.S. Perhaps it was worth writing a separate example for each point. But it seems to me that a single
cloudbuild.yamlwould be more useful.