Starting Builds Manually

This page describes how to start a build in Container Builder manually using the gcloud command-line tool and the Container Builder API.

Before you begin

  • If you want to use the command-line examples in this guide, install the gcloud command-line tool.
  • If you want to use the API examples in this guide, enable the Container Builder API.
  • To build using the Container Builder build config, create a build config file.
  • To build using a Dockerfile, have a Dockerfile handy.
  • If your build requires source code, have it handy.

Running builds

console

You cannot start a new build using Google Cloud Platform Console, however you can retry a previous build.

To rebuild:

  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.

  4. In the Build history page, click on a build that you wish to rebuild.

  5. Click Retry.

gcloud

Using a Dockerfile:

Your Dockerfile contains all information needed to build a Docker image using Container Builder.

To run a build request using your Dockerfile, run the following command from the directory containing your application code, Dockerfile, and any other assets:

gcloud container builds submit --tag gcr.io/[PROJECT_ID]/[IMAGE_NAME] .

Where:

  • [PROJECT_ID] is the name of your GCP project.
  • [IMAGE_NAME] is the image to be built.
  • . specifies that the source code is in the current working directory.

The full name of the image to be built is gcr.io/[PROJECT_ID]/[IMAGE_NAME]. Images pushed to Container Registry use the registry name format.

The gcloud container builds submit command:

  • compresses your application code, Dockerfile, and any other assets in the current directory as indicated by .;
  • uploads the files to a Cloud Storage bucket;
  • initiates a build using the uploaded files as input;
  • tags the image using the provided name; and
  • pushes the built image to Container Registry.

As the build progresses, its output is displayed in your shell or terminal window. When the build is complete, you should see output similar to the following:

    DONE
    ---------------------------------------------------------------------------------
    ID                                    CREATE_TIME                DURATION STATUS
    $BUILD_ID                             2016-10-28T15:21:18+00:00  12S      SUCCESS

where $BUILD_ID is your build's unique identifier.

Using the Container Builder build config file:

To submit a build using the build config, run the following command:

    gcloud container builds submit --config [BUILD_CONFIG] [SOURCE_CODE]

where:

  • [BUILD_CONFIG] is the path to the build config file
  • [SOURCE_CODE] is the path or URL source code

For example, the following command submits the cloudbuild.yaml build request using archived source code stored in a Cloud Storage bucket.

    gcloud container builds submit --config cloudbuild.yaml \
        gs://container-builder-examples/node-docker-example.tar.gz

You can use . to specify that the source code is in the current working directory:

    gcloud container builds submit --config cloudbuild.yaml .

gcloudignore: When including source code for the build, the above command uploads all of the files in the specified directory to Google Cloud Platform to build. If want to exclude certain files in the directory, you can include a file named .gcloudignore in the top-level upload directory; the files that it specifies will be ignored. If no .gcloudignore file is present in the top-level upload directory, but a .gitignore file is, the gcloud tool will generate a Git-compatible .gcloudignore file that respects your .gitignore-ed files. For more information, see the gcloudignore documentation.

If you do not have source code to pass in to your build, use the --no-source flag:

    gcloud container builds submit --config [BUILD_CONFIG] --no-source

API

To submit the build request using curl:

  1. Create a file named request.json with the following contents:

    {
        "source": {
            "storageSource": {
                "bucket": "container-builder-examples",
                "object": "node-docker-example.tar.gz"
            }
        },
        "steps": [{
            "name": "gcr.io/cloud-builders/docker",
            "args": [
                "build",
                "-t",
                "gcr.io/$PROJECT_ID/my-image",
                "."
            ]
        }],
        "images": [
            "gcr.io/$PROJECT_ID/my-image"
        ]
    }
    

Note: At build time, Container Builder automatically replaces $PROJECT_ID with your project ID.

In this build request, Container Builder calls the docker build step with the arguments build -t gcr.io/$PROJECT_ID/cb-demo-img ..

The full name of the image to be built is gcr.io/$PROJECT_ID/cb-demo-img. Images pushed to Container Registry use the registry name format.

The code source for the build is in a compressed tar archive, node-docker-example.tar.gz. The file is stored in a Cloud Storage bucket named container-builder-examples.

  1. Run the following command:

    curl -X POST -T request.json -H "Authorization: Bearer $(gcloud config config-helper \
        --format='value(credential.access_token)')" \
        https://cloudbuild.googleapis.com/v1/projects/[PROJECT_ID]/builds
    

where [PROJECT-ID] is your GCP project ID.

In this command, curl sends request.json in a POST call to the builds endpoint for the projects.builds.create API method.

The command displays details about your build in your shell or terminal window. The output is a JSON response, and appears similar to the following:

    {
        "name": "operations/build/$PROJECT-ID/NmZhZW...",
        "metadata": {
            "@type": "type.googleapis.com/google.devtools.cloudbuild.v1.BuildOperationMetadata",
            "build": {
                "id": $BUILD-ID,
                "status": "QUEUED",
                "source": {
                    "storageSource": {
                        "bucket": "container-builder-examples",
                        "object": "node-docker-example.tar.gz"
                    }
                },
                "createTime": "2017-05-12T18:58:07.341526Z",
                "steps": [
                {
                    "name": "gcr.io/cloud-builders/docker",
                    "args": [
                        "build",
                        "-t",
                        "gcr.io/$PROJECT-ID/cb-demo-img",
                        "."
                    ]
                }
                ],
                "timeout": "600s",
                "images": [
                    "gcr.io/$PROJECT-ID/cb-demo-img"
                ],
                "projectId": $PROJECT-ID,
                "logsBucket": "gs://...",
                "sourceProvenance": {
                    "resolvedStorageSource": {
                        "bucket": "container-builder-examples",
                        "object": "node-docker-example.tar.gz",
                        "generation": "..."
                    }
                },
                "logUrl": "https://console.cloud.google.com/gcr/builds/...?project=$PROJECT-ID"
            }
        }
    }

The JSON response is modeled using the Operation resource in the Container Builder API. The metadata field is modeled using the Build resource. The QUEUED status indicates that the build is awaiting execution.

What's next

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Container Builder