Search code examples
kubernetesgoogle-kubernetes-enginebazelkubectlgoogle-cloud-build

Cloud Build kubectl - How to Apply Output of Previous Step to Kubernetes Cluster


I have a simple cloudbuild.yaml file which runs a Bazel command. This command returns a Kubernetes configuration in form as a log output.

My goal is to take the output of the first step and apply it to my Kubernetes cluster.

steps:
  - name: gcr.io/cloud-builders/bazel
    args: ["run", "//:kubernetes"]

  - name: "gcr.io/cloud-builders/kubectl"
    args: ["apply", "<log output of previous step>"]
    env:
      - "CLOUDSDK_COMPUTE_ZONE=europe-west3-a"
      - "CLOUDSDK_CONTAINER_CLUSTER=cents-ideas"

Update

I've tried the following:

- name: gcr.io/cloud-builders/bazel
  entrypoint: /bin/bash
  args:
    [
      "bazel",
      "run",
      "//:kubernetes",
      " > kubernetes.yaml",
    ]

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

But then I get this error:

Running: kubectl apply -f kubernetes.yaml
error: the path "kubernetes.yaml" does not exist

Solution

  • As everyone already suggested here use volumes.

    Tweak your cloudbuild.yaml file like this:

    - name: gcr.io/cloud-builders/bazel
      entrypoint: /bin/bash
      args:
        [
          "bazel",
          "run",
          "//:kubernetes",
          " > /workspace/kubernetes.yaml",
        ]
    
    - name: "gcr.io/cloud-builders/kubectl"
      args: ["apply", "-f", "/workspace/kubernetes.yaml"]
      env:
        - "CLOUDSDK_COMPUTE_ZONE=europe-west3-a"
        - "CLOUDSDK_CONTAINER_CLUSTER=cents-ideas"