<p>I have a small <code>cloudbuild.yaml</code> file where I build a Docker image, push it to <a href="https://cloud.google.com/container-registry/" rel="nofollow noreferrer">Google container registry</a> (GCR) and then apply the changes to my Kubernetes cluster. It looks like this:</p>

<pre><code>steps:

  - name: 'gcr.io/cloud-builders/docker'
    entrypoint: 'bash'
    args: [
    '-c',
    'docker pull gcr.io/$PROJECT_ID/frontend:latest || exit 0'
    ]

  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "build",
        "-f",
        "./services/frontend/prod.Dockerfile",
        "-t",
        "gcr.io/$PROJECT_ID/frontend:$REVISION_ID",
        "-t",
        "gcr.io/$PROJECT_ID/frontend:latest",
        ".",
      ]

  - name: "gcr.io/cloud-builders/docker"
    args: ["push", "gcr.io/$PROJECT_ID/frontend"]

  - name: "gcr.io/cloud-builders/kubectl"
    args: ["apply", "-f", "kubernetes/gcp/frontend.yaml"]
    env:
      - "CLOUDSDK_COMPUTE_ZONE=europe-west3-a"
      - "CLOUDSDK_CONTAINER_CLUSTER=cents-ideas"

  - name: "gcr.io/cloud-builders/kubectl"
    args: ["rollout", "restart", "deployment/frontend-deployment"]
    env:
      - "CLOUDSDK_COMPUTE_ZONE=europe-west3-a"
      - "CLOUDSDK_CONTAINER_CLUSTER=cents-ideas"

</code></pre>

<p>The build runs smoothly, until the last step. <code>args: ["rollout", "restart", "deployment/frontend-deployment"]</code>. It has the following log output:</p>

<pre><code>Already have image (with digest): gcr.io/cloud-builders/kubectl
Running: gcloud container clusters get-credentials --project="cents-ideas" --zone="europe-west3-a" "cents-ideas"
Fetching cluster endpoint and auth data.
kubeconfig entry generated for cents-ideas.
Running: kubectl rollout restart deployment/frontend-deployment
error: unknown command "restart deployment/frontend-deployment"
See 'kubectl rollout -h' for help and examples.
</code></pre>

<p>Allegedly, <code>restart</code> is an unknown command. But it works when I run <code>kubectl rollout restart deployment/frontend-deployment</code> manually.</p>

<p>How can I fix this problem?</p>

<p>Looking at the <a href="https://v1-15.docs.kubernetes.io/docs/setup/release/notes/" rel="nofollow noreferrer">Kubernetes release notes</a>, the <code>kubectl rollout restart</code> commmand was introduced in the v1.15 version. In your case, it seems Cloud Build is using an older version where this command wasn't implemented yet.</p>

<p>After doing some test, it appears Cloud Build uses a kubectl client version depending on the cluster's server version. For example, when running the following build:</p>

<pre><code>steps:
  - name: "gcr.io/cloud-builders/kubectl"
    args: ["version"]
    env:
      - "CLOUDSDK_COMPUTE_ZONE=&lt;cluster_zone&gt;"
      - "CLOUDSDK_CONTAINER_CLUSTER=&lt;cluster_name&gt;"
</code></pre>

<p>if the cluster's master version is v1.14, Cloud Build uses a v1.14 kubectl client and returns the same <code>unknown command "restart"</code> error message. When master's version is v1.15, Cloud Build uses a v1.15 kubectl client and the command runs successfully.</p>

<p>So about your case, I suspect your cluster "cents-ideas" master version is &lt;1.15 which would explain the error you're getting. As per why it works when you run the command manually (I understand locally), I suspect your kubectl may be authenticated to another cluster with master version >=1.15.</p>
