Triggering a build for a specific git commit via gcloud commandline tool

2/24/2018

All the examples that I've come across have been of the following format:

gcloud container builds submit --config cloudbuild.yaml .

The man-page says the following:

 [SOURCE]
    The source directory on local disk or tarball in Google Cloud Storage
    or disk to use for this build. If source is a local directory this
    command skips files specified in the .gcloudignore file (see $ gcloud
    topic gcloudignore for more information).

Now, the source-directory on my local disk is very large and a lot of time is being spent in transferring the source code from my local machine to the Google build servers/cloud. Is either of the following possible? How?

  • Give a git/github URL instead of local source-directory
  • My git-repo is incidentally being mirrored in Google Source Repository as well, because I have setup build triggers for my repo. Can I give a URL to the repo being mirrored by Google?
-- Saurabh Nanda
google-cloud-platform
google-container-registry
google-kubernetes-engine

2 Answers

2/25/2018

Unfortunately there isn't great support for this today in gcloud. You can accomplish this a few other ways though:

  1. Use curl or the client library of your choice to send an API request to request a build that specifies a RepoSource. For example:

    { "source": { "repoSource": { "repoName": "my-repo", "commitSha": "deadbeef" } }, "steps": [...] }

  2. In your local environment, fetch the commit and build it using gcloud:

    git checkout && gcloud container builds submit . --config=cloudbuild.yaml

  3. Create a trigger that automatically executes your build, then issue an API request to run the trigger manually, on the specific commit you want, again using curl or a client library.

-- Jason Hall
Source: StackOverflow

2/27/2018

If you are building Docker images you can use a cached image present in your container registry to build upon. If you only have made changes to the last layers of the build you can actually avoid transferring most of the data and mostly build only the changes.

As in the linked example, you can add a --cache-from to the .yaml file selecting the image on your Google container registry on to build on:

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: ['pull', 'gcr.io/$PROJECT_ID/latest-image']
- name: 'gcr.io/cloud-builders/docker'
  args: [
            'build',
            '--cache-from',
            'gcr.io/$PROJECT_ID/latest-image',
            '-t', 'gcr.io/$PROJECT_ID/latest-image',
            '.'
        ]
images: ['gcr.io/$PROJECT_ID/latest-image']

Then, the command to build:

gcloud container builds submit --config cloudbuild.yaml .

This should avoid you quite a bit of transfer time.

-- Jordi Miralles
Source: StackOverflow