Quickstart for Packer

This page explains how to use Packer and Cloud Build to create a VM image for use on Google Compute Engine. You'll create a build configuration by defining instructions for Packer and Cloud Build. You'll then use the config to start a build.

Before you begin

  1. 登入您的 Google 帳戶。

    如果您沒有帳戶,請申請新帳戶

  2. 在 GCP 主控台中,前往「Manage resources」(管理資源) 頁面,選取或建立專案。

    前往「Manage resources」(管理資源) 頁面

  3. 請確認您已啟用 Google Cloud Platform 專案的計費功能。

    瞭解如何啟用計費功能

  4. 啟用Cloud Build API。

    啟用 API

  5. 安裝並初始化 Cloud SDK

Log in to Google Cloud Platform

  1. Authorize the gcloud command-line tool to access your project:

    gcloud auth login
    
  2. Configure your project for the gcloud tool:

    gcloud config set project [PROJECT_ID]
    

    [PROJECT_ID] is your GCP project ID that you created or selected in the previous section.

Configure your project

  1. Complete one of the following actions:

    • If you have not created a configuration before, see gcloud topic configurations.

    • If you have already created a configuration, activate it:

      gcloud config configurations activate [CONFIGURATION NAME]

  2. Set the project variable:

    PROJECT=$(gcloud config get-value project)
    

    After the project variable is set, the command returns the following output:

    Your active configuration is: [<your-config-name>]
    
  3. Ensure the project variable is set correctly:

    echo $PROJECT
    

Enable services

Enable the required Cloud API services for your project:

gcloud services enable sourcerepo.googleapis.com
gcloud services enable cloudapis.googleapis.com
gcloud services enable compute.googleapis.com
gcloud services enable servicemanagement.googleapis.com
gcloud services enable storage-api.googleapis.com
gcloud services enable cloudbuild.googleapis.com

Set permissions

  1. Locate the Cloud Build service account:

    CLOUD_BUILD_ACCOUNT=$(gcloud projects get-iam-policy $PROJECT --filter="(bindings.role:roles/cloudbuild.builds.builder)"  --flatten="bindings[].members" --format="value(bindings.members[])")
    
  2. Add the editor role to the account:

    gcloud projects add-iam-policy-binding $PROJECT \
      --member $CLOUD_BUILD_ACCOUNT \
      --role roles/editor
    

    You'll see the updated IAM policy report for your project.

Create a Packer builder image

Clone the cloud-builders-community repository to build an image using the Packer image builder. Then, push the image to the container repository:

  1. Save your working directory:

    project_dir=$(pwd)
    
  2. Navigate to your temporary directory:

    cd /tmp
    
  3. Clone the cloud-builders-community repository:

    git clone https://github.com/GoogleCloudPlatform/cloud-builders-community.git
    
  4. Navigate to the Packer builder image:

    cd cloud-builders-community/packer
    
  5. Submit the builder to your project:

    gcloud builds submit .
    

    Once the build is complete, you'll see an output similar to the following:

    DONE
    --------------------------------------------------------------------------------
    
    ID                                    CREATE_TIME                DURATION  SOURCE                                                                                         IMAGES                                       STATUS
    e3d89cdd-924c-4539-bc9d-2c87e0e910e5  2019-06-14T19:06:44+00:00  1M11S     gs://[PROJECT_ID]_cloudbuild/source/1560539203.19-1d492a75515340f1bc92c0a8a6937308.tgz  gcr.io/[PROJECT_ID]/packer (+2 more)  SUCCESS
    

    You can also view the build on the Cloud Build page in the Google Cloud Platform Console.

    Open the Cloud Build page

    You'll see the Build history page with details of your build:

    Screenshot of the Packer build image

  6. Remove the repository from your temporary directory:

    rm -rf /tmp/cloud-builders-community
    
  7. Navigate back to $project_dir:

    cd $project_dir
    

Prepare source files

