Packer is an open source tool for creating identical Virtual Machine (VM) images for multiple platforms from a single source configuration. This page explains how to use Packer and Cloud Build to create a VM image for use on Compute Engine.
Before you begin
The instructions on this page assume that you are familiar with Packer
. In addition:
- Have your source code including the Packer template handy.
- If you want to use the
gcloudcommands in this page, install the Google Cloud CLI . -
Enable the following APIs:
gcloud services enable compute.googleapis.com gcloud services enable servicemanagement.googleapis.com gcloud services enable storage-api.googleapis.com
Required IAM permissions
-
To use Packer with Cloud Build, grant the Compute Engine Instance Admin role (
roles/compute.instanceAdmin.v1) to your build service account. -
To store built images in Artifact Registry, grant the Artifact Registry Writer (
roles/artifactregistry.writer) role to your build service account.
Creating a Packer builder image
Cloud Build provides a Packer community builder image
that you can use to invoke packer
commands in Cloud Build.
Before using this builder in a Cloud Build config file, you must build
the image and push it to Artifact Registry:
-
Clone the cloud-builders-community repository:
git clone https://github.com/GoogleCloudPlatform/cloud-builders-community.git -
Navigate to the Packer builder image:
cd cloud-builders-community/packer -
Submit the builder to your project:
gcloud builds submit .
Using the Packer builder
-
Ensure that you have your packer.json file along with your source code.
-
In your project root directory, create a build config file named
cloudbuild.yamlorcloudbuild.json. -
In your build config file, add a build step to invoke the
packer buildcommand:YAML
steps : - name : 'gcr.io/[PROJECT_ID]/packer' args : - build - -var - image_name=[IMAGE_NAME] - -var - project_id=[PROJECT_ID] - -var - image_family=[IMAGE_FAMILY] - -var - image_zone=[IMAGE_ZONE] - packer.jsonJSON
{ "steps" : [ { "name" : "gcr.io/[PROJECT_ID]/packer" , "args" : [ "build" , "-var" , "image_name=[IMAGE_NAME]" , "-var" , "project_id=[PROJECT_ID]" , "-var" , "image_family=[IMAGE_FAMILY]" , "-var" , "image_zone=[IMAGE_ZONE]" , "packer.json" ] } ] }Where:
-
[PROJECT_ID]is your Google Cloud project ID. -
[IMAGE_NAME]is the name of the VM image you're building. -
[IMAGE_FAMILY]is the image family of the VM image. -
[IMAGE_ZONE]is the image zone .
-
-
Start the build using the build config file:
gcloud builds submit --region=[REGION] --config [CONFIG_FILE_PATH] [SOURCE_DIRECTORY]Where:
-
[CONFIG_FILE_PATH]is the path to the build config file. -
[SOURCE_DIRECTORY]is the path or URL to the source code. -
[REGION]is one of the supported build regions .
If you don't specify a
[CONFIG_FILE_PATH]and[SOURCE_DIRECTORY]in thegcloud builds submitcommand, Cloud Build assumes that the config file and the source code are in the current working directory. -
Once the images are built, you can view them in the Compute Engine Image page in the Google Cloud console.
What's next
- Learn how to build containers .
- Learn how to build
Goprojects . - Learn how to troubleshoot build errors .

