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 Google 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.

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 Google 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. The build step uses a builder to execute your tasks. Builders are container images with common languages and tools installed in them. Cloud Build provides a set of builders that are maintained by Google. In this section, you will use these builders to build the Go binary and store it in 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, Cloud Build 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']
        location: 'gs://[BUCKET_NAME]/'
        paths: ['hello']

    The config file above contains two 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 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

Var denne siden nyttig? Si fra hva du synes:

Send tilbakemelding om ...

Cloud Build Documentation