<p>I'm trying to set up a CI pipeline using Cloud Build. My build file builds and pushes the Docker images, and then uses the <code>kubectl</code> builder to update the images in a kubernetes deployment. However I'm getting the following error:</p>

<pre><code>Error from server (NotFound): deployments.extensions "my-app" not found
Running: kubectl set image deployment my-app my-app-api=gcr.io/test-project-195004/my-app-api:ef53550e2ahy784a14iouyh79712c9f
</code></pre>

<p>I've verified via the UI that the deployment is active and has that name. Thought it could be a permissions issue but as far as I know the Cloud Build service account has the Kubernetes Engine Admin role, and is successfully able to pull the cluster auth data in the previous step.</p>

<p>EDIT: As requested, here is my build script:</p>

<pre><code>steps:
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/my-app-api:$COMMIT_SHA', '-f', 'deploy/api/Dockerfile', '--no-cache', '.']
- name: 'gcr.io/cloud-builders/kubectl'
  args:
  - set
  - image
  - deployment
  - my-app
  - my-app=gcr.io/$PROJECT_ID/my-app-api:$COMMIT_SHA
  env:
  - 'CLOUDSDK_COMPUTE_ZONE=${_ZONE}'
  - 'CLOUDSDK_CONTAINER_CLUSTER=${_CLUSTER}'
images: ['gcr.io/$PROJECT_ID/my-app-api:$COMMIT_SHA']
timeout: 5000s
</code></pre>

<p>And the deployment.yaml --</p>

<pre><code>metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: 2018-11-04T17:34:44Z
  generation: 1
  labels:
    app: my-app
  name: my-app
  namespace: my-app
  resourceVersion: "4370"
  selfLink: /apis/extensions/v1beta1/namespaces/my-app/deployments/my-app
  uid: 65kj54g3-e057-11e8-81bc-42010aa20094
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: my-app
    spec:
      containers:
      - image: gcr.io/test-project/my-app-api:f16erogierjf1abd436e733398a08e1b76ce6b712
        imagePullPolicy: IfNotPresent
        name: my-appi-api
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 3
  conditions:
  - lastTransitionTime: 2018-11-04T17:37:59Z
    lastUpdateTime: 2018-11-04T17:37:59Z
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  observedGeneration: 1
  readyReplicas: 3
  replicas: 3
  updatedReplicas: 3
</code></pre>

<p>You need to define the namespace for your deployment when using <code>kubectl</code>
if you're not using the default namespace.</p>

<p>Because you're using namespace for your deployment <code>namespace: my-app</code> you will need to add it to the command using the <code>--namspace</code> flag.</p>

<p>Here's how to do it inside <code>cloudbuild.yaml</code></p>

<pre><code>steps:
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/my-app-api:$COMMIT_SHA', '-f', 'deploy/api/Dockerfile', '--no-cache', '.']
- name: 'gcr.io/cloud-builders/kubectl'
  args:
  - set
  - image
  - deployment
  - my-app
  - my-app=gcr.io/$PROJECT_ID/my-app-api:$COMMIT_SHA
  - --namespace
  - my-app
  env:
  - 'CLOUDSDK_COMPUTE_ZONE=${_ZONE}'
  - 'CLOUDSDK_CONTAINER_CLUSTER=${_CLUSTER}'
images: ['gcr.io/$PROJECT_ID/my-app-api:$COMMIT_SHA']
timeout: 5000s
</code></pre>
