Cloudbuild does not trigger new pod deployment, No resources found in namespace GKE

8/24/2020

I've been playing around with GCP Triggers to deploy a new pod every time a push is made to a Github repo. I've got everything set up and the docker image is pushed to the GCP Container Registry and the trigger completes successfully without any errors. I use the $SHORT_SHA tags that are generated by the build pipeline as my tags. But, however, the new pod deployment does not work. I am not sure what the issue is because I am modifying the codebase as well with every new push just to test the deployment. I've followed couple of tutorials by Google on Triggers, but I am unable to understand what exactly the issue is and why does the newly pushed image does not get deployed.

cloudbuild.yaml

  - name: maven:3-jdk-8
    id: Maven Compile
    entrypoint: mvn
    args: ["package", "-Dmaven.test.skip=true"]
  - name: 'gcr.io/cloud-builders/docker'
    id: Build
    args:
    - 'build'
    - '-t'
    - 'us.gcr.io/$PROJECT_ID/<image_name>:$SHORT_SHA'
    - '.'
   
  - name: 'gcr.io/cloud-builders/docker'
    id: Push
    args:
    - 'push'
    - 'us.gcr.io/$PROJECT_ID/<image_name>:$SHORT_SHA'
  - name: 'gcr.io/cloud-builders/gcloud'
    id: Generate manifest
    entrypoint: /bin/sh
    args:
    - '-c'
    - |
       sed "s/GOOGLE_CLOUD_PROJECT/$SHORT_SHA/g" kubernetes.yaml
  - name: "gcr.io/cloud-builders/gke-deploy"
    args:
    - run
    - --filename=kubernetes.yaml
    - --image=us.gcr.io/$PROJECT_ID/<image_name>:$SHORT_SHA
    - --location=us-central1-c
    - --cluster=cluster-1

kubernetes.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: <deployment_name>
spec:
  replicas: 1
  selector:
    matchLabels:
      app: <container_label>
  template:
    metadata:
      labels:
        app: <container_label>
    spec:
      nodeSelector:
        cloud.google.com/gke-nodepool: default-pool
      containers:
      - name: <container_name>
        image: us.gcr.io/<project_id>/<image_name>:GOOGLE_CLOUD_PROJECT
        ports:
        - containerPort: 8080
apiVersion: v1
kind: Service
metadata:
  name: <service-name>
spec:
  selector:
    app: <selector_name>
  ports:
  - port: 80
    targetPort: 8080
  type: LoadBalancer
-- user782400
build-triggers
continuous-integration
docker
google-cloud-platform
kubernetes

1 Answer

1/29/2021

I would recommend few changes to work your cloud build to deploy an application in the EKS cluster.

cloudbuild.yaml

  1. In build and push stage change the arg into gcr.io/$PROJECT_ID/<image_name>:$SHORT_SHA use the gcr.io/$PROJECT_ID/sample-image:latest.
  2. Generate a manifest stage - you can skip/delete the stage.
  3. gke-deploy stage - remove the image step.

kubernetes.yaml

  1. In the spec - you can mention the image as gcr.io/$PROJECT_ID/sample-image:latest it will always take the latest on each deployment.

Rest all seems good.

-- Sridhar m
Source: StackOverflow