<p>I have a Kubernetes deployment on GCE, which I'd like to get automatically updated based on new images being created in Google Container Registry (ideally via a Build Trigger).  Is there a way to do that?</p>

<p>Thanks in advance.</p>

<p>-Mark</p>

<p>I was able to do this using GCR and Cloud Builder with a <code>cloudbuild.yaml</code> file like the below.  For it to work, the service account with a name like <code>xyz@cloudbuild.gserviceaccount.com</code> had to have the IAM permissions assigned by clicking Project -> Editor.  This is required so that the Cloud Build service can make SSH keys and add them to your GCE metadata to allow Cloud Builder to SSH in.   This SSHing is the big work-around to effectively run any command on your GCE VM server.  </p>

<pre><code>steps:
# Build Docker image: docker build -f Dockerfile -t gcr.io/my-project/my-image:latest .
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-f', 'Dockerfile', '-t', 'gcr.io/my-project/my-image:latest', '.']

# Push to GCR: gcloud docker -- push gcr.io/my-project/my-image:latest
- name: 'gcr.io/cloud-builders/docker'
  args: ['push', 'gcr.io/my-project/my-image:latest']

# Connect to GCE server and pull new image
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['compute', 'ssh', '$_SERVER', '--zone', '$_ZONE', '--command', 'gcloud docker -- pull gcr.io/my-project/my-image:latest']

# Connect to server and stop current container
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['compute', 'ssh', '$_SERVER', '--zone', '$_ZONE',  '--command', 'docker stop my-image']

# Connect to server and stop current container
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['compute', 'ssh', '$_SERVER', '--zone', '$_ZONE',  '--command', 'docker rm my-image']

  # Connect to server and start new container
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['compute', 'ssh', '$_SERVER', '--zone', '$_ZONE',  '--command', 'docker run  --restart always --name my-image -d -p 443:443  --log-driver=gcplogs  gcr.io/my-project/my-image:latest']


substitutions:
  _SERVER: 'my-gce-vm-server'
  _ZONE: 'us-east1-c'
</code></pre>

<h2>Bonus Pro Tips:</h2>

<ol>
<li>the <code>substitutions</code> are nice in case you prop up a new server some day and want to use it instead</li>
<li>using <code>--log-driver=gcplogs</code> makes your Docker logs show up in your Google Cloud Console's Stackdriver Logging in the appropriate "GCE VM Instance".  Just be sure to have "All logs" and "Any Log Level" selected since Docker logs have no log level and are not <code>syslog</code> or <code>activity_log</code> messages </li>
</ol>
