Quickstart for Go

This page explains how to use Google Cloud Container Builder to build a Go binary and push the binary to Google Cloud Storage. You'll create a build configuration file, in which you'll add instructions for Container Builder. You'll then use the config file to start the build.

Before you begin

  1. Sign in to your Google account.

    If you don't already have one, sign up for a new account.

  2. In the Cloud Platform Console, go to the Manage resources page and select or create a new project.

    Go to the Manage resources page

  3. Enable billing for your project.

    Enable billing

  4. Enable the Container Builder API.

    Enable the API

  5. Install and initialize the Cloud SDK.

Log in to Google Cloud

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

    gcloud auth login
  2. Configure your project for the gcloud tool, where [PROJECT_ID] is your GCP project ID that you created or selected in the previous section:

    gcloud config set project [PROJECT_ID]

Preparing source files

You'll need some sample source code to build. In this section, you'll create a simple Hello World example in Go.

  1. Create a file named main.go with the following contents:

    package main
    import (
    func main() {
        fmt.Println("Hello, world!")

Creating a Cloud Storage bucket

You'll need a Google Cloud Storage bucket to push the Go binary after you build it. In this section, you'll create the Cloud Storage bucket.

  1. Open a terminal window.

  2. Use the gsutil mb command and a unique name to create a bucket.

    gsutil mb gs://[BUCKET_NAME]/...

    Where [BUCKET_NAME] is your own, globally-unique bucket name.

    If successful, the command returns:

    Creating gs://[BUCKET_NAME]/...

    You've just created a bucket where you can store your stuff!

    If your bucket name is already being used, either by you or someone else, the command returns:

    Creating gs://[BUCKET_NAME]/...
    ServiceException: 409 Bucket [BUCKET_NAME] already exists.

    Try again with a different bucket name.

Configure the build

You specify the actions that you want Container Builder to perform in a build configuration file. The actions are specified as build steps. The build step uses a builder to execute your tasks. Builders are container images with common languages and tools installed in them. Container Builder provides a set of builders that are maintained by Google. In this section, you will use these builders to build the Go binary and push it to Google Cloud Storage.

  • In the same directory that contains main.go, create a file named cloudbuild.yaml with the following contents. This file is your build config file. At build time, Container Builder automatically replaces $PROJECT_ID with your project ID.

    - name: 'gcr.io/cloud-builders/go'
      args: ['install', '.']
      env: ['PROJECT_ROOT=hello']
    - name: 'gcr.io/cloud-builders/go'
      args: ['build', 'hello']
      env: ['PROJECT_ROOT=hello']
    - name: 'gcr.io/cloud-builders/gsutil'
      args: ['-m', 'cp', 'hello', 'gs://[BUCKET_NAME]/']

    The config file above contains three build steps. The name of the build steps refers to the builder that is used to execute your task. The args field takes a list of arguments and passes them to the builder. The env field takes the environment variables to be used when running a step; in the above example, it specifies the root project. In this config file:

    • the first build step calls the go builder to install dependencies.
    • the second build step calls the go builder to build the binary.
    • the third build step calls the gsutil builder to copy the binary to the Cloud Storage bucket.

Start the build

  • Start the build by running the gcloud container builds submit command:

    gcloud container builds submit --config cloudbuild.yaml .

    When the build is complete, you will see an output similar to the following:

    ID                                    CREATE_TIME                DURATION  SOURCE                                                                                  IMAGES  STATUS
    $BUILD_ID                             2018-01-25T15:36:40+00:00  10S       gs://[PROJECT_ID]_cloudbuild/source/1516894599.15-25da391996554cdf81fb7786f914d7f1.tgz  -       SUCCESS

    You've just built the Go binary using the build config file and pushed the binary to the Cloud Storage bucket.

View the build details

  1. Open the Container Registry page in the Google Cloud Platform Console.

    Open the Container Registry page

  2. Select your project and click Open.

  3. In the left nav, click Build history.

    You will see a page similar to the following screenshot:

    Screenshot of the build history page

  4. Click on a build.

    You will see the Build details page, similar to the following screenshot:

    Screenshot of the build details page

Clean up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:

  1. Open a terminal window (if not already open).

  2. Use the gsutil rm command with the -r flag to delete the bucket and anything inside of it, replacing [BUCKET_NAME] with your bucket name:

    gsutil rm -r gs://[BUCKET_NAME]

    If successful, the command returns a message similar to:

    Removing gs://[BUCKET_NAME]/just-a-folder/cloud-storage.logo.png#1456530077282000...
    Removing gs://[BUCKET_NAME]/...

    The artifacts that you created as part of this quickstart are deleted from your project.

That's it, you completed this quickstart!

What's next

Send feedback about...

Cloud Container Builder