This build step creates a base image builder that uses a GCE instance similar to remote buidler. The base-image-builder requires some specific licenses be added for nested virtualization. This builder makes use of packer to build an agent image that will be used by the resulting builder to build GCP base images. That agent also has packer installed.
The builder also adds the packer builder if it is not already part of the project's container registry. The resulting builder requires the following environmental settings:
- BUILD_NUMBER - added to resulting base image name (available by default with GCB)
- PROJECT_ID - used to store the image (available by default with GCB)
- TAG_NAME - included in the name of the stored image (available with GCB when tags are pushed with git)
- IMAGE_ZONE where to build/upload the image (passed in as a substitution with GCB)
- PACKER_SPEC the path to the Packer specification in the workspace (passed in as a substitution with GCB)
PROJECT=$(gcloud config get-value project)
GCB_SA=$(gcloud projects get-iam-policy $PROJECT --filter="(bindings.role:roles/cloudbuild.builds.builder)" --flatten="bindings[].members" --format="value(bindings.members[])")
gcloud projects add-iam-policy-binding $PROJECT \
--member $GCB_SA --role roles/compute.instanceAdmin.v1
gcloud projects add-iam-policy-binding $PROJECT \
--member $GCB_SA --role roles/iam.serviceAccountUser
Run this build step and it will:
- Create a packer container image in your GCP Project GCR (later used as a build step)
- Create a custom GCE image with nested virtualization enabled
- Create a Packer Agent with packer installed and nested virtualization enabled
- Create a base-image-builder container image in your GCP Project GCR
$ git clone https://github.com:GoogleCloudPlatform/cloud-builders-community
$ cd cloud-builders-community/base-image-builder
$ gcloud builds submit .
# or pass in custom substitutions
$ export ZONE=us-central1-c
$ gcloud builds submit . --substitutions "_IMAGE_ZONE=$ZONE,_BUILD_PACKER_AGENT_IMAGE=true,_BUILD_NESTED_VIRT_IMAGE=true"
gsutil mb gs://$PROJECT-custom-images
After the builder is successfully built (with the above steps), you can use it to build custom images from an ISO:
$ git clone https://github.com/GoogleCloudPlatform/compute-custom-boot-images
$ cd compute-custom-boot-images
$ gcloud builds submit . --config cloudbuild/custom-boot-image-build.yaml
After the build is successful, you will have a custom GCE image available in your GCS Bucket