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"
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
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"
but your code snippet doesn't mention
volumes
/workspace is a default volume shared by all containers for your convenience. You don't need to mount it explicitly. You can attach custom volumes following documentation provided.
ok nice, i'll try it!
unfortunately:
error: the path "/workspace/kubernetes.yaml" does not exist
I tested it myself with this example and it worked just fine with either custom volume or
/workspace
default volume. So the error originates in step 1, duringbazel run
. Can you check if it actually outputs anything to/stdout
during build by running only withargs: ["run", "//:kubernetes"]
. If it doesn't output anything thenkubernetes.yaml
is not created and leads to error in next step. Also you don't need "bazel" argument in first step. It is there by default.