HashiCorp Packer is a powerful open-source tool that enables automated building of machine images, including Virtual Machine (VM) images for Compute Engine. Cloud Build is a hosted service by Google Cloud for continuous integration. In this exercise, you will set up a Cloud Build pipeline to bake VM images using Packer.
If you don't already have a Google Account (Gmail or Google Apps), you must create one. Sign-in to Google Cloud Platform console (console.cloud.google.com) and create a new project:
Remember the project ID, a unique name across all Google Cloud projects (the name above has already been taken and will not work for you, sorry!). It will be referred to later in this codelab as PROJECT_ID
.
Next, you'll need to enable billing in the Cloud Console in order to use Google Cloud resources.
Running through this codelab shouldn't cost you more than a few dollars, but it could be more if you decide to use more resources or if you leave them running (see "cleanup" section at the end of this document).
New users of Google Cloud Platform are eligible for a $300 free trial.
If you see a "request account button" at the top of the main Codelabs window, click it to obtain a temporary account. Otherwise ask one of the staff for a coupon with username/password.
These temporary accounts have existing projects that are set up with billing so that there are no costs associated for you with running this codelab.
Note that all these accounts will be disabled soon after the codelab is over.
Use these credentials to log into the machine or to open a new Google Cloud Console window https://console.cloud.google.com/. Accept the new account Terms of Service and any updates to Terms of Service.
Here's what you should see once logged in:
When presented with this console landing page, please select the only project available. Alternatively, from the console home page, click on "Select a Project" :
This tutorial will use a number of Google Cloud services. Before proceeding, you need to activate them. This only needs to be done once per project to make the API accessible.
$ gcloud services enable compute.googleapis.com \ sourcerepo.googleapis.com \ cloudbuild.googleapis.com \ storage-api.googleapis.com
As Cloud Build is triggered by git commit pushes, create a Cloud Source Repository to store the code for this codelab.
$ gcloud source repos create packer-image-build
Then clone the repository out into Cloud Shell and enter it.
$ gcloud source repos clone packer-image-build && cd packer-image-build
To use Packer from Google Cloud Build, you need to build an image with Packer installed. Thankfully, one already exists for you to use - you just need to add it to your project.
Clone the cloud-builders-community repo:
$ git clone https://github.com/GoogleCloudPlatform/cloud-builders-community
Go to the directory that has the source code for the packer Docker image:
$ cd cloud-builders-community/packer
Build the Docker image via Cloud Build:
$ gcloud builds submit --config cloudbuild.yaml .
Remove this temporary directory:
$ cd ../.. && rm -rf cloud-builders-community
To run Packer builds, you need a packer.json file which defines the image properties and provisioning steps to take.
Download the sample Packer configuration from GitHub:
$ curl -sSfO https://raw.githubusercontent.com/morgante/google-cloud-examples/codelab-packer/codelabs/packer-cloud-build/packer.json
This file specifies to build a Debian image in the us-central1
region and writes the Operating System info to a sysinfo.txt
file.
{
"variables": {
"project_id": "{{env `PROJECT_ID`}}",
"source_image_family": "debian-9",
"machine_type": "n1-standard-1",
"region": "us-central1",
"zone": "us-central1-a"
},
"builders": [
{
"type": "googlecompute",
"project_id": "{{user `project_id`}}",
"machine_type": "{{user `machine_type`}}",
"source_image_family": "{{user `source_image_family`}}",
"image_description": "Sample Debian Image",
"image_name": "debian-demo-",
"disk_size": 16,
"disk_type": "pd-ssd",
"ssh_username": "root",
"region": "{{user `region`}}",
"zone": "{{user `zone`}}"
}
],
"provisioners": [
{
"type": "shell",
"inline": [
"uname -a >> ~/sysinfo.txt"
]
}
]
}
Now that you have a Packer image and configuration, you need to configure Google Cloud Build to run Packer.
Cloud Build bases its build steps on a cloudbuild.yaml file in your repository. Download the sample yaml from GitHub:
$ curl -sSfO https://raw.githubusercontent.com/morgante/google-cloud-examples/codelab-packer/codelabs/packer-cloud-build/cloudbuild.yaml
steps:
- name: 'gcr.io/$PROJECT_ID/packer'
args:
- build
- packer.json
In order to build images, Cloud Build needs permission to access the Compute Engine API in your project.
Retrieve the Cloud Build Service Account's email:
$ CLOUD_BUILD_SA=$(gcloud projects get-iam-policy $GOOGLE_CLOUD_PROJECT --flatten=bindings --filter="bindings.role:roles/cloudbuild.builds.builder" --format="value(bindings.members[])")
Grant it access to your project:
$ gcloud projects add-iam-policy-binding $GOOGLE_CLOUD_PROJECT \ --member $CLOUD_BUILD_SA \ --role roles/editor
You now need to configure Cloud Build to automatically trigger builds every time your repository is pushed to.
Now that you have configured Cloud Build and Packer, you just need to push your code and trigger your first build.
Before you can push, you need to configure Git. You can use your real information or fake information in this step:
$ git config --global user.email "you@example.com" $ git config --global user.name "Your Name"
Next, push the changes:
$ git add -A $ git commit -a -m "Initial commit" $ git push
This will trigger a Packer build, which you can view from the Build history page of the Cloud Console. Once the build is complete, you can see the image is now available in your project.
You learned how to use a module for HashiCorp Terraform to create a network on Google Cloud.
If you are done exploring, please consider deleting your project.
This work is licensed under a Creative Commons Attribution 2.0 Generic License.