<p>I am struggling trying to replace an existing container with a container from my container-register from Google Cloud Platform.</p>

<p>This is my cloudbuild.yaml file.</p>

<p>steps:</p>

<pre><code>  # This steps clone the repository into GCP
  - name: gcr.io/cloud-builders/git
    args: ['clone', 'https:///user/:password@github.com/PatrickVibild/scrappercontroller']

  # This step runs the unit tests on the src
  - name: 'docker.io/library/python:3.7'
    id: Test
    entrypoint: /bin/sh
    args:
      - -c
      - 'pip install -r requirements.txt &amp;&amp; python -m pytest src/tests/**'

  #This step creates a container and leave it on CloudBuilds repository.
  - name: 'gcr.io/cloud-builders/docker'
    args: ['build', '-t', 'gcr.io/abiding-robot-255320/scrappercontroller', '.']

  #Adds the container to Google container registry as an artefact
  - name: 'gcr.io/cloud-builders/docker'
    args: ['push', 'gcr.io/abiding-robot-255320/scrappercontroller']

  #Uses the container and replaces the existing one in Kubernetes
  - name: 'gcr.io/cloud-builders/kubectl'
    args: ['set', 'image', 'deployment/scrappercontroller', 'scrappercontroller-sha256=gcr.io/abiding-robot-255320/scrappercontroller:latest']
    env:
      - 'CLOUDSDK_COMPUTE_ZONE=us-central1-a'
      - 'CLOUDSDK_CONTAINER_CLUSTER=scrapper-admin'
</code></pre>

<p>I have no issues building my project and I get green on all the steps, I might be missing in the last step but I cant find a way to replace my container in my cluster with a newer version of my code. </p>

<p>I can create a new workload inside my existing cluster manually using the GUI and selecting a container from my container registry, but from there the step to replace that workload container with my new version from the clouds fails.</p>

<p>It's a common pitfall. According with the <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#updating-a-deployment" rel="nofollow noreferrer">documentation</a>:</p>

<blockquote>
  <p>Note: A Deployment’s rollout is triggered if and only if the Deployment’s Pod template (that is, .spec.template) is changed, for example if the labels or container images of the template are updated. Other updates, such as scaling the Deployment, do not trigger a rollout.</p>
</blockquote>

<p>Your issue come from the tag of your image doesn't change: the <code>:latest</code> is deployed and you ask for deploying <code>:latest</code>. No image name change, no rollout.</p>

<p>For changing this, I propose you to use <a href="https://cloud.google.com/cloud-build/docs/configuring-builds/substitute-variable-values#using_default_substitutions" rel="nofollow noreferrer">substitution variables</a>, especially <code>COMMIT_SHA</code> or <code>SHORT_SHA</code>. You can not this in the documentation:</p>

<blockquote>
  <p>only available for triggered builds</p>
</blockquote>

<p>This means that this variable is only populated when the build is automatically triggered  and not manually. </p>

<p>For manual run, you have to specify your own variable, like this</p>

<pre><code>gcloud builds submit --substitutions=COMMIT_SHA=&lt;what you want&gt;
</code></pre>

<p>And update your build script like this:</p>

<pre><code>  # This steps clone the repository into GCP
  - name: gcr.io/cloud-builders/git
    args: ['clone', 'https:///user/:password@github.com/PatrickVibild/scrappercontroller']

  # This step runs the unit tests on the src
  - name: 'docker.io/library/python:3.7'
    id: Test
    entrypoint: /bin/sh
    args:
      - -c
      - 'pip install -r requirements.txt &amp;&amp; python -m pytest src/tests/**'

  #This step creates a container and leave it on CloudBuilds repository.
  - name: 'gcr.io/cloud-builders/docker'
    args: ['build', '-t', 'gcr.io/abiding-robot-255320/scrappercontroller:$COMMIT_SHA', '.']

  #Adds the container to Google container registry as an artefact
  - name: 'gcr.io/cloud-builders/docker'
    args: ['push', 'gcr.io/abiding-robot-255320/scrappercontroller:$COMMIT_SHA']

  #Uses the container and replaces the existing one in Kubernetes
  - name: 'gcr.io/cloud-builders/kubectl'
    args: ['set', 'image', 'deployment/scrappercontroller', 'scrappercontroller-sha256=gcr.io/abiding-robot-255320/scrappercontroller:COMMIT_SHA']
    env:
      - 'CLOUDSDK_COMPUTE_ZONE=us-central1-a'
      - 'CLOUDSDK_CONTAINER_CLUSTER=scrapper-admin'
</code></pre>

<p>And during the deployment, you should see this line:</p>

<pre><code>Step #2: Running: kubectl set image deployment.apps/test-deploy go111=gcr.io/&lt;projectID&gt;/postip:&lt;what you want&gt;
Step #2: deployment.apps/test-deploy image updated
</code></pre>

<p>If you don't see it, this mean that your rollout has not take into account.</p>
