Building Go applications

This page explains how to use Cloud Build to build a Go binary and push the binary to Cloud Storage. If you're new to Cloud Build, read the quickstarts and the build configuration overview first.

Cloud Build enables you to use any publicly available container image to execute your tasks. You can use the public golang image from DockerHub in your config file to build your Go applications.

Before you begin

The instructions on this page assume that you are familiar with Go. In addition:

  • You'll need the Go application source code.
  • You'll need a Cloud Storage bucket to push the built Go binary. For instructions on creating a bucket, see Creating storage buckets.
  • To run the gcloud commands in this page, install the gcloud command-line tool.

Building using Go modules

Go 1.11 includes preliminary support for versioned modules. The public golang image from Docker Hub supports building using Go modules. Using this image as a build step in your Cloud Build config file enables you to invoke go commands within the image. Arguments passed to this build step are passed to the golang tool directly, allowing you to run any go command in this image.

To build your Go application:

  1. [OPTIONAL, if you're not using go modules] Initialize go modules: In the same directory that contains the application source code, run the following command to specify a GOPATH to enable the use of Go modules:

    go mod init [GOPATH]
    
  2. Create a Cloud Build config file named cloudbuild.yaml or cloudbuild.json.

  3. Build the module: In the config file, add a build step to invoke the go build command:

    YAML

    # Build the module.
    - name: golang
      args: ['go', 'build', '.']
    

    JSON

    {
       "steps": [
        {
           "name": "golang",
           "args": [
               "go",
               "build",
               "."
            ]
           }
      ]
    }
    
  4. Store the build artifacts: Add the artifacts field to store the binary in a Cloud Storage bucket.

    YAML

    # Build the module.
    - name: golang
      args: ['go', 'build', '.']
    artifacts:
      objects:
        location: '[STORAGE_LOCATION]'
        paths: ['[ARTIFACT_PATH]']
    

    JSON

    {
       "steps": [
        {
           "name": "golang",
           "args": [
               "go",
               "build",
               "."
            ]
        }
      ]
      "artifacts": {
        "objects": {
            "location": [
                "[STORAGE_LOCATION]"
            ],
            "paths":
            [
                "[ARTIFACT_PATH]"
    
            ]
        }
      }
    }
    

    Where,

    • [STORAGE_LOCATION]: A Cloud Storage bucket or a folder within the bucket where Cloud Build must store the artifact, such as gs://mybucket or gs://mybucket/some/folder.
    • [ARTIFACT_PATH]: Path to one or more artifacts.
  5. Start the build using the build config file:

    gcloud builds submit --config [CONFIG_FILE_PATH] [SOURCE_DIRECTORY]
    

    Where:

    • [CONFIG_FILE_PATH] is the path to the build config file.
    • [SOURCE_DIRECTORY] is the path or URL to the source code.

    If you don't specify a [CONFIG_FILE_PATH] and [SOURCE_DIRECTORY] in the gcloud builds submit command, Cloud Build assumes that the config file and the source code are in the current working directory.

What's next