Quickstart for Go

This page explains how to use Cloud Build 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 Cloud Build. 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 Console, on the project selector page, select or create a Cloud project.

    Go to the project selector page

  3. Make sure that billing is enabled for your Google Cloud project. Learn how to confirm billing is enabled for your project.

  4. Enable the Cloud Build API.

    Enable the API

  5. Install and initialize the Cloud SDK.
  6. Install Go.

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 Cloud 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 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 Cloud Build to perform in a build configuration file. The actions are specified as build steps. In this section, the build step uses the golang image from Docker Hub to build the Go binary using Go modules and store it in Cloud Storage.

If you are not using Go modules, run the following command to specify a GOPATH to enable the use of Go modules. In this example, the specified GOPATH is github.com/foo/bar.

go mod init github/foo/bar

After running the command, you will have a go.mod file in your directory.

In the same directory that contains main.go, create a build config file named cloudbuild.yaml with the contents below. This example builds and stores the bar binary initialized above:

- name: golang
  args: ['go', 'build', '.']
    location: 'gs://[BUCKET_NAME]/'
    paths: ['bar']

In this build config file:

  • The build step builds the Go module.
  • The artifacts field is used to store the binary to a Cloud Storage bucket.

Start the build

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

    gcloud 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 stored the binary in the Cloud Storage bucket.

View the build details

  1. Open the Cloud Build page in the Google Cloud Console.

    Open the Cloud Build page

  2. Select your project and click Open.

    You will see the Build history page:

    Screenshot of the build history page

  3. 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 account for the resources used in this quickstart, follow these steps.

  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