<p>I am in the learning phase of Kubernetes and want to set up the CI/Cd pipeline for my project. I am using google cloud and have the following elements are ready</p>
<ol>
<li>3 Node cluster is deployed on google cloud</li>
<li>Github has been integrated with google cloud build to trigger the build.</li>
<li>I am using <code>helm</code> to maintain my K8s templates.</li>
<li><code>cloudbuilld.yaml</code> is developed to compile the docker image and push it to google container registry.</li>
</ol>
<p>I am stuck at - Once my cloudbuild.yaml is done with building the docker image and pushed it to the registry, how do I use helm to upgrade the chart?</p>
<p>Here is my sample <code>cloudbuild.yaml</code></p>
<pre><code>steps:
  - name: 'gcr.io/cloud-builders/docker'
    args: [&quot;build&quot;, &quot;-t&quot;, &quot;gcr.io/kubernetes-amit-test/github.com/0xvoila/apache/phoenix:$SHORT_SHA&quot;, &quot;.&quot;]

  - name: &quot;gcr.io/cloud-builders/docker&quot;
    args: [&quot;push&quot;, &quot;gcr.io/kubernetes-amit-test/github.com/0xvoila/apache/phoenix:$SHORT_SHA&quot;]

  - name: &quot;alpine/helm:latest&quot;.   --- It is not working 
    args: [&quot;helm&quot;,&quot;upgrade&quot;,&quot;mychart&quot;,&quot;image&quot;, &quot;gcr.io/kubernetes-amit-test/github.com/0xvoila/apache/phoenix:$SHORT_SHA&quot;]

</code></pre>
<p>My Question is</p>
<ol>
<li>How can I use helm to upgrade the latest charts.</li>
<li>As I am new to Kubernetes, it is even the best practice for K8s deployment? Do people even use helm?</li>
</ol>

<blockquote>
<p>How can I use helm to upgrade the latest charts.</p>
</blockquote>
<p>There is already default helm exist : <strong>gcr.io/$PROJECT_ID/cloud-builders-helm</strong></p>
<pre><code>- name: 'gcr.io/$PROJECT_ID/cloud-builders-helm'
  args: ['upgrade', '--install', 'filebeat', '--namespace', 'filebeat', 'stable/filebeat']
</code></pre>
<p>For managing chart version you should check the : <a href="https://cloud.google.com/artifact-registry/docs/helm/manage-charts" rel="noreferrer">https://cloud.google.com/artifact-registry/docs/helm/manage-charts</a></p>
<p>Helm cloud builder <a href="https://github.com/GoogleCloudPlatform/cloud-builders-community/tree/master/helm" rel="noreferrer">Github</a></p>
<blockquote>
<p>As I am new to Kubernetes, it is even the best practice for K8s
deployment? Do people even use helm?</p>
</blockquote>
<p>Helm is the best way to manage it instead of using any other.</p>
<p>i would suggest checking out the <code>helm atomic</code></p>
<pre><code>helm upgrade --install --atomic
</code></pre>
<p>which will also auto rollback deployment if it's failing in K8s.</p>
<blockquote>
<p>--atomic                       if set, upgrade process rolls back changes made in case of failed upgrade. The --wait flag will be set
automatically if --atomic is used</p>
</blockquote>
<p><a href="https://helm.sh/docs/helm/helm_upgrade/" rel="noreferrer">Read more</a></p>
<p><strong>Extra :</strong></p>
<p>Instead of fixing the GCR name, you can also use variables this template will work across the <strong>branches</strong> of across repo also.</p>
<pre><code>- id: 'build test core image'
  name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/$REPO_NAME/$BRANCH_NAME:$SHORT_SHA', '.']
- id: 'push test core image'
  name: 'gcr.io/cloud-builders/docker'
  args: ['push', 'gcr.io/$PROJECT_ID/$REPO_NAME/$BRANCH_NAME:$SHORT_SHA']
</code></pre>
<p><strong>Update :</strong></p>
<p>Adding GKE cluster details to Cloud build</p>
<pre><code>- name: 'gcr.io/cloud-builders/kubectl'
  args: ['apply', '-f', 'deployment.yaml']
  env:
  - 'CLOUDSDK_COMPUTE_ZONE=${_CLOUDSDK_COMPUTE_ZONE}'
  - 'CLOUDSDK_CONTAINER_CLUSTER=${_CLOUDSDK_CONTAINER_CLUSTER}'
</code></pre>
<p>i am using the <strong>kubectl</strong> <strong>apply</strong> but you can add these <strong>environment</strong> <strong>variables</strong> to your <strong>helm</strong> step.</p>
<p><a href="https://github.com/harsh4870/basic-ci-cd-cloudbuild/blob/main/cloudbuild-deployment.yaml" rel="noreferrer">File location</a></p>
<p><strong>Full file</strong></p>
<pre><code>substitutions:
    _CLOUDSDK_COMPUTE_ZONE: us-central1-c  # default value
    _CLOUDSDK_CONTAINER_CLUSTER: standard-cluster-1      # default value
steps:
- id: 'set test core image in yamls'
  name: 'ubuntu'
  args: ['bash','-c','sed -i &quot;s,TEST_IMAGE_NAME,gcr.io/$PROJECT_ID/$REPO_NAME/$BRANCH_NAME:$SHORT_SHA,&quot; deployment.yaml']
- name: 'gcr.io/cloud-builders/kubectl'
  args: ['apply', '-f', 'deployment.yaml']
  env:
  - 'CLOUDSDK_COMPUTE_ZONE=${_CLOUDSDK_COMPUTE_ZONE}'
  - 'CLOUDSDK_CONTAINER_CLUSTER=${_CLOUDSDK_CONTAINER_CLUSTER}'
</code></pre>
