<p>I am trying to do the CI/CD with GCP cloudbuild. </p>

<ol>
<li>I have a k8s cluster ready in GCP. check the deployment manifest bellow.</li>
<li>I have a cloudbuild.yaml ready to build new image and push it to registry and command to change the deployment image. check the cloudbuild yaml bellow.</li>
</ol>

<p>Previously, I used to push the image using the <strong>TAG latest</strong> for the docker image and use the same tag in deployment but it didn't pull the latest image so  Now I have changed it to use the <strong>TAG $COMMIT_SHA</strong>. Now, I am not able to figure out the way to pass the new image with TAG based on commit_sha to the deployment.</p>

<p><strong><em>nginx-deployment.yaml</em></strong></p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mynginx
spec:
  replicas: 3
  minReadySeconds: 50
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  selector:
    matchLabels:
      app: nginx

  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - image: gcr.io/foods-io/cloudbuildtest-image:latest
          name: nginx
          ports:
            - containerPort: 80
</code></pre>

<p><strong><em>cloudbuild.yaml</em></strong></p>

<pre><code>steps:
  #step1      
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/cloudbuildtest-image:$COMMIT_SHA', '.' ]
  #step 2
- name: 'gcr.io/cloud-builders/docker'
  args: ['push', 'gcr.io/$PROJECT_ID/cloudbuildtest-image:$COMMIT_SHA']
  #STEP-3
- name: 'gcr.io/cloud-builders/kubectl'
  args: ['set', 'image', 'deployment/mynginx', 'nginx=gcr.io/foods-io/cloudbuildtest-image:$COMMIT_SHA']
  env:
  - 'CLOUDSDK_COMPUTE_ZONE=us-central1-a'
  - 'CLOUDSDK_CONTAINER_CLUSTER=cloudbuild-test'
images:
- 'gcr.io/$PROJECT_ID/cloudbuildtest-image'
</code></pre>

<blockquote>
  <p>Note: I repeat previously I was using the latest tag to the image and
  as is the same in deployment I expected to pull the new image with my
  3rd steps in cloudbuild but that didn't so I made the above changes in
  TAG but now wondering how do I make changes to deployment manifest. Is
  using the <strong>helm</strong> only solution here?</p>
</blockquote>

Tara Prasad Gurung
<p>You need a step to replace the tag in your deployment.yaml, one way to do it is to use an environment variable and use <code>envsubst</code> to replace it.</p>

<p>Change deployment.yaml:</p>

<pre><code>    - image: gcr.io/foods-io/cloudbuildtest-image:$COMMIT_SHA
</code></pre>

<p>Use some <code>bash</code> script to replace the variable (using the <code>ubuntu</code> <a href="https://cloud.google.com/cloud-build/docs/create-custom-build-steps" rel="nofollow noreferrer">step</a> for example):</p>

<pre class="lang-sh prettyprint-override"><code>envsubst '$COMMIT_SHA' &lt; deployment.yaml &gt; nginx-deployment.yaml
</code></pre>

<p>Alternative using <code>sed</code>:</p>

<pre class="lang-sh prettyprint-override"><code>sed -e 's/$COMMIT_SHA/'"$COMMIT_SHA"'/g' deployment.yaml &gt; /workspace/nginx-deployment.yaml
</code></pre>