You'll need some sample source code to build. In this section, you'll create a simple Hello World example using the Packer builder.

  1. Create a directory called hello-world and navigate to it:

    mkdir hello-world
    cd hello-world
    
  2. Create a file named cloudbuild.yaml with the following contents:

    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
    
  3. Create a file named install-website.sh with the following contents:

    #!/bin/sh
    sudo yum install -y epel-release
    sudo yum install -y nginx
    sudo chkconfig nginx on
    LOCATION_OF_INDEX=/usr/share/nginx/html/index.html
    sudo bash -c "cat <<A_VERY_SPECIAL_MESSAGE>$LOCATION_OF_INDEX
    <html>
    <h2> Hello World! </h2>
    </html>
    
    A_VERY_SPECIAL_MESSAGE
    "
    
  4. Create a file named packer.json with the following contents:

    {
        "variables": {
            "image_name": "",
            "image_family": "",
            "image_zone": "",
            "project_id": ""
        },
    
        "builders": [
            {
                "image_name": "{{ user `image_name` }}",
                "type": "googlecompute",
                "project_id": "{{ user `project_id`}}",
                "source_image_family": "centos-7",
                "image_family": "{{ user `image_family` }}",
                "ssh_username": "packer",
                "zone": "{{ user `image_zone` }}",
                "startup_script_file": "install-website.sh",
                "scopes": [
                    "https://www.googleapis.com/auth/userinfo.email",
                    "https://www.googleapis.com/auth/compute",
                    "https://www.googleapis.com/auth/devstorage.full_control"
                ]
            }
        ]
    }
    

    You now have the following files in your hello-world directory: cloudbuild.yaml, install-website.sh, and packer.json.

  5. Verify the build:

    gcloud builds submit --substitutions=_IMAGE_NAME="hello-v1",_IMAGE_FAMILY="hello-world",_IMAGE_ZONE="us-east1-b"
    

    After the build is complete, you'll see an output similar to the following:

    DONE
    --------------------------------------------------------------------------------
    
    ID                                    CREATE_TIME                DURATION  SOURCE                                                                                         IMAGES  STATUS
    9a279714-e908-4033-9b46-25f8753e2cdc  2019-06-14T19:43:49+00:00  5M35S     gs://[PROJECT_ID]_cloudbuild/source/1560541428.62-64d0dc6fb01648d1be6fae9e045f6093.tgz  -       SUCCESS
    

Modify the image

  1. Open install-website.sh.

  2. In the <h2> tag, change "Hello World" to "Hello Universe":

    #!/bin/sh
    sudo yum install -y epel-release
    sudo yum install -y nginx
    sudo chkconfig nginx on
    LOCATION_OF_INDEX=/usr/share/nginx/html/index.html
    sudo bash -c "cat <<A_VERY_SPECIAL_MESSAGE>$LOCATION_OF_INDEX
    <html>
    <h2> Hello Universe! </h2>
    </html>
    
    A_VERY_SPECIAL_MESSAGE
    "
    
  3. Verify the build with the new image name:

    gcloud builds submit --substitutions=_IMAGE_NAME="hello-v2",_IMAGE_FAMILY="hello-world",_IMAGE_ZONE="us-east1-b"
    

    After the build is complete, you'll see an output similar to the following:

    DONE
    --------------------------------------------------------------------------------
    
    ID                                    CREATE_TIME                DURATION  SOURCE                                                                                         IMAGES  STATUS
    7787c289-cb07-4e14-91f0-c74055b527a0  2019-06-14T19:57:11+00:00  4M56S     gs://[PROJECT_ID]_cloudbuild/source/1560542231.04-89ff301824a348be9c524a90124488ff.tgz  -       SUCCESS
    
  4. Open the Images page in the Google Cloud Platform Console to see both images.

    Go to the Images page

    Screenshot of the Packer image family

Clean up

To avoid incurring charges to your GCP account for the resources used in this quickstart:

  1. Delete the Packer image:

    gcloud container images delete --quiet gcr.io/$PROJECT/packer  --force-delete-tags
    
  2. Delete the hello-world images:

    gcloud compute images delete --quiet hello-v1 hello-v2
    

What's next

本頁內容對您是否有任何幫助?請提供意見:

傳送您對下列選項的寶貴意見...

這個網頁
Cloud Build