How to increase speed Google Cloud build
Since in recent work we using continuous integration for development, one of service we rely is cloud build.
It automatically build docker image based on our specific trigger (tag creation in bitbucket), but it took some times to get job done (about 18–20 minutes) and it can delay some testing and production deployment.
At first we read some google documentation here but we found some aspect not too optimized. With several trial, we can decrease build time to around 5 minutes. And there are some tips:
we set docker private image with included base image (use alpine or slim image) + installed library, we set credential in google secret manager, so it secure enough to put in repository, so docker start with login with private docker account. For example we install font, pdf generator, node JS and yarn
cloudbuild.yaml
- name: 'gcr.io/cloud-builders/docker'
entrypoint: 'bash'
args: [ '-c', 'docker login --username $$DOCKER_USERNAME --password $$DOCKER_TOKEN']
secretEnv: ['DOCKER_USERNAME', 'DOCKER_TOKEN']
-- rest of docker code here --
availableSecrets:
secretManager:
- versionName: projects/SOME_PROJECT_ID/secrets/SOME_SECRET_KEY_1/versions/latest
env: 'DOCKER_USERNAME'
- versionName: projects/SOME_PROJECT_ID/secrets/SOME_SECRET_KEY_2/versions/latest
env: 'DOCKER_TOKEN'
In dockerfile we can use base image from private repository
# Base image:
FROM DOCKER_REPO/PROJECT:TAG
Next step: upgrade machine type
Google cloud build can use specific machine type to increase build speed, we know it will increase cost, but since it billed by time used and it decrease build time, it will par. See technical documentation here
The default machine type is 1 CPU. Requesting a high-CPU virtual machine may increase the startup time of your build. Since in our build, we can use parallel cpu, we choose maximum eligible cpu. If your build cannot run parallel, this step may not relevant
Took below code in last code of cloudbuild.yaml
options:
machineType: 'E2_HIGHCPU_32'
So these are our result